Button Switch

In the previous post, I learned how to wire a button to control an LED. However, this sample was limited because the state of the button directly controlled the state of the LED.

I wish to have the LED turn on/off each time a button is pressed and released. The wiring remains the same, however its the logic of the programming that can now solve the problem.

As you can see, it works pretty well.  Source code, as always, can be found at the bottom of this post. Here are the improvements:

I added a boolean variable called tester. Each time the light toggles on/off, so does the variable toggle true/false. In this way we can keep track of the state of the LED.

Since the arduino runs in an infinite loop, if the button is held down long enough, the status of the light changes back and forth. This is not wanted. Therefore I put in the while loop to delay and check the status of the button.  This way, the status of the light changes the moment the button is depressed, and no matter how long it is held, it will not affect the LED.


/*

ButtonSwitch

Button presses toggle an LED on/off.

The circuit:

* LED attached from pin 13 to ground

* pushbutton attached to pin 2 from +5V

* 10K resistor attached to pin 2 from ground

* Note: on most Arduinos there is already an LED on the board

attached to pin 13.

created 5/16/2010

Jordan LeGrand

https://arduinostudy.wordpress.com

This is a modification of the sample button sketch bundled with the arduino IDE.

*/

// constants won't change. They're used here to

// set pin numbers:

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

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

// variables will change:

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

boolean tester =false;       // variable storing the state of the LED (false = off, true = on)

void setup() {

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

}

void loop(){

// read the state of the pushbutton value:

buttonState = digitalRead(buttonPin);

//if the button is pressed...

if (buttonState == HIGH) {

//if the light is currently on, turn it off, and set the boolean to false.

if (tester == true){

digitalWrite(ledPin,LOW);

tester = false;

}

//if the light is off, turn it on, and set the boolean to true.

else{

digitalWrite(ledPin,HIGH);

tester = true;

}

//while the button is held down, pause the main loop until it is released.

while(buttonState==HIGH){

delay(10);

buttonState=digitalRead(buttonPin);

}

}

}

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s