loading

Push button is a very very common component used in most of the DIY projects.

Programming a push button is a piece of cake. Right ?

Anyway, there are still some ticks on it.

Step 1: Control LED on and Off by One Push Button -- 1st Way

//Button_01
//Control LED on and off by a push button
//
// created by uvvvvw
// 2015-04-05
//
const int Button = 12; //Define button pin
const int LED = 4; //Define LED pin
int LED_state = LOW; //Status of LED : LOW(on), HIGH(off)
//
//
void setup()
{
pinMode(Button, INPUT);
digitalWrite(Button, HIGH); //Use internal pull up resistor for the button. It saved a resistor for the button.
//
pinMode( LED, OUTPUT);
digitalWrite( LED, LOW);
}
//
//
void loop()
{
if (digitalRead(Button)==LOW) //if button is pressed
{
if ( LED_state == LOW)
{//if the LED is off at this moment,
digitalWrite( LED, HIGH); //turn it on
LED_state = HIGH; //new status of the LED is HIGH
}
else
{//if the LED is on at this moment,
digitalWrite( LED, LOW); //trun it off
LED_state = LOW; //new status of the LED is LOW
}
}
delay(300); //A very simple way to handle button bouncing, but need to adjust the delay time to optimize the result.
}

Step 2: Control LED on and Off by One Push Button -- 2nd Way

Same as "Button_01", except bitwise XOR is used for simplification purpose.

//Button_02
//Control LED on and off by a push button
//Same as "Button_01", except bitwise XOR is used for simplification purpose
//
// created by uvvvvw
// 2015-04-05
//
const int Button = 12; //Define button pin
const int LED = 4; //Define LED pin
//
//
void setup()
{
pinMode(Button, INPUT);
digitalWrite(Button, HIGH); //Use internal pull up resistor for the button. It saved a resistor for the button.
//
pinMode( LED, OUTPUT);
digitalWrite( LED, LOW);
}
//
//
void loop()
{
if (digitalRead(Button)==LOW) //if button is pressed
{
digitalWrite( LED, digitalRead(LED) ^ 1); //read the state of the LED, reverse it by XOR(^).And turn it on or off accordingly
delay(300); //A very simple way to handle button bouncing, but need to adjust the delay time to optimize the result.
}
}

Step 3: Control LED On, Blink and Off by a Push Button

//Button_03
//Control LED on, blink and off by a push button
//
// created by uvvvvw
// 2015-04-05
//
const int Button = 12; //Define button pin
const int LED = 4; //Define LED pin
int LED_state = 3; //Status of LED : 1(on), 2(blink), 3(off)
//
//handle the blinking while LED_state == 2
long interval = 500; //interval of blinking (milliseconds)
unsigned long currentMillis; //record the current time for blinking
long previousMillis = 0; //store the last time LED was updated
//
//
void setup()
{
pinMode(LED, OUTPUT);
pinMode(Button, INPUT);
digitalWrite(Button, HIGH); //Use internal pull up resistor for the button. It saved a resistor for the button.
}
//
//
void loop()
{
if (digitalRead(Button) == LOW) //if button is pressed
{
LED_state++;
if (LED_state > 3) LED_state = 1; //button State from 1 to 3
//
delay(300); //A very simple way to handle button bouncing, but need to adjust the delay time to optimize the result.
}
//
switch (LED_state)
{
case 1://LED on
digitalWrite(LED, HIGH);
break;
case 2://LED blink
currentMillis = millis(); //start the interval counting
if (currentMillis - previousMillis > interval)
{
digitalWrite( LED, digitalRead( LED ) ^ 1 ); //read the state of the Led, reverse it by XOR.And turn it on or off accordingly
previousMillis = currentMillis;
}
break;
case 3://LED off
digitalWrite(LED, LOW);
break;
}
//
}

Step 4: Turn Three LEDs on and Off in Sequence by One Push Button

