element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • More
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • More
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • More
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • More
  • 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
Arduino
  • Products
  • More
Arduino
Arduino Forum ARDUINO UNO LED + SWITCH
  • Blog
  • Forum
  • Documents
  • Events
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Arduino requires membership for participation - click to join
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 2 replies
  • Answers 2 answers
  • Subscribers 100 subscribers
  • Views 240 views
  • Users 0 members are here
Related

ARDUINO UNO LED + SWITCH

wimydnvr
wimydnvr over 1 year 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.

  • Reply
  • Cancel
  • Cancel

Top Replies

  • Andrew J
    Andrew J over 1 year 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…

  • javagoza
    javagoza over 1 year 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;
      …

  • Andrew J
    0 Andrew J over 1 year 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
    • Up +3 Down
    • Reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • javagoza
    0 javagoza over 1 year ago

    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 processedFlag to false;
    }
    loop(){
      read newButtonState;
      if lastButtonState is not equal to newButtonState and processedFlag is false  then { 
        if(lastButtonState is pressed) {
          turn on red led;
          wait for 3 seconds;
          turn off red led;
        else {
          turn on green led;
          wait for 3 seconds;
          turn off green led;
         }
    
         set processedFlag to true;
    
      } else{
        set processedFlag to false;
      }
      set lastButtonState to newButtonState;
      delay some time for debouncing button
    }

     

    if the leds are on during 3 seconds you can skip the debouncing delay

    • Cancel
    • Up +1 Down
    • Reply
    • Verify Answer
    • Reject Answer
    • Cancel
Element14

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 © 2022 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

  • Facebook
  • Twitter
  • linkedin
  • YouTube