/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-08 23:32:34 UTC
  • Revision ID: edam@waxworlds.org-20120208233234-4h5pzeetrz0vp91f
added phantom button press test and temporarily disabled pin 4 (that drives the PNP transistor)

Show diffs side-by-side

added added

removed removed

 
1
/* -*- mode: c++; compile-command: "BOARD=pro5v make"; -*- */
1
2
/*
2
3
 * propeller-clock.ino
3
4
 *
35
36
   13 is at the outside.
36
37
 
37
38
 * 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.
 
39
   used to indirectly drive a transistor which in turn drives several
 
40
   LEDs that turn on anf off in unison in the centre of the clock.
40
41
 
41
42
 * a button should be attached to pin 3 that grounds it when pressed.
42
43
 
 
44
 * A DS1307 remote clock is connected via I2C on analog pins 4 and 5.
 
45
 
43
46
Implementation details:
44
47
 
45
 
 * for a schematic, see project/propeller-clock.sch.
 
48
 * for a schematic, see ../project/propeller-clock.sch.
46
49
 
47
50
 * the timing of the drawing of the clock face is recalculated with
48
51
   every rotation of the propeller.
67
70
    - pressing the button increments the field currently being set
68
71
    - pressing and holding the button for a second cycles through the
69
72
      fields that can be set
70
 
    - press and holding the button for 5 seconds to finish
 
73
    - pressing and holding the button for 5 seconds sets the time and
 
74
      exits "time set" mode
71
75
 
72
76
******************************************************************************/
73
77
 
74
78
 
75
79
#include <Bounce.h>
 
80
#include <DS1307.h>
 
81
#include <Wire.h>
76
82
 
77
83
//_____________________________________________________________________________
78
84
//                                                                         data
97
103
static bool inc_draw_mode = false;
98
104
 
99
105
// a bounce-managed button
100
 
static Bounce button( 3, 5 );
 
106
static Bounce button( 3, 50 );
101
107
 
102
108
// the time
103
109
static int time_hours = 0;
155
161
}
156
162
 
157
163
 
 
164
// turn an led on/off
 
165
void ledOn( int num, bool on )
 
166
{
 
167
        if( num < 0 || num > 9 ) return;
 
168
 
 
169
        // convert to pin no.
 
170
        num += 4;
 
171
 
 
172
        // pin 4 needs to be inverted (it's driving a PNP)
 
173
        // NOTE: PIN 4 TEMPORARILY DISABLED
 
174
        if( num == 4 ) on = true; //!on
 
175
 
 
176
        digitalWrite( num, on? HIGH : LOW );
 
177
}
 
178
 
 
179
 
158
180
// draw a segment for the test display
159
181
void drawNextSegment_test( bool reset )
160
182
{
164
186
        segment++;
165
187
 
166
188
        // turn on inside and outside LEDs
167
 
        digitalWrite( 4, HIGH );
168
 
        digitalWrite( 13, HIGH );
 
189
        ledOn( 0, true );
 
190
        ledOn( 9, true );
169
191
 
170
192
        // display segment number in binary across in the inside LEDs,
171
193
        // with the LED on pin 12 showing the least-significant bit
172
194
        for( int a = 0; a < 8; a++ )
173
 
                digitalWrite( 12 - a, ( ( segment >> a ) & 1 )? HIGH : LOW );
 
195
                ledOn( 8 - a, ( segment >> a ) & 1 );
174
196
}
175
197
 
176
198
 
177
199
// draw a segment for the time display
178
200
void drawNextSegment_time( bool reset )
179
201
{
180
 
        static unsigned int second = 0;
181
 
        static unsigned int segment = 0;
 
202
        static int second = 0;
 
203
        static int segment = 0;
182
204
 
183
205
        // handle display reset
184
206
        if( reset ) {
193
215
        bool draw_hour = !segment && second == time_hours;
194
216
 
195
217
        // 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 );
 
218
        ledOn( 9, true );
 
219
        ledOn( 8, draw_tick || draw_minute );
 
220
        for( int a = 6; a <= 7; a++ )
 
221
                ledOn( a, draw_minute || draw_second );
 
222
        for( int a = 0; a <= 5; a++ )
 
223
                ledOn( a, draw_minute || draw_second || draw_hour );
202
224
 
203
225
        // inc position
204
226
        if( ++segment >= NUM_SECOND_SEGMENTS ) {
303
325
 
304
326
        // set up mode-switch button on pin 3
305
327
        pinMode( 3, INPUT );
 
328
        digitalWrite( 3, HIGH );
 
329
 
 
330
        // get the time from the real-time clock
 
331
        int rtc_data[ 7 ];
 
332
        RTC.get( rtc_data, true );
 
333
        time_hours = rtc_data[ DS1307_HR ];
 
334
        time_minutes = rtc_data[ DS1307_MIN ];
 
335
        time_seconds = rtc_data[ DS1307_SEC ];
306
336
 
307
337
        // serial comms
308
338
        Serial.begin( 9600 );