element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • 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
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • 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
Blog how to use multiple buttons with arduino???????
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: nmmbeginer
  • Date Created: 1 Mar 2015 10:23 PM Date Created
  • Views 5167 views
  • Likes 1 like
  • Comments 14 comments
  • help
  • button
  • arduino
Related
Recommended

how to use multiple buttons with arduino???????

nmmbeginer
nmmbeginer
1 Mar 2015

ok so i am new to arduino. i am trying to have one button turn on the led witch is in pin 13 and another button to turn it off but im having problems. when i press the button it turns on when i let off the button it goes off.  here is my code so far.

 

const int buttonPin = 2;     // the number of the pushbutton pin

const int ledPin =  13;      // the number of the LED pin

const int buttonPin1 = 3;

// variables will change:

int buttonState = 0;         // variable for reading the pushbutton status

 

 

void setup() {

  // initialize the LED pin as an output:

  pinMode(ledPin, OUTPUT);    

  // initialize the pushbutton pin as an input:

  pinMode(buttonPin, INPUT); 

  pinMode(buttonPin1, INPUT);

}

 

 

void loop(){

  // read the state of the pushbutton value:

  buttonState = digitalRead(buttonPin);

   buttonState = digitalRead(buttonPin1);

  // check if the pushbutton is pressed.

  // if it is, the buttonState is HIGH:

  if (buttonState == HIGH) {   

    // turn LED on:  

    digitalWrite(ledPin, HIGH);

  }

  {

    delay(100);  // Wait 0.5 seconds before re-checking button states

  }

    if (buttonState == HIGH) {   

    // turn LED on:  

    digitalWrite(ledPin, LOW);

    }

 

 

}

 

if some one can tell me what im doing wrong please tell me.i just started programming and i dont know much i can read and under stand code but i am not good at writing code.

  • Sign in to reply

