Simple Pushbutton on off Project 3
Arduino Uno - 01
Breadboard - 01
220 Ohm Resistors - 02
LED - 01
Push Button Switch - 01
Jumper wires
                             Arduino Sketch

const int button = 2;                                   // pin where we connect the button
const int led =3;                                         // pin where we connect the LED
int ledflag=0;                                             // LED status flag
void setup() {
  pinMode(button,INPUT);                     // define button as an input 
  pinMode(led,OUTPUT);                      // define LED as an output
  digitalWrite(led,HIGH);                      // turn output ON just in case
}
void loop() {
  if (digitalRead(button)==HIGH){                         // if button is pressed
    if (ledflag==0) {                                                     // and the status flag is LOW
      ledflag=1;                                                            // make status flag HIGH
      digitalWrite(led,HIGH);                                   // and turn on the LED
      }                                                                          // 
    else {                                                                     // otherwise...
      ledflag=0;                                                          // make status flag LOW
      digitalWrite(led,LOW);                                   // and turn off the LED
    }
  delay(2000);                                                          // wait a sec for the 
  }                                                                             // hardware to stabilize
}                                                                               // begin again 

Comments