Go Down

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

#### ymtees

##### Jan 03, 2013, 04:37 pm
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!

#### John_Smith

#1
##### Jan 03, 2013, 04:55 pm
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.
45 years of editing projects with a knife and soldering iron, then I found Arduino !

#### ymtees

#2
##### Jan 03, 2013, 04:59 pm
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 timeint switchState =0;    // define time for comparison.int prevSwitchState =0;int led = 2; // variable created to know which pin to start fromlong interval = 1000; // interval between each LED turning onvoid 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