Hi all!
I've been moving through the arduino projects slowly but surely, but I am now stuck on the last section of the 08 extension asking to reverse the lights upon a shake "Unlike an hourglass filled with sand, the lights go either up or down dependingon the orientation of the switch. Can you figure out how you can use the switchState variable to indicate what direction the lights should go?" I've not been able to get the arduino to do anything except turn all the lights on at the ends. Code is below. Any help is appreciated. Thank you!
unsigned long PreviousTime=0;//Using the unsigned long variable as expleined before
int InterruptorState=0;//Initialtating variables
int PreviousStateInterruptor=0;
int Led=2;//Variable used to count which LED will be the next on lighting
long TimeIntervalocadaLed=1000;//Every 1 second(s) a LED will light
int c=0;
void setup() {
for(int x=2;x<8;x++){
pinMode(x,OUTPUT); //Stablishing all the LEDs as outputs
}
pinMode(PinInterruptor,INPUT); //Stablishing the switch as an input
}
void loop() {
unsigned long ActualTime=millis();//Knowing how long the program has been working
if(ActualTime-PreviousTime>TimeIntervalocadaLed){ //Veryfing if has transcurred the required time to switch on another LED
PreviousTime=ActualTime;
digitalWrite(Led,HIGH);//Lighting a new LED
Led++;//Preparing to light the next one
if(Led==7){ //In case that there is just one LED switch off advise by blinking all LEDs when the time is about to finish(2.5 seconds before)
delay(2500);
c=0;
while(c<5){
for(int x=2;x<8;x++){
digitalWrite(x,LOW);
}
delay(500);
for(int x=2;x<8;x++){
digitalWrite(x,HIGH);
}
delay(500);
c++;
}
}
}
InterruptorState=digitalRead(PinInterruptor); //Read sensor state
if(InterruptorState !=PreviousStateInterruptor){ //Puting all variables at zero if the state has changed
for(int x=2;x<8;x++){
digitalWrite(x,LOW);
}
Led=2;
PreviousTime=ActualTime;
}
PreviousStateInterruptor=InterruptorState; //Stablishing the actual state as the previous one
}
1 post - 1 participant