# Digital Hourglass: reverse the lighting sequence by turning them off

Please kindly look at the Project 8 in the Official Arduino Kit. The link below will take you to the TinkerCad Circuits where you can copy and tinker the circuit and sketch. What I would like to do is the turn off the LEDs (pin 2-7) in the reversal order (from LED 7 to 2). Please see the custom code from line 54 to 58 that I made. The problem is it does not turn off the LEDs in the reversal order. What should I do? Did I put it in the wrong place? Please advise.

Please inline your circuit and code, see the sticky topics at the top of the forum.

In general you have to reverse the loop counting the LED number once up and later down again.

``````/*
Arduino Starter Kit example
Project 8 - Digital Hourglass

This sketch is written to accompany Project 8 in the Arduino Starter Kit

Parts required:
- 10 kilohm resistor
- six 220 ohm resistors
- six LEDs
- tilt switch

created 13 Sep 2012
by Scott Fitzgerald

http://www.arduino.cc/starterKit

This example code is part of the public domain.
*/

// named constant for the switch pin
const int switchPin = 8;

unsigned long previousTime = 0; // store the last time an LED was updated
int switchState = 0; // the current switch state
int prevSwitchState = 0; // the previous switch state
int led = 2; // a variable to refer to the LEDs

long interval = 200; // interval at which to light the next LED

void setup() {
// set the LED pins as outputs
for (int x = 1; x < 8; x++) {
pinMode(x, OUTPUT);
}
// set the tilt switch pin as input
pinMode(switchPin, INPUT);
}

void loop() {
// store the time since the Arduino started running in a variable
unsigned long currentTime = millis();

// compare the current time to the previous time an LED turned on
// if it is greater than your interval, run the if statement
if (currentTime - previousTime > interval) {
// save the current time as the previous time you changed an LED
previousTime = currentTime;
digitalWrite(led, HIGH);
led++;
}

//reverse the sequence by turning off the LED's from 7 to 2
if (led == 7) {
for (int y = 7; y > 1; --y) {
digitalWrite(y, LOW);
}
}

if (switchState != prevSwitchState) {
for (int x = 1; x < 8; x++) {
digitalWrite(x, LOW);
}

// reset the LED variable to the first one
led = 2;

//reset the timer
previousTime = currentTime;
}
// set the previous switch state to the current state
prevSwitchState = switchState;
}
``````

Restart from the very first (example?) version of your code, with the millis() etc. turning on the LEDs one by one.

Add a state variable indicating up, down, and optionally idle. Depending on the state of that variable change the digitalWrite to HIGH or LOW for on or off, and the led++; to led--; for counting up or down. Also change the state to idle when the upper or lower bound of the LED numbers is reached.

Add code for handling the buttons and changing the state and led variables.

Thank you for the quick reply. The sketch has exactly what you suggested. Please take a look. I'm sure you will see my mistake. Thank you for your time.

“ The problem is it does not turn off the LEDs in the reversal order. ”

How long does this take ?

`````` if (led == 7) {
for (int y = 7; y > 1; --y) {
digitalWrite(y, LOW);
}
}
``````

Probably 5,000 milliseconds

5,000ms is 5 seconds.

Let’s assume each LED is turned off in 10µs, so <60µs for 6 LEDS, <0.00006 seconds.

If we are understanding you correctly, eyes will see the LEDs ‘all’ turn off ‘instantly’.

I didn't know that =)

5,000,000ms is 5,000 seconds ~ 84 minutes.

Hi Larry,

I'm sorry for the confusion. It does not matter how long it takes. Five seconds in real time. I just to see if the logic in the sketch is correct. Thank you for your valuable time.

I just to see if the logic in the sketch is correct.

if (led == 7)
{
for (int y = 7; y > 1; --y)
{
digitalWrite(y, LOW);
}
}

The above code will turn off the LEDs in the 7 to 2 direction.

If you want to see the LEDs go out one at a time you will need to slow things down.

BTW
Your code, as it is in post #2, operates on pin 1 (Serial TX pin) we almost never do this.

Edit
Updated sketch

sketch_apr30a.ino (1.28 KB)

Hi Larry,

Thank you so much!

Hi Larry,

All LEDs flash up and down except the LED at pin 7. It does not turn on. I measured the voltage across the LED, resistor, and even the yellow output wire (see attached image). Nothing is live at that particular junction pin 7.

Then I changed number 7 to 8 in this line: if (led == 7) ==> if (led == 8) then it works. But it does not make sense. Please shed some light in this logic. Many thanks.

You have no idea what your code does? Never tried to understand it?

``````//Version 1.00

#define PUSHED               HIGH

bool FlagUp                = true;
bool FlagDown              = false;

byte switchState           = 0;
byte prevSwitchState       = 0;
byte led                   = 2;

const byte switchPin       = 8;

unsigned long previousTime;
unsigned long interval     = 200;

void setup()
{
for (int x = 2; x <= 7; x++)
{
pinMode(x, OUTPUT);
}

pinMode(switchPin, INPUT_PULLUP);

} //END of setup()

void loop()
{
unsigned long currentTime = millis();

//***********************************
if (FlagUp == true && currentTime - previousTime > interval)
{
previousTime = currentTime;

digitalWrite(led, HIGH);

led++;

if (led == 8)
{
previousTime = currentTime;

FlagUp = false;

FlagDown = true;
}
}

//***********************************
if (FlagDown == true && currentTime - previousTime > interval)
{
previousTime = currentTime;

led--;

digitalWrite(led, LOW);

if (led == 2)
{
FlagDown = false;
}
}

//***********************************

if (switchState != prevSwitchState)
{
prevSwitchState = switchState;

if (switchState == PUSHED)
{
FlagUp = true;

previousTime = currentTime;
}
}

} //END of loop()
``````

DrDiettrich:
You have no idea what your code does? Never tried to understand it?

Hi DrDietrich,

I understand it. Just don't know why led == 8 works when there's no initialized pin for 8. Do you know why?

Did you try the code in post #15 ?

The test for 8 reveals an invalid LED number and consequently terminates the loop. A test for the highest valid LED number were okay as well, but only after that LED has been served.

Hi DrDietrich. I like you screen name. Sir, I understand the code logic. However I don't why the code line with "
led == 8 " works when the set up initializes the pinMode only to 7. It does not make sense.