The final countdown
In Flanders (Belgium) sixth-formers yearly celebrate the last 100 days. This tradition celebrates that high school is almost over, and that freedom is on its way. Students will usually dress up in costumes and party all day and night long. Most school also organize special activities like a breakfast or allow students to decorate the school buildings.
This year I was one of the revelers and since the event is the start of a countdown, I decided to make a big countdown timer to count down to our graduation. # Hardware At first I considered using large 7 segment display, but after some consideration I went with a large clock face that counts down from ‘100 days’ to ‘we survived!’ (the theme of this year)
The clock face and hand are laser cuted out of 3 mm plywood, and all electronics and servo’s are immediately attached to it. The board consists out of an ATmega8 running the Arduino boot loader and a RTC (ds1307).
Last year I noticed a network switch above the ceiling tiles, so I assumed there must power there. Popping my head through the ceiling confirmed that, so I just went ahead and installed the thing.
It's easier to ask forgiveness than it is to get permission. — Grace Hopper —
Software
The software is very simple. It reads out the RTC, calculates the remaining number of days and then maps those value’s to a position for the servo. The code is at the end of this post.
1 // Date and time functions using a DS1307 RTC connected via I2C and Wire lib
2
3 /* The following code is licensed to the public domain.
4 You should however probably not use it,
5 since it was written in a hurry and is quite crappy */
6
7 #include <Wire.h>
8 #include "RTClib.h"
9 #include <servo.h>
10
11 const int ledPin = 13; // the number of the LED pin
12 int ledState = 0;
13
14 int servoPin = 9;
15 int prevClockhandPosition = -1;
16 int servoMin = 2;
17 int servoMax = 163;
18
19 int daysInMonth[12] = {
20 21, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
21
22 int targetDay = 25;
23 int targetMonth = 6;
24 int targetYear = 2013;
25
26 int setRTC = 0;
27
28 RTC_DS1307 RTC;
29 Servo clockhand;
30
31 void setup () {
32 Serial.begin(9600);
33 Wire.begin();
34 RTC.begin();
35 // pinMode(16, OUTPUT);
36 // pinMode(17, OUTPUT);
37 pinMode(ledPin, OUTPUT);
38 // digitalWrite(16, LOW);
39 // digitalWrite(17, HIGH);
40
41 if (! RTC.isrunning() || setRTC == 1) {
42 Serial.println("RTC is NOT running!");
43 // following line sets the RTC to the date & time this sketch was compiled
44 RTC.adjust(DateTime(__DATE__, __TIME__));
45 }
46 }
47
48 void loop () {
49 DateTime now = RTC.now();
50
51 int daysToTarget = 0;
52
53 if (now.month() <= targetMonth){
54 daysToTarget = targetDay - now.day();
55 for (int i = now.month(); i < targetMonth; i++){
56 // Serial.print("i= ");
57 // Serial.print(i);
58 // Serial.print(" DaysInMonth= ");
59 // Serial.println(daysInMonth[i-1]);
60 daysToTarget += daysInMonth[i-1];
61 }
62 }
63
64 Serial.print("Dagen tot proclamatie: ");
65 Serial.println(daysToTarget);
66
67 int clockhandPosition = map(daysToTarget, 0, 109, servoMin, servoMax);
68 clockhandPosition = min(clockhandPosition, servoMax);
69 clockhandPosition = max(clockhandPosition, servoMin);
70
71 if (clockhandPosition != prevClockhandPosition){
72 clockhand.attach(servoPin);
73 clockhand.write(80);
74 delay(1000);
75 clockhand.write(clockhandPosition);
76 delay(5000);
77 clockhand.detach();
78 prevClockhandPosition = clockhandPosition;
79 }
80
81 Serial.print("Servo hoek: ");
82 Serial.println(clockhandPosition);
83 Serial.println();
84
85 ledState = !ledState;
86 digitalWrite(ledPin, ledState);
87
88 delay(3000);
89 Serial.print(now.year(), DEC);
90 Serial.print('/');
91 Serial.print(now.month(), DEC);
92 Serial.print('/');
93 Serial.print(now.day(), DEC);
94 Serial.print(' ');
95 Serial.print(now.hour(), DEC);
96 Serial.print(':');
97 Serial.print(now.minute(), DEC);
98 Serial.print(':');
99 Serial.print(now.second(), DEC);
100 Serial.println();