Home cutting scanning  circuits programs academy progress final project

With task 5 the button had to be pressed three times before there was to be any action. This would mean a counter of some sort with another one of those funny double character comparisons.

#define LED 13            //pin for LED

#define BUTTON 7       //pin for Button


int val = 0;                //set the button val

int state = 0;            //set count for swich at 0


void setup(){

  pinMode(LED, OUTPUT);             //set direction out

  pinMode(BUTTON, INPUT);        //set direction in

}


void loop(){

  digitalWrite(LED, LOW);

  val = digitalRead(BUTTON);       //read the button

  if (val == HIGH){

    state = state + 1;                 //add one to memory

    delay(500);

  } else {

    digitalWrite(LED, LOW);     //keep light off until the                                              //count is right

  }

  if (state == 3){

    digitalWrite(LED, HIGH);       // one third press turn                                                //LED on

    delay(1000);

    digitalWrite(LED, LOW);    // after a second turn it off

    state = 0;

  }

}


The setup is the same but in the loop I start by making sure the LED is off - this may be unnecessary but it lets me be sure of my starting point.

As soon as the button is pressed  the program counts it and then delays for half a second to basically debounce the switch (this used to be done with capacitors and you could purchase a debounced switch)

Assuming the button is not pressed the LED is kept low - this again may be unnecessary but again lets me know where I am.

Once the button has been pressed three times the program turns on the lights but only for a second after which it turns the lights off and resets the counter back to 0 again.