I am not sure if Fabio881's post solved the problem or not to the issue.
I have approached the direction of the flashing lights in a different way. I stuck to the code written in the book for this project as closely as possible and only made changes to the if(led==7){}statement.
Here is the original code from the book where nothing happens after all the LEDs are lit:
const int switchPin=8;
unsigned long previousTime=0;
int switchState=0;
int previous SwitchState=0;
int led=2;
long interval=600000; //in my code I changed the interval to 10000 to speed everything up
void setup(){
for(int x=2;x<8;x++){
pinMode(x,OUTPUT);
}
pinMode(switchPin,INPUT);
}
void loop(){
unsigned long currentTime=millis();
if(currentTime-previousTime>interval){
previousTime=currentTime;
digitalWrite(led,HIGH);
led++;
if(led==7){ //this is the area we need to modify. The book has nothing in between the brackets so that you can program what will happen once all 6 LEDs are lit
}
}
switchState=digitalRead(switchPin);
if(switchState!=previousSwitchState){
for(int x=2;x<8;x++){
digitalWrite(x,LOW);
}
led=2;
previousTime=CurrentTime;
}
previousSwitchState=switchState;
}
There is a small problem with the code as is straight from the book and has to do with line 18 & 19 of the code:
led++;
if(led==7){
if the code is left as is once the 5th LED lights up the the led variable will increase to 7 and whatever you put between the {} of the if(led==7) statement will occur before all LEDs are lit. So to correct this we change the == signs to > sign and then things will work how we want them. Now here is the code I placed within the if(led>7)statement:
if(led>7){
// these statements will cause the LEDs to flash on and off from low to high than high to low
for(int x=2;x<8;x++){
digitalWrite(x,HIGH); //will cause LEDs to flash on for 100millseconds each in succesion from 2-7
delay(100);
digitalWrite(x,LOW);
}
for(int x=7;x>1;x--){
digitalWrite(x,HIGH); //will cause LEDs to flash on for 100millseconds each in succesion from 7-2
delay(100);
digitalWrite(x,LOW);
}
}
Now once the 6th LED comes on they will flash from the first to last LED in succession then Flash from the last LED to the first. However, as the code is, after the LEDs do one cycle of the up and down flashing they will pause for an 1 interval before flashing again. This happens every time. That is because the entire if(led>7){ statement is within the if(currentTime-previousTime>interval){ statement, and therefore we have to wait one full interval for the process to continue.
if(currentTime-previousTime>interval){
previousTime=currentTime;
digitalWrite(led,HIGH);
led++;
if(led>7){
Fellow Arduino Community Member BadMrFresli discovered that if we separate our two if statements by putting a } after the led++; there will no longer be a pause and the lights will flash as intended. Here is what this section of code should look to work properly:
if(currentTime-previousTime>interval){
previousTime=currentTime;
digitalWrite(led,HIGH);
led++;
}
if(led>7){
// these statements will cause the LEDs to flash on and off from low to high than high to low
for(int x=2;x<8;x++){
digitalWrite(x,HIGH); //will cause LEDs to flash on for 100millseconds each in succesion from 2-7
delay(100);
digitalWrite(x,LOW);
}
for(int x=7;x>1;x--){
digitalWrite(x,HIGH); //will cause LEDs to flash on for 100millseconds each in succesion from 7-2
delay(100);
digitalWrite(x,LOW);
}
}
by separating the if(LED>7) statement from the if(currentTime-previousTime>interval) statement it is no longer dependent on the program to go through an interval. It is now only dependent on whether or not the variable led is greater than 7.
I hope this helps
If there are any further questions please post questions to this forum