Built-in project · Arduino Uno R3

Tilt hourglass on an Arduino Uno

Six LEDs light one by one on a millis() timer, and tipping the tilt switch on pin 8 clears them and starts again. The whole build, an Arduino Uno and 14 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner Runs in your browser. Free, and no account needed.

Tilt hourglass · Arduino Uno R3live0.000 s 0.00x
Hold the button
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Tilt hourglass: six LEDs light one at a time as the time runs, and turning
// the hourglass over starts it again.
//
//   pins 2 to 7   the six LEDs, each through 220 ohm to GND
//   pin 8         the tilt switch: one leg to 5V, the other to pin 8 and
//                 through a 10k resistor to GND
//
// The sketch never stops to wait. It notes the time of the last LED with
// millis() and checks on every pass whether INTERVAL has gone by since, so
// it can keep watching the tilt switch the whole time. delay() would leave it
// deaf for the whole interval.
//
// INTERVAL is two seconds here so you can watch it run. Set it to 600000 for
// ten minutes a light, an hour for all six.

const unsigned long INTERVAL = 2000;
const int FIRST_LED = 2;
const int LED_COUNT = 6;
const int TILT = 8;

unsigned long lastTime = 0;
int lit = 0;
int lastTilt = -1;

void setup() {
  for (int i = 0; i < LED_COUNT; i++) pinMode(FIRST_LED + i, OUTPUT);
  pinMode(TILT, INPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: tilt hourglass");
}

void loop() {
  unsigned long now = millis();
  if (lit < LED_COUNT && now - lastTime >= INTERVAL) {
    lastTime = now;
    digitalWrite(FIRST_LED + lit, HIGH);
    lit++;
    Serial.print("lights on: ");
    Serial.println(lit);
    if (lit == LED_COUNT) Serial.println("time is up");
  }

  int tilt = digitalRead(TILT);
  if (lastTilt >= 0 && tilt != lastTilt) {
    for (int i = 0; i < LED_COUNT; i++) digitalWrite(FIRST_LED + i, LOW);
    lit = 0;
    lastTime = now;
    Serial.println("turned over: starting again");
  }
  lastTilt = tilt;
}

Parts list

16 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

15 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Ground: Arduino Uno R3 pin GND; Resistor, 10k Ω pin 1; LED, yellow (1) pin C; LED, yellow (2) pin C; LED, yellow (3) pin C; LED, yellow (4) pin C; LED, yellow (5) pin C; LED, yellow (6) pin C
  • Arduino Uno R3 pin 8; Tilt switch pin 2; Resistor, 10k Ω pin 2
  • Arduino Uno R3 pin 7; Resistor, 220 Ω (6) pin 1
  • Arduino Uno R3 pin 6; Resistor, 220 Ω (5) pin 1
  • Arduino Uno R3 pin 5; Resistor, 220 Ω (4) pin 1
  • Arduino Uno R3 pin 4; Resistor, 220 Ω (3) pin 1
  • Arduino Uno R3 pin 3; Resistor, 220 Ω (2) pin 1
  • Arduino Uno R3 pin 2; Resistor, 220 Ω (1) pin 1
  • Arduino Uno R3 pin 5V; Tilt switch pin 1
  • Resistor, 220 Ω (1) pin 2; LED, yellow (1) pin A
  • Resistor, 220 Ω (2) pin 2; LED, yellow (2) pin A
  • Resistor, 220 Ω (3) pin 2; LED, yellow (3) pin A
  • Resistor, 220 Ω (4) pin 2; LED, yellow (4) pin A
  • Resistor, 220 Ω (5) pin 2; LED, yellow (5) pin A
  • Resistor, 220 Ω (6) pin 2; LED, yellow (6) pin A

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.