For this task I was to turn on a light normally (with a push button) and then switch it off again.
#define LED 13 //pin for LED
#define BUTTON 7 // pin for switch
int val = 0; // setting val as a memory
int old_val = 0; // setting another memory
int state = 0; //state of the LED
void setup(){
pinMode(LED, OUTPUT); // setting the direction for LED
pinMode (BUTTON, INPUT); //setting the direction for BUTTON
}
void loop(){
val = digitalRead(BUTTON); //read the button
if ((val == HIGH) && (old_val == LOW)){
state =1 - state;
}
old_val = val; // val is now old so lets store it
if (state == 1) {
digitalWrite(LED, HIGH); // turn on LED
} else {
digitalWrite(LED, LOW);
}
}
This time not only do I have a double == to get to grips with but also a &&, but as before this is just a way of bringing in an and function with a condition. The basic layout of the program is relatively easy to follow