Hi all,
Please forgive me for any stupid comments or questions, i got an Arduino UNO last night, some LED's resistors and switches, and have been playing around with it and watching lots of videos on youtube, so am very much a newbie first timer.
Ive got 3 amber leds, followed by a green led, lighting up and going out after a 1 second delay in sequence, ran by a switch that sets off the sequence. So it goes, amber1 on, 1 second delay, amber 1 off amber 2 on, one second delay amber2 off amber 3 on, one second delay, amber 3 off green on. Then ledred comes on and flashes 4 or 5 times.
Ive also got another seperate redled2, which i have managed to get a seperate switch controlling turning it on/off.
The problem i have is, i cant turn the redled2 on whilst the first sequence is running. Well i can, but until the sequence has finished the led doesnt light up until it has.
So basically, what i want to do is have the ability to turn on redled2 at my leisure whether the other sequence is running or not.
Im pretty certain its something to do with my code, and my IF statement placing, but could anyone be kind enough to have a look for me please?
I am able to turn the redled2 lights on BEFORE i press the button to start the sequence, and it stays on. But if i turn it off mid sequence, it doesnt turn off until the sequence has finished.
I feel i am nearly there and have been moving my IF's my }'s and trying else if's but no luck.
Here is my code.
---------------------------------------------------------------------------
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
const int button1pin = 2;
const int button2pin = 7;
const int ledgreen = 12;
const int ledamber3 = 11;
const int ledamber2 = 10;
const int ledamber1 = 9;
const int ledred = 8;
const int ledred2 = 6;
int buttonState = 0;
int buttonState2 = 0;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(ledgreen, OUTPUT);
pinMode(ledamber3, OUTPUT);
pinMode(ledamber2, OUTPUT);
pinMode(ledamber1, OUTPUT);
pinMode(ledred, OUTPUT);
pinMode(ledred2, OUTPUT);
pinMode(button1pin, INPUT);
pinMode(button2pin, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(button1pin);
buttonState2 = digitalRead(button2pin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
digitalWrite(ledamber1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(ledamber1, LOW); // turn the LED off by making the voltage LOW
digitalWrite(ledamber2, HIGH);
delay(1000);
digitalWrite(ledamber2, LOW);
digitalWrite(ledamber3, HIGH);
delay(1000); // wait for a second
digitalWrite(ledamber3, LOW);
digitalWrite(ledgreen, HIGH);
delay(1000);
digitalWrite(ledgreen, LOW);
digitalWrite(ledred, HIGH);
delay(500);
digitalWrite(ledred, LOW);
delay(500);
digitalWrite(ledred, HIGH);
delay(500);
digitalWrite(ledred, LOW);
delay(500);
digitalWrite(ledred, HIGH);
delay(500);
digitalWrite(ledred, LOW);
}
else if (buttonState2 == HIGH) {
digitalWrite(ledred2, HIGH);}
else
{
digitalWrite(ledamber1, LOW);
digitalWrite(ledamber2, LOW);
digitalWrite(ledamber3, LOW);
digitalWrite(ledgreen, LOW);
digitalWrite(ledred, LOW);
digitalWrite(ledred2, LOW);
}
}