element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum ARDUINO UNO LED + SWITCH
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 2 replies
  • Answers 2 answers
  • Subscribers 388 subscribers
  • Views 951 views
  • Users 0 members are here
Related

ARDUINO UNO LED + SWITCH

wimydnvr
wimydnvr over 4 years ago

Hi guys. Can i get an example code for situation below.


if the switch is press, led red will turn on for 3 seconds, then end process.

if the switch is not press, led green will turn on for 3 seconds then end process.

Ive been struggling to do the code.

I use 1 button switch, 1 green led & 1 red led.

  • Sign in to reply
  • Cancel

Top Replies

  • Andrew J
    Andrew J over 4 years ago +3 suggested
    The code for that is really simple so you should be able to figure out what you need. Here's a link to something that shows an led being lit when a button is pressed: you can extrapolate from that to what…
  • javagoza
    javagoza over 4 years ago +1 suggested
    Just use a flag to keep track if the process is done or not. When you detect a change from press to not press or from no press to press activate the flag setup(){ set lastButtonState to pressed; set…
Parents
  • Andrew J
    0 Andrew J over 4 years ago

    The code for that is really simple so you should be able to figure out what you need.  Here's a link to something that shows an led being lit when a button is pressed: you can extrapolate from that to what you need in terms of turning an led on or off.  Arduino function: delay(3000) will pause execution for 3 seconds (3000 milliseconds).  So you could turn an LED on, delay for 3 seconds, then turn it off.

     

    You will have to decide what you mean by "if the switch is not pressed" because of course the majority of the time it won't be pressed, so how often do you want to light the green LED when it detects the button not pressed. 

     

    The link has code that shows you how to detect if a button is pressed or not pressed and how to turn on and turn off an LED.  It also has pictures on how to wire up the button and LED - for your second LED do the same but with a different digital port, e.g. 4.  You don't need to use more than one breadboard if you don't want to as long as you make sure your wiring is correct.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
Reply
  • Andrew J
    0 Andrew J over 4 years ago

    The code for that is really simple so you should be able to figure out what you need.  Here's a link to something that shows an led being lit when a button is pressed: you can extrapolate from that to what you need in terms of turning an led on or off.  Arduino function: delay(3000) will pause execution for 3 seconds (3000 milliseconds).  So you could turn an LED on, delay for 3 seconds, then turn it off.

     

    You will have to decide what you mean by "if the switch is not pressed" because of course the majority of the time it won't be pressed, so how often do you want to light the green LED when it detects the button not pressed. 

     

    The link has code that shows you how to detect if a button is pressed or not pressed and how to turn on and turn off an LED.  It also has pictures on how to wire up the button and LED - for your second LED do the same but with a different digital port, e.g. 4.  You don't need to use more than one breadboard if you don't want to as long as you make sure your wiring is correct.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube