Post

Homework 8: Digital Hourglass

Homework 9 project

This project went by pretty quickly, it was the same follow along with the book as usual, though, it was interesting to work with the new tilt switch sensor.

The Circuit

Circuit

Parts List

NameQuantityComponent
U11Arduino Uno R3
R1110 kΩ Resistor
TILT11Tilt Sensor
R2, R3, R4, R5, R6, R76220 Ω Resistor
D1, D2, D3, D4, D5, D66Red LED

Parts List CSV

Schematics

Schematics Schematics pdf

Building the Circuit

Assembling the circuit was simple since there very few components and wires, however, one issue I did run into was when the video suggested to trim the battery snap insulation, which I did need to do, however, I did end up slightly mangling my cables

Mangled cables

Luckily, I was able to still plug them in and uploading the sketch still resulted in a working project as demonstrated below ( wait time set to 500ms per light using example sketch)

Digital Hourglass

Programming

After confirming that the circuit was working, I began writing the code for the project, which was straight and understandable, though one unexpected thing I learnt was that the Arduino can store how long it’s been running in just raw numbers, around 50 days, which is a lot more than I thought it could do for some reason.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// named constant for the switch pin
const int switchPin = 8;

unsigned long previousTime = 0;
int switchState = 0;
int prevSwitchState = 0;
int led = 2;

long interval = 500;  

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() {
  unsigned long currentTime = millis(); // time since Arduino started running

  if (currentTime - previousTime > interval) {
    previousTime = currentTime;
    // Turn the LED on
    digitalWrite(led, HIGH);
    led++;
    if(led == 7){
      // do something else
    }
  }

  switchState = digitalRead(switchPin);

  // if 'hourglass' reset
  if (switchState != prevSwitchState) {
    for (int x = 2; x < 8; x++) {
      digitalWrite(x, LOW);
    }

    // reset LED var, and timer
    led = 2;
    previousTime = currentTime;
  }
  prevSwitchState = switchState;
}
This post is licensed under CC BY 4.0 by the author.