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 4354 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
  • gadget.iom
    0 gadget.iom over 10 years ago

    This is how I interpreted the design brief.

    The code compiles but has not been tested.

     

    int buttonPin = 2;
    int led1 = 3;
    int led2 = 4;
    int led3 = 5;
    
    
    int buttonState = 0;
    int ledState = 0;
    
    
    void setup() {
      // put your setup code here, to run once:
      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
    }
    
    
    void loop() {
      // put your main code here, to run repeatedly: 
      if (buttonState == 0 && digitalRead(buttonPin) == HIGH) {
        buttonState = 1;
        changeState();
      } else {
        buttonState = 0;
      }
    }
    
    
    int changeState() {
      ledState++;
      if (ledState > 3) {
        ledState = 1;
      }
      if (ledState == 1) {
        digitalWrite(led3, LOW);
        digitalWrite(led1, HIGH);
      }
      if (ledState == 2) {
        digitalWrite(led1, LOW);
        digitalWrite(led2, HIGH);
      }
      if (ledState == 3) {
        digitalWrite(led2, LOW);
        digitalWrite(led3, HIGH);
      }
    }

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

    It seems to test OK on the simulator.

     

    Regards,

     

    Dave M.

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

    Thanks. image

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

    Thanks. image

    • 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