Simple Pushbutton on off Project 2

 

 Simple Pushbutton on off Project 2


Arduino Uno - 01
Breadboard - 01
220 Ohm Resistors - 02
LED - 01
Push Button Switch - 01
Jumper wires

                                       Arduino Sketch

int inPin = 2;                                    // pin where we connect the button
int outPin = 3;                                 // pin where we connect the LED

int state = LOW;                             //current state of the output pin
int reading;                                     //current reading from the input pin
int previous = HIGH;                     //previous reading from the input pin

long time = 0;                                  // the last time the output pin was toggled
long debounce = 1000;                   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(inPin, INPUT);             // define button as an input
  pinMode(outPin, OUTPUT);      // define LED as an output
}
void loop()
{
  reading = digitalRead(inPin);                                 // Button reading
    if (reading == HIGH && previous == LOW && millis()- time > debounce) {   // wait a sec for                                                                                                                            the hardware to stabilize  
    if (state == HIGH)                                              // *if button is pressed 
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }
  digitalWrite(outPin, state);                                 // *and turn on the LED
  previous = reading;
}

Comments