//Define LEDs
const int red = 12;
const int yellow = 11;
const int green = 10;
const int Button = 2;
// define time delays
int time_delay = 300;
int LEDchecker = 1;
// creating a string for the LEDs
const int size = 3;
byte LEDstring[size] = {red,yellow,green};
void setup() {
// put your setup code here, to run once:
pinMode(Button, INPUT);
//must initialise pins as outputs and LEDs as off initally
initialise_pins();
turn_off_pins();
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(Button) == LOW){
disco_lights();
delay(time_delay); //having the delay outside of the function makes it easier to alter the delay
}
}
// function to initialise pins
void initialise_pins(){
for (int i = 0; i < size; i++){ //remember that indexing starts at 0
pinMode(LEDstring[i], OUTPUT);
} // to index a value in an array, you must use square brackets
// the round brackets are for functions only
}
// turn off pins
void turn_off_pins(){
for (int i = 0; i < size; i++){
digitalWrite(LEDstring[i], LOW);
}
}
// start blinking
void disco_lights(){
if (LEDchecker == 1){
digitalWrite(yellow, LOW);
digitalWrite(red, HIGH);
digitalWrite(green, HIGH);
LEDchecker = 2;
}
else{
digitalWrite(red, LOW);
digitalWrite(green, LOW);
digitalWrite(yellow, HIGH);
LEDchecker = 1;
}
}