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 Simple LED Sequence with Button Question..
  • 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 Verified Answer
  • Replies 20 replies
  • Answers 1 answer
  • Subscribers 394 subscribers
  • Views 4357 views
  • Users 0 members are here
Related

Simple LED Sequence with Button Question..

medictrode
medictrode over 10 years ago

Hello all..

 

I want to make a simple 3 LED Sequence circuit and would like some assistance as to where to get started. Basically, press a push button, have LED1 Light, press it again, LED2 Light, Press again LED3 on, Press again back to LED1.Etc... I just can't get it in my head to to what code to use. Thank you for any assistance.image

  • Sign in to reply
  • Cancel

Top Replies

  • medictrode
    medictrode over 10 years ago in reply to D_Hersey +2
    Your 100% correct but You can do a lot of the arduino basic projects you see floating around with the old school simple logic chips. In my case I already built one with a hardware debounce circuit and…
  • mcb1
    mcb1 over 10 years ago in reply to medictrode +2 verified
    George Well done on exploring the possibilities of microcontrollers. Yes you could do this with hardware, but the idea was to do first steps and learn, then progress to more complicated that cannot be…
  • dmaruska
    dmaruska over 10 years ago in reply to gadget.iom +1
    It seems to test OK on the simulator. Regards, Dave M.