//Button_04
//Turn three LEDs on and off in sequence by one push button
//
// created by
// 2015-04-05
//
const int Button = 12; //Define button
const int LED_01 = 4; //Define pin# of 1st LED
const int LED_02 = 3; //Define pin# of 3nd LED
const int LED_03 = 2; //Define pin# of 3rd LED
int LED_state = 1; //Status of LED : 1(LED_01 on), 2(LED_02 on), 3(LED_03 on)
//
//
void setup()
{
pinMode(Button, INPUT);
digitalWrite(Button, HIGH); //Use internal pull up resistor for the button. It saved a resistor for the button.
//
pinMode( LED_01, OUTPUT);
digitalWrite( LED_01, LOW);
pinMode( LED_02, OUTPUT);
digitalWrite( LED_02, LOW);
pinMode( LED_03, OUTPUT);
digitalWrite( LED_03, LOW);
}
//
//
void loop()
{
if (digitalRead(Button) == LOW) //if button is pressed
{
LED_state++;
if (LED_state > 3) LED_state = 1; //button State from 1 to 3
//
delay(300); //A very simple way to handle button bouncing, but need to adjust the delay time to optimize the result.
}
//
switch (LED_state)
{
case 1://LED_01 on
digitalWrite(LED_01, HIGH);
digitalWrite(LED_02, LOW);
digitalWrite(LED_03, LOW);
break;
case 2://LED_02 on
digitalWrite(LED_01, LOW);
digitalWrite(LED_02, HIGH);
digitalWrite(LED_03, LOW);
break;
case 3://LED_03 on
digitalWrite(LED_01, LOW);
digitalWrite(LED_02, LOW);
digitalWrite(LED_03, HIGH);
break;
}
//
}

Step 5: Turn on All Three LEDS Gradually Through One Single Push Button

//Button_05
//Turn on all three LEDS gradually through one single push button
//
// created by
// 2015-04-05
//
const int Button = 12; //Define button
const int LED_01 = 4; //Define pin# of 1st LED
const int LED_02 = 3; //Define pin# of 3nd LED
const int LED_03 = 2; //Define pin# of 3rd LED
int LED_state = 0; //Status of LED : 0(all LEDs off), 1(LED_01 on), 2(LED_02 on), 3(LED_03 on)
//
//
void setup()
{
pinMode(Button, INPUT);
digitalWrite(Button, HIGH); //Use internal pull up resistor for the button. It saved a resistor for the button.
//
pinMode( LED_01, OUTPUT);
digitalWrite( LED_01, LOW);
pinMode( LED_02, OUTPUT);
digitalWrite( LED_02, LOW);
pinMode( LED_03, OUTPUT);
digitalWrite( LED_03, LOW);
}
//
//
void loop()
{
if (digitalRead(Button) == LOW) //if button is pressed
{
LED_state++;
if (LED_state > 3) LED_state = 0; //button State from 0 to 3
//
delay(300); //A very simple way to handle button bouncing, but need to adjust the delay time to optimize the result.
}
//
switch (LED_state)
{
case 0://All LEDs off
digitalWrite(LED_01, LOW);
digitalWrite(LED_02, LOW);
digitalWrite(LED_03, LOW);
break;
case 1://LED_01 on
digitalWrite(LED_01, HIGH);
digitalWrite(LED_02, LOW);
digitalWrite(LED_03, LOW);
break;
case 2://LED_02 on
digitalWrite(LED_01, HIGH);
digitalWrite(LED_02, HIGH);
digitalWrite(LED_03, LOW);
break;
case 3://LED_03 on
digitalWrite(LED_01, HIGH);
digitalWrite(LED_02, HIGH);
digitalWrite(LED_03, HIGH);
break;
}
//
}

Step 6: Turn Two LEDs on and Off Individually by One Single Push Button

That is use one single button as two buttons.

