/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock

« back to all changes in this revision

Viewing changes to src/propeller-clock.ino

  • Committer: edam
  • Date: 2012-02-02 01:19:19 UTC
  • Revision ID: edam@waxworlds.org-20120202011919-rcu4pt8mq8pieujs
modified schematic and updated notes

Show diffs side-by-side

added added

removed removed

1
1
/*
2
 
 * propeller-clock.pde
 
2
 * propeller-clock.ino
3
3
 *
4
 
 * Copyright (C) 2011 Tim Marston <edam@waxworlds.org>
 
4
 * Copyright (C) 2011 Tim Marston <tim@ed.am> and Dan Marston.
5
5
 *
6
6
 * This file is part of propeller-clock (hereafter referred to as "this
7
 
 * program"). See http://ed.am/software/arduino/propeller-clock for more
 
7
 * program"). See http://ed.am/dev/software/arduino/propeller-clock for more
8
8
 * information.
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
35
35
   13 is at the outside.
36
36
 
37
37
 * if a longer hand (and a larger clock face) is desired, pin 4 can be
38
 
   used to indirectly drive (via a MOSFET) multiple LEDs which turn on
39
 
   and off in unison in the centre of the clock.
 
38
   used to indirectly drive a transistor which in turn drives several
 
39
   LEDs that turn on anf off in unison in the centre of the clock.
40
40
 
41
41
 * a button should be attached to pin 3 that grounds it when pressed.
42
42
 
 
43
 * A DS1307 remote clock is connected via I2C on analog pins 4 and 5.
 
44
 
43
45
Implementation details:
44
46
 
45
 
 * for a schematic, see project/propeller-clock.sch.
 
47
 * for a schematic, see ../project/propeller-clock.sch.
46
48
 
47
49
 * the timing of the drawing of the clock face is recalculated with
48
50
   every rotation of the propeller.
67
69
    - pressing the button increments the field currently being set
68
70
    - pressing and holding the button for a second cycles through the
69
71
      fields that can be set
70
 
    - press and holding the button for 5 seconds to finish
 
72
    - pressing and holding the button for 5 seconds sets the time and
 
73
      exits "time set" mode
71
74
 
72
75
******************************************************************************/
73
76
 
74
77
 
75
78
#include <Bounce.h>
 
79
#include <DS1307.h>
 
80
#include <Wire.h>
76
81
 
77
82
//_____________________________________________________________________________
78
83
//                                                                         data
155
160
}
156
161
 
157
162
 
 
163
// turn an led on/off
 
164
void ledOn( int num, bool on )
 
165
{
 
166
        if( num < 0 || num > 9 ) return;
 
167
 
 
168
        // convert to pin no.
 
169
        num += 4;
 
170
 
 
171
        // pin 4 needs to be inverted (it's driving a PNP)
 
172
        if( num == 4 ) on = !on;
 
173
 
 
174
        digitalWrite( num, on? HIGH : LOW );
 
175
}
 
176
 
 
177
 
158
178
// draw a segment for the test display
159
179
void drawNextSegment_test( bool reset )
160
180
{
164
184
        segment++;
165
185
 
166
186
        // turn on inside and outside LEDs
167
 
        digitalWrite( 4, HIGH );
168
 
        digitalWrite( 13, HIGH );
 
187
        ledOn( 0, true );
 
188
        ledOn( 9, true );
169
189
 
170
190
        // display segment number in binary across in the inside LEDs,
171
191
        // with the LED on pin 12 showing the least-significant bit
172
192
        for( int a = 0; a < 8; a++ )
173
 
                digitalWrite( 12 - a, ( ( segment >> a ) & 1 )? HIGH : LOW );
 
193
                ledOn( 8 - a, ( segment >> a ) & 1 );
174
194
}
175
195
 
176
196
 
177
197
// draw a segment for the time display
178
198
void drawNextSegment_time( bool reset )
179
199
{
180
 
        static unsigned int second = 0;
181
 
        static unsigned int segment = 0;
 
200
        static int second = 0;
 
201
        static int segment = 0;
182
202
 
183
203
        // handle display reset
184
204
        if( reset ) {
189
209
        // what needs to be drawn?
190
210
        bool draw_tick = !segment && second % 5 == 0;
191
211
        bool draw_second = !segment && second == time_seconds;
192
 
        bool draw_minute = !segment && second == time_minute;
193
 
        bool draw_hour = !segment && second == time_hour;
 
212
        bool draw_minute = !segment && second == time_minutes;
 
213
        bool draw_hour = !segment && second == time_hours;
194
214
 
195
215
        // set the LEDs
196
 
        digitalWrite( 13, HIGH );
197
 
        digitalWrite( 12, draw_tick || draw_minute );
198
 
        for( int a = 10; a <= 11; a++ )
199
 
                digitalWrite( a, draw_minute || draw_second );
200
 
        for( int a = 4; a <= 9; a++ )
201
 
                digitalWrite( 10, draw_minute | draw_second || draw_hour );
 
216
        ledOn( 9, true );
 
217
        ledOn( 8, draw_tick || draw_minute );
 
218
        for( int a = 6; a <= 7; a++ )
 
219
                ledOn( a, draw_minute || draw_second );
 
220
        for( int a = 0; a <= 5; a++ )
 
221
                ledOn( a, draw_minute || draw_second || draw_hour );
202
222
 
203
223
        // inc position
204
224
        if( ++segment >= NUM_SECOND_SEGMENTS ) {
304
324
        // set up mode-switch button on pin 3
305
325
        pinMode( 3, INPUT );
306
326
 
 
327
        // get the time from the real-time clock
 
328
        int rtc_data[ 7 ];
 
329
        RTC.get( rtc_data, true );
 
330
        time_hours = rtc_data[ DS1307_HR ];
 
331
        time_minutes = rtc_data[ DS1307_MIN ];
 
332
        time_seconds = rtc_data[ DS1307_SEC ];
 
333
 
307
334
        // serial comms
308
335
        Serial.begin( 9600 );
309
336
}