Pages: [1]   Go Down
Author Topic: Programming Hourglass to start once last LED is reached  (Read 489 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your reply, here's the code

Code:
// 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;
}
Logged

Pages: [1]   Go Up
Jump to: