Go Down

Topic: Programming Hourglass to start once last LED is reached (Read 556 times) previous topic - next topic

ymtees

Hello, this is my first post,

I'm successfully getting my 'hourglass' (made up of 6 LEDs and a tilt switch) to start again once the last LED has been reached, but I've had to fiddle with the programme and don't understand why.

The last led is on pin 7, but I call pin 9 for it to work...
  if(led == 9) {
    for(int x = 2; x < 8; x++){
    digitalWrite(x, LOW);
  }
 
  led = 2;
  previousTime = currentTime;
  }

I wonder if it is because I start on pin 2 and there is a secret, hidden, substraction of 2 going on...

It'd be great to understand it rather than just accepting that it works...

Thanks!

Boffin1

post your whole code, ( with declarations and set up  ),    but select the code and click on the # button to compress it into a code box, before sending.
With my mobile phone I can call people and talk to them -  how smart can you get ?

ymtees

Thanks for your reply, here's the code

Code: [Select]
// Every ten seconds, a light goes on.
//After all the lights have been turned on, it starts again.
//if the switch is tilted, it starts again.

const int switchPin = 8;
unsigned long previousTime=0; // variable to hold the time
int switchState =0;    // define time for comparison.
int prevSwitchState =0;

int led = 2; // variable created to know which pin to start from

long interval = 1000; // interval between each LED turning on



void setup (){
pinMode (switchPin, INPUT);

for (int x=2; x<8; x++){
  pinMode(x,OUTPUT);
  }
}

void loop () {
  unsigned long currentTime = millis();
 
  if(currentTime - previousTime > interval) {
  previousTime = currentTime;
 
  digitalWrite(led,HIGH);
  led++;
 
   // When the LED on pin 7 is turned on, the whole process starts with all lEDs turned off
  if(led == 9) {
    for(int x = 2; x < 8; x++){
    digitalWrite(x, LOW);
  }
 
  led = 2;
  previousTime = currentTime;
  }

}
 
  switchState = digitalRead(switchPin);
if(switchState != prevSwitchState){
  for(int x =2; x<8; x++){
    digitalWrite(x, LOW);
  }
 
  led = 2;
  previousTime = currentTime;
}

prevSwitchState = switchState;
}

Go Up