Parents
  • medictrode
    0 medictrode over 10 years ago

    Ok, so I got the code in and am trying my best to wrap my head around what is happening. I currently have 2 issues to deal with. The first is either a hardware or software debounce is needed. The second, which is more of the issue is when I press the button, all three LEDs will illuminate. Once the button is released the next LED will activate correctly, minus any debounce issues. I will play around some more and report any findings. As far as hookup goes, each LED has a current limiting resistor, and the button has a 10k pull up resistor before Pin 2. 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • medictrode
    0 medictrode over 10 years ago

    Ok, so I got the code in and am trying my best to wrap my head around what is happening. I currently have 2 issues to deal with. The first is either a hardware or software debounce is needed. The second, which is more of the issue is when I press the button, all three LEDs will illuminate. Once the button is released the next LED will activate correctly, minus any debounce issues. I will play around some more and report any findings. As far as hookup goes, each LED has a current limiting resistor, and the button has a 10k pull up resistor before Pin 2. 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • gadget.iom
    0 gadget.iom over 10 years ago in reply to medictrode

    Would you mind trying my code on your hardware to see if that works and what issues there might be with that one?

    It's a slightly different approach.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • dmaruska
    0 dmaruska over 10 years ago in reply to medictrode

    What is the initial status of the button press variable.  If the pin is started high and you are looking for a low. Is this what you are doing?  You stated you pulled pin 2 with 10k and switch?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • medictrode
    0 medictrode over 10 years ago in reply to dmaruska

    Thanks again for the help..

     

    I tried both sets of code here with a similar outcome. In both cases the LEDs will all light when the button is pressed, then when released it appears to cycle to the next LED and the other 2 will go out relative to any switch bounce that is happening. 

     

    The switch is being pulled low with a 10k on pin 2, then the switch will bring it high when pressed. I suppose a schematic should be placed here, I will have to do that.

     

    I also tried this code below in an attempt to bring some debounce code in, but it literally does nothing.. Hmmm

     

     

     

    int buttonPin = 2;  
    int led1 = 3;  
    int led2 = 4;  
    int led3 = 5; 
    
    // initialize all leds to zero at start
    int led1State = 0;
    int led2State = 0;
    int led3State = 0;
    
    
    // finite state machine:
    // 0 = all outputs off
    // 1 = output1 on
    // 2 = output2 on
    // 3 = output3 on
    int fsm = 0;
    
    
    int buttonState = 0;             // the current reading from the input pin
    int lastButtonState = 0;   // the previous reading from the input pin
    
    
    // the following variables are long's because the time, measured in miliseconds,
    // will quickly become a bigger number than can be stored in an int.
    long lastDebounceTime = 0;  // the last time the output pin was toggled
    long debounceDelay = 50;    // the debounce time; increase if the output flickers
      
      
    void setup() {  
    
    
      pinMode(buttonPin, INPUT);      // sets the button pin as input  
      pinMode(led1, OUTPUT);      // sets the led pin as output  
      pinMode(led2, OUTPUT);      // sets the led pin as output  
      pinMode(led3, OUTPUT);      // sets the led pin as output  
      
      digitalWrite(led1, led1State);
      digitalWrite(led2, led2State);
      digitalWrite(led3, led3State);
    }  
      
     void loop() {
      // read the state of the switch into a local variable:
      int digRead1 = digitalRead(buttonPin);
    
    
      // check to see if you just pressed the button 
      // (i.e. the input went from LOW to HIGH),  and you've waited 
      // long enough since the last press to ignore any noise:  
    
    
      // If the switch changed, due to noise or pressing:
      if (digRead1 != lastButtonState) {
      // reset the debouncing timer
      lastDebounceTime = millis();
      } 
      
      if ((millis() - lastDebounceTime) > debounceDelay) {
      // whatever the reading is at, it's been there for longer
      // than the debounce delay, so take it as the actual current state:
    
    
      // if the button state has changed:
      if (digRead1 != buttonState) {
      buttonState = digRead1;
    
    
      // only toggle the LED if the new button state is HIGH
      if (buttonState == 1) {
      changeState();
      }
      }
      }
    } 
    
    
    int changeState() {
      switch(fsm) {
      case 0:
      led1State = 0;
      led2State = 0;
      led3State = 0;
      break;
      case 1:
      led1State = 1;
      led2State = 0;
      led3State = 0;
      break;
      case 2:
      led1State = 0;
      led2State = 1;
      led3State = 0;
      break;
      case 3:
      led1State = 0;
      led2State = 0;
      led3State = 1;
      break;
      }
    
      digitalWrite(led1, led1State);
      digitalWrite(led2, led2State);
      digitalWrite(led3, led3State);
    
      if (fsm == 3) {
      fsm = 0;
      } else {
      fsm++;
      }
    
    
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 10 years ago in reply to medictrode

    George

    Well done on exploring the possibilities of microcontrollers.

    Yes you could do this with hardware, but the idea was to do first steps and learn, then progress to more complicated that cannot be done easily with hardware. image

     

     

    I see you have used the Debounce example in the Arduino IDE.

    It works but IMO is wasteful because it constantly writes the LED output, and constantly resets the lastButtonState.

    (note ... they change lastButtonState constantly)

     

     

    Your problem starts in line 54.

    You read the input and compare it to lastButtonState.

    This never changes, so as soon as the button is HIGH line 54 equates to TRUE.

     

    You reset the timer, BUT in line 59 you are checking 'are we there yet'.

    Line 59 will equate to FALSE, and you return to the loop.

    You then read the button and again reset the timer ... etc.

     

    If you add this

    lastButtonState = digRead1;

     

    if (buttonState == 1)
              {
                changeState();
              }
            }
          }
        lastButtonState = digRead1;
        }

    at Line 75 and shift the } down one more line it should work.

     

     

    One alternative is this approach

    void Check_Buttons()
    
      /* This routine checks the timer (LastButtonCheck) and if necessary reads the button input
         If the button is detected, it waits until it counts it 10 times at the set time rate.
         This prevents button bounce from triggering and ensures the button is pressed.
       
         Once a valid button press has been detected, it starts a timer (ButtonPressTime),
        
      */ 
        {
          if (millis() - LastButtonCheck > 5)                    // Reads button state every 5mS and then updates button counts
          {     
            ButtonState = digitalRead(Pushbutton);
            LastButtonCheck = millis();
          
            if (ButtonState == HIGH)
            {
              ButtonCount ++;                                     // Increment the Count by 1
              if (ButtonCount > 5)                               // the button should be HIGH for 5x5mS = 25mS
              {
                // Button has been pressed for longer than 25mS so its valid
                ButtonPressTime = millis();                       // set the Button press timer
                ButtonCount = 0;
                ProcessButton();                                  // Do something with the valid button press
              }
            }
            else                                                  // Button is LOW
            {
              ButtonCount =0;                                     // Reset the counter as the button has been released
            }
          }
        }

     

    While it checks the button every xx it doesn't do anything unless its in that state for 5 times x.

     

    They do something similar here

    http://playground.arduino.cc/Learning/SoftwareDebounce

     

     

     

    Your reset of fsm also needs a tweak.

    You reset the counter to zero in line 107 and you have no case for zero.

    It therefore takes two button presses after the third LED is light.

     

    It's a simple fix fsm =1;

     

     

    Keep it up its the best way to learn.

    BTW I always comment at the start about how the inputs are connected, since later I will have forgotten image, or if someone uses my code they shouldn't need an explaination.

     

    Mark

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Reject Answer
    • Cancel
  • shabaz
    0 shabaz over 10 years ago in reply to mcb1

    Hehe I do the same, write down any pins/connections, and sometimes even the commands to compile the code, because guaranteed I too will have forgotten in a few months time.

     

    Also, although eventually we all build up our personal structures/templates for how to handle popular tasks like button debounce, LED toggling or indications of power/error/etc., it's cool to see implementations because it helps refine or rethink my own sometimes.

     

    Also, quite amazing sometimes how much sophisticated user interaction can occur with just a single button, once you start to accept things like (say) pressed for a short or for a long length of time to mean something, or quick-double-press to mean something else, etc.

    For some home lights I used normal (short) press to change brightness levels, and a long-press to power off as an example.

    The quick-double-press is underrated, it should be used more in projects ; )

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 10 years ago in reply to shabaz

    http://www.element14.com/community/people/shabaz

    The long/short button press might be medictrode next steps.

    There is also the flashing of a LED based on an error.

     

    I have one I use that has long (10's) and short (1's).

     

    However small steps first.

    Mark

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • medictrode
    0 medictrode over 10 years ago in reply to mcb1

    You are pretty amazing sir as your solutions made everything come alive and work properly. Also a huge thanks for all the replies and this forum in general. I figured after hearing about it on all the Ben Heck shows I've watched I better check it out. Quite a few other electronics related forums I've been on over the years don't encourage learning from experience, and I've run into rude replies. So for being fair and helpful, again, Thank You...image

     

    Now onto my next Arduino project that has been stumping me for quite some time. I will start another thread soon and see if you awesome experts can assist.. image

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 10 years ago in reply to medictrode

    George

    Glad it worked out.

     

    Can I suggest trying some debugging using Serial.print and maybe a delay to slow it down.

    I have to admit to resrting to this more than I should, and the result is usually a " Duuuh why did I do that ..."

     

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
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