Hey all,
So i'm very new to learning the Arduino coding but i have managed to build a simple code from some tutorials that after a button press input 5 lights come on in sequence, each delayed 1 second to the previous.
Now that is done what i want the code to do is at a random time interval of between 4 to 7 seconds turn all the lights out at once. Is this possible within the code?
I've been using the Simulator for Arduino software which shows the light on sequence is working as i want. And if i turn the switch input off again the leds go out as expected, i just want it to do this automatically.
the code to date is as follows but this has no random time off functions within it
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin1 = 9; // the number of the LED pin
const int ledPin2 = 10; // the number of the LED pin
const int ledPin3 = 11; // the number of the LED pin
const int ledPin4 = 12; // the number of the LED pin
const int ledPin5 = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
delay (1000);
digitalWrite(ledPin2, HIGH);
delay (1000);
digitalWrite(ledPin3, HIGH);
delay (1000);
digitalWrite(ledPin4, HIGH);
delay (1000);
digitalWrite(ledPin5, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, LOW);
}
}