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 4344 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
  • dmaruska
    0 dmaruska over 10 years ago

    George,  Give this a try.  I'm assuming that you are using the D I/O 13 - 10, and the Button press (pin 10) is low to hi when pressed.  LEDS are off when low outputs are on D I/O 13 to 11.  I'm sure there are more ways to do this, but I wrote this and its seems to test ok on the simulator.

     

    //LED SEQUENCER WITH BUTTON

    int ledPin1 = 13;

    int ledPin2 = 12;

    int ledPin3 = 11;

    int butPress1 = 10;

    int countBP = 0;

     

     

    void setup()

    {

      pinMode(ledPin1, OUTPUT);

      pinMode(ledPin2, OUTPUT);

      pinMode(ledPin3, OUTPUT);

      pinMode(butPress1, INPUT);

    }

     

     

    void loop()

    {

      digitalWrite(ledPin1, LOW);

      digitalWrite(ledPin2, LOW);

      digitalWrite(ledPin3, LOW);

      countBP = digitalRead(butPress1);

     

      while (countBP != 0)

      {

        switch (countBP)

          {

            if (countBP == 0)

          {

         case 1:

          digitalWrite(ledPin1, HIGH);

          digitalWrite(ledPin2, LOW);

          digitalWrite(ledPin3, LOW);

          countBP = countBP + digitalRead(butPress1);

          break;

          }

            if (countBP == 1)

          {

        case 2:

          digitalWrite(ledPin1, LOW);

          digitalWrite(ledPin2, HIGH);

          digitalWrite(ledPin3, LOW);

          countBP = countBP + digitalRead(butPress1);

          break;

          }

            if (countBP == 2)

          {

        case 3:

          digitalWrite(ledPin1, LOW);

          digitalWrite(ledPin2, LOW);

          digitalWrite(ledPin3, HIGH);

          countBP = countBP + digitalRead(butPress1);

          break;

        }

        default:

          digitalWrite(ledPin1, LOW);

          digitalWrite(ledPin2, LOW);

          digitalWrite(ledPin3, LOW);

          countBP = 0;

          break; 

        }

      }

    }

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

    Thank you both for the replies. In regards to the first reply, the best I had was a simple on/then off with momentary pushbutton code. The problem I was having is finding a way to expand that to more LEDs with just the one button.

     

    That code is much appreciated. I suppose it may need a debounce function, but what you wrote is what I couldn't wrap my Newb Head around. I appreciate the knowledge. By the way, which simulator do you use?

     

    image

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

    Does the provided code loop through all the LEDs as you hold the button down?

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

    www.virtronics.com.au, Simulator for Arduino Proversion V0.99D.  Its not expensive.  I'm very new to Programming also, this is the 2nd time I have ever used CASE statements.  You need to create a flow chart to understand,first what you want to do, and will the micro in its simplest form, (I/O) do it, and what is needed to do it.  The software sets the initial state, for all i/o and counter.  I noticed that it can miss button push, during the testing, but it will keep looping at the state count you have reached for each loop, ie if all off, will stay all off, or led 1 on, will stay on until reset or counter is incremented by button push, and so on until counter reaches 4 and dumps out and resets counter to 0.  As stated, I'm VERY new to programming and I'm sure there is a better way, but if it works.....  As for debounce, you can add code for that or a CAP to increase the RC time.  There are also Debounce chips on the market that will work also.

     

    Regards,

     

    Dave M.

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

    www.virtronics.com.au, Simulator for Arduino Proversion V0.99D.  Its not expensive.  I'm very new to Programming also, this is the 2nd time I have ever used CASE statements.  You need to create a flow chart to understand,first what you want to do, and will the micro in its simplest form, (I/O) do it, and what is needed to do it.  The software sets the initial state, for all i/o and counter.  I noticed that it can miss button push, during the testing, but it will keep looping at the state count you have reached for each loop, ie if all off, will stay all off, or led 1 on, will stay on until reset or counter is incremented by button push, and so on until counter reaches 4 and dumps out and resets counter to 0.  As stated, I'm VERY new to programming and I'm sure there is a better way, but if it works.....  As for debounce, you can add code for that or a CAP to increase the RC time.  There are also Debounce chips on the market that will work also.

     

    Regards,

     

    Dave M.

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