//Button_06
//Turn two LEDs on and off individually by one single push button
//That is use one single button as two buttons
//
// created by
// 2015-04-05
//
const int Button = 12; //Define button
const int LED_01 = 4; //Define pin# of 1st LED
const int LED_02 = 3; //Define pin# of 3nd LED
int Button_still_pressed = LOW; //Status of the Button
long Button_count = 0; //Record the pressing time
int LED_affected = 0; //Affected LED : 0(no LED affected), 1(LED_01), 2(LED_02)
//
//
void setup()
{ pinMode(Button, INPUT);
digitalWrite(Button, HIGH); //Use internal pull up resistor for the button. It saved a resistor for the button.
//
pinMode( LED_01, OUTPUT);
digitalWrite( LED_01, LOW);
pinMode( LED_02, OUTPUT);
digitalWrite( LED_02, LOW);
}
//
//
void loop()
{
do {
if (digitalRead(Button) == LOW) //if button is pressed
{
Button_still_pressed = HIGH;
Button_count++;
//
if (Button_count <100000)
{
LED_affected = 1; //for short press, LED_01 is affected
}
if (Button_count == 100000) //this value defines the time required for long press
{
LED_affected = 2; //for long press, LED_02 is affected
digitalWrite( LED_02, digitalRead(LED_02) ^ 1); //read the state of the LED_02, reverse it by XOR(^).And turn it on or off accordingly
}
}
else
{
Button_still_pressed = LOW;
}
}while (Button_still_pressed);
//
//
if (LED_affected == 1) digitalWrite( LED_01, digitalRead(LED_01) ^ 1); //read the state of the LED_01, reverse it by XOR(^).And turn it on or off accordingly
LED_affected = 0;
Button_count = 0;
delay(300); //A very simple way to handle button bouncing, but need to adjust the delay time to optimize the result.
}

Step 7: Control Value Changing Speed Based on Pressing Time of a Push Button

If the button is pressed one time, the value will be increased by one.


But if the button is pressed continuously, the value will be increased by one continuously at the
beginning. And later on, the value will be increased by 10 continuously. And then 100 if keep on
pressing the button.

//Button_07
//Control changing speed based on pressing time of a push button
//
// created by uvvvvw
// 2015-04-05
//
#include
#include

LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
//
const int Button = 12; //Define button pin
int Button_still_pressed = LOW; //Status of the Button
long Button_count = 0; //Record the pressing time
long Value = 0; //Value that will be increased while the Button is pressed
//
//
void setup()
{
pinMode(Button, INPUT);
digitalWrite(Button, HIGH); //Use internal pull up resistor for the button. It saved a resistor for the button.
//
lcd.init();
delay(400);
lcd.init();
lcd.clear();
lcd.cursor();
lcd.blink();
lcd.backlight();
//
lcd.setCursor(0,0);
lcd.print(Value);
}
//
//
void loop()
{
do {
if (digitalRead(Button) == LOW) //if button is pressed
{
Button_still_pressed = HIGH;
Button_count++;
//
if (Button_count < 20)
{
Value++;
lcd.setCursor(0,0);
lcd.print(Value);
lcd.print(" ");
}
else
{
if (Button_count < 100)
{
Value = Value + 10;
lcd.setCursor(0,0);
lcd.print(Value);
lcd.print(" ");
}
else
{
Button_count = 100;
Value = Value + 100;
lcd.setCursor(0,0);
lcd.print(Value);
lcd.print(" ");
}
}
}
else
{
Button_still_pressed = LOW;
Button_count = 0;
}
delay(300); //A very simple way to handle button bouncing, but need to adjust the delay time to optimize the result.
}while (Button_still_pressed);
//
}

Step 8: Enjoy It :)

There are many other ticks to manipulate a push button.

Wish you all share yours. :)

<p>good work dude ! its exactly what i was looking for !</p>

About This Instructable

2,047views

27favorites

License:

More by uvvvvw:CNC Machine Carved Pumpkin Auto Guiding Sky Tracker CNC Egg Shell Bas-relief Engraving  
Add instructable to: