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 5169 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 11 years ago in reply to clem57

    thanks for all ur help i got it working. now lets say i want to do 2 thing. for example button 1 turns the led1 on and off but button 2 turns led2 on and off how do i identify the buttons

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

    after testing button for a press

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

    ok so i see how this would work but where do i put the    LEDState^=1

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • clem57
    clem57 over 11 years ago

    First you really need only 1 button! Add a variable that remembers whether LED is on/off. When buttonState is HIGH then change LED state by flipping the bit with XOR:

     

    int LEDState  = 0; // Put this up top

    // flip it

    LEDState ^= 1;

     

    Change LED with

    digitalWrite(ledPin, LEDState);  // 0 for off and 1 for on!

    • Cancel
    • Vote Up +1 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