Builtin Example 10/p08-DigitalHourglass Starter Kit Project -- out-of-bounds error

The Starter Kit Digital Hourglass Project has an out-of-bounds error when it starts turning on LEDs above 7.

Here's a speeded-up Wokwi simulation:

image https://wokwi.com/projects/422992836439284737

// code from https://github.com/arduino/arduino-examples/blob/main/examples/10.StarterKit_BasicKit/p08_DigitalHourglass/p08_DigitalHourglass.ino
// sim: https://wokwi.com/projects/422992836439284737
// for issue https://github.com/arduino/arduino-examples/issues/83
// about turning off out-of-bounds LEDs above pin 7 
/*
  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

  https://store.arduino.cc/genuino-starter-kit

  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

// 600000 = 10 minutes in milliseconds
long interval = 600000/100;  // interval at which to light the next LED

void setup() {
  // set the LED pins as outputs
  for (int x = 2; 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 last time you changed an LED
    previousTime = currentTime;
    // Turn the LED on
    digitalWrite(led, HIGH);
    // increment the led variable
    // in 10 minutes the next LED will light up
    led++;

    if (led == 7) {
      // the hour is up
    }
  }

  // read the switch value
  switchState = digitalRead(switchPin);

  // if the switch has changed
  if (switchState != prevSwitchState) {
    // turn all the LEDs low
    for (int x = 2; 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;
}

Note that this speeds it up to 6-second steps, 36sec total. After 42 secs, when led==8, the digitalWrite(led,HIGH) pulls up the input, overriding the external pulldown and inhibiting button signal changes.

I think this forum post experienced the problem:

and this one:

I opened an issue at:

...with a suggestion that they limit the bounds of the led-energizing circuit with something like:

if (currentTime - previousTime > interval && led < 8) {

In this other forum post, I think the issue is that the stock sketch is incredibly boring, and that the first LED doesn't even turn on for 10 minutes (60000ms):

In the simulation linked above, I wired up an additional 6 leds (on a DIP) in active-LOW format to show that something happens.

1 Like

They closed the issue as not planned, because the book would need updating.

... and made some changes to the comment:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.