Top Comments

  • gadget.iom
    gadget.iom over 11 years ago in reply to nmmbeginer +4
    The following variables define the physical pin each button is connected to: const int buttonPin = 2; const int buttonPin1 = 3; In this case you've essentially names the switch connected to pin 2 'buttonPin…
  • clem57
    clem57 over 11 years ago in reply to nmmbeginer +2
    nmmbeginer I would suggest to take code you had working for one LED and duplicate it with each variable changed to a new one like LED to LED2 etc. The logic is the same, but need there own set of variables…
  • nmmbeginer
    nmmbeginer over 10 years ago in reply to gadget.iom +2
    so when i posted this post i knew almost next to nuthen about arduino and how to program it. basically i was trying to do something that was way over my head. so i went and watched a bunch of beginner…
  • nmmbeginer
    nmmbeginer over 10 years ago in reply to gadget.iom

    so when i posted this post i knew almost next to nuthen about arduino and how to program it. basically i was trying to do something that was way over my head. so i went and watched a bunch of beginner videos and did some simple things first. now i came back and took another look at this code and now i understand it soooo much better. i got it working and now starting to continue on with my project. thank you for explaining what i was doing wrong.

    sincerely

    nathanael

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 10 years ago

    Dear Nathanael (and others reading this)...

     

    By all means... press on in the direction you have started, get it "working"... but learn this from this project: One of the great things about "playing" with Arduinos is that there is no one right answer!

     

    There are all sorts of ways to do JUST "turn lights on and off with buttons".

     

    The idea you had originally, one button for on, one for off, is something that would actually be used in some real world applications. Once you get the other answer working, go back, try to get your original idea working TOO! (^_^)

     

    Before long, you're going to need to learn about, conquer "switch de-bouncing". Google will give you a lifetime of discussions of the matter.

     

    Think about this challenge. It may SEEM "simple" enough at first... but it is harder than it seems. And harder to program WELL. (Code that WORKS is not necessarily GOOD code)

     

    Set up an Arduino with 3 (or more) buttons, 3 or more LEDs. Each button triggers a different "pretty pattern" on the LEDs... for the real challenge to emerge, these patterns have to be "long"... say, five seconds to repeat themselves. So... pattern 1 might be "first LED on, wait two seconds, second LED on, wait two seconds, third LED on, wait two seconds. Turn all off and immediately start again."

     

    The tricky bit, unless you are lucky (or brilliant) with how you went about it: Fixing things so that if you press button 2 when the LEDs are half way through doing pattern 1, the Arduino switches (immediately) do doing pattern two.

     

    Now... if you have lots of LEDs... 6?... and only two buttons, you could still demand three patterns. One for button 1 pressed. One for button 2 pressed. A different one for BOTH buttons pressed. (If neither button is pressed, it should carry on doing whatever it was doing before.)

     

    THAT is not easy. Be sure to write it in a way that makes it trivial to add a third button if you think you can come up with 7 interesting patterns. If you use a few smart LEDs, you can certainly make 7 fun patterns.

     

    Smart LEDs?....

     

    www.arunet.co.uk/tkboyd/ec/WS2812SmartLEDs.htm

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • clem57
    clem57 over 11 years ago in reply to nmmbeginer

    nmmbeginer

      I would suggest to take code you had working for one LED and duplicate it with each variable changed to a new one like LED to LED2 etc. The logic is the same, but need there own set of variables.

    Cheers,

    Clem

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • nmmbeginer
    nmmbeginer over 11 years ago in reply to gadget.iom

    ok but in your code in line 23 when  you press button it tun on but when u let off it turn of. i would like when that button is pressed to turn on and stay on. THEN when i press the button in line 30 i want it to turn off and stay off till the button in line 23 is pressed. u may ask why i want that well because i do. image my plan eventually is to make a security lock. i have a keypad that has 12 buttons when i press (for example) 1,4,7 on the key pad i want the light to come on and when i press * i want it to turn off. but for now i want to get just one button to turn light on and one to turn it off. like i said before i can read code but i cant wright it. image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • gadget.iom
    gadget.iom over 11 years ago in reply to nmmbeginer

    The following variables define the physical pin each button is connected to:

    const int buttonPin = 2; 
    const int buttonPin1 = 3;

    In this case you've essentially names the switch connected to pin 2 'buttonPin' and the switch connected to pin 3 as 'buttonPin1'

     

    This line of code reads the status and puts it into a variable for you

    buttonState = digitalRead(buttonPin);
    buttonState = digitalRead(buttonPin1);

    In this case you have loaded the variable with the status of buttonPin (Pin 2) and then overwritten it with the status of 'ButtonPin1' (Pin 3)

     

    Your code should read:

    buttonState = digitalRead(buttonPin);
    buttonState1 = digitalRead(buttonPin1);

    notice the 'buttonState1' variable on line 2.

     

    These variables can then be called to refer to each button.

     

    So to fix your original code:

    const int buttonPin = 2;     // the number of the pushbutton pin
    const int ledPin =  13;      // the number of the LED pin
    const int buttonPin1 = 3;
    // variables will change:
    int buttonState = 0;         // variable for reading the pushbutton status
    
    
    void setup() {
      // initialize the LED pin as an output:
      pinMode(ledPin, OUTPUT);    
      // initialize the pushbutton pin as an input:
      pinMode(buttonPin, INPUT); 
      pinMode(buttonPin1, INPUT);
    }
    
    
    void loop(){
      // read the state of the pushbutton value:
      buttonState = digitalRead(buttonPin);
       buttonState = digitalRead(buttonPin1);
      // check if the pushbutton is pressed.
      // if it is, the buttonState is HIGH:
      if (buttonState == HIGH) {   
        // turn LED on:  
        digitalWrite(ledPin, HIGH);
      }
      {
        delay(100);  // Wait 0.5 seconds before re-checking button states
      }
        if (buttonState == HIGH) {   
        // turn LED on:  
        digitalWrite(ledPin, LOW);
        }
    
    
    }

    Line 20 and 30 need to read 'buttonState1'.

     

    Hope this makes sense.

    • Cancel
    • Vote Up +4 Vote Down
    • Sign in to reply
    • More
    • 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 © 2026 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