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
  • 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 4912 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 10 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 10 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…
  • gadget.iom
    gadget.iom over 9 years ago in reply to engineerhitesh

    What exactly do you want these other pins to do?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • engineerhitesh
    engineerhitesh over 9 years ago

    I want to use multiple digital pins like 3,4,5 ,here is code only for 2 number pin .how can i use multiple pins having same function in this code ..which are the changes are require in this code?.please help ,thank you...

     

     

     

    int state = 0;

    const int pin = 2;

    void setup()

    {

      //Set Exact Baud rate of the GSM/GPRS Module.

      Serial.begin(9600);

    }

    void loop()

    {

      if (digitalRead(pin) == HIGH && state == 0) {

        Serial.print("\r");

        delay(1000);

        Serial.print("AT+CMGF=1\r");

        delay(1000);

        /*Replace XXXXXXXXXX to 10 digit mobile number &

        ZZ to 2 digit country code*/
        Serial.print("AT+CMGS=\"+ZZXXXXXXXXXX\"\r");
        delay(1000);
        //The text of the message to be sent.
        Serial.print("HELLO WORLD");
        delay(1000);
        Serial.write(0x1A);
        delay(1000);
        state = 1;
      }
      else {
        state = 0;
      }
    }
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • gadget.iom
    gadget.iom over 10 years ago in reply to Former Member

    Arduino also has a Keyboard library which may simplify things. You can read more about it here: Arduino Playground - Keypad Library

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

    Just as some advice, when you do go ahead and do the security lock thing with the keypad, you're going to want to do something with bit-shift ( << or >>) so that way you don't have to use up all of your IO on your arduino for each individual button.

     

    I don't remember how it works but I did it while ago and it opens up a ton of possibilities having all of the extra IO.

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

    You're welcome. image

     

    These kind of things can seem impossible at first, but you quickly get your head around them.

     

    Thank you for getting back in touch and letting us know how you got on.

     

    Paul

    • Cancel
    • Vote Up 0 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 © 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