diff --git a/doc/alarm-clock.dox b/doc/alarm-clock.dox index 909adf85..bcbc09ff 100644 --- a/doc/alarm-clock.dox +++ b/doc/alarm-clock.dox @@ -38,6 +38,7 @@ Field, SoftI2C, DS1307RTC (or DS3232RTC), Melody and \li Configurable alarm timeout between 2 and 10 seconds. \li Single button to activate the back light and/or stop the alarm. \li Up, down, left, and right buttons to change clock settings. +\li Relay that controls the power to a separate radio circuit. \section clock_main_circuit Main circuit @@ -57,9 +58,9 @@ See \ref clock_ds3232 "below" for information on using a DS3232-based clock module instead of a DS1307-based module. The whole circuit is built on a prototyping shield, with ribbon cables -connecting to the LCD. The Stop Alarm button and piezo buzzer are not -shown in this picture and some of the components are soldered to the bottom -of the shield: +connecting to the LCD. The Stop Alarm button, piezo buzzer, and radio +relay are not shown in this picture and some of the components are +soldered to the bottom of the shield: \image html clock_shield.jpg @@ -87,6 +88,17 @@ Arduino designs should also work. \image html kitten_minimal.jpg +\section clock_radio Controlling a radio + +The relay connected to D11 can be used to control the power to a separate +radio circuit so that the radio can be used as an alarm. The relay switch +should be inserted between the radio's power supply and the main radio circuit. +When the relay is off, no power is applied to the radio. If the radio is +powered off 5V, then the relay circuit can be altered as follows to power +the radio directly from the Arduino's power supply: + +\image html relay_control.png + \section clock_ds3232 Using DS3232 instead of DS1307 For clock modules based on the DS3232 chip, such as the diff --git a/libraries/RTC/examples/AlarmClock/AlarmClock.pde b/libraries/RTC/examples/AlarmClock/AlarmClock.pde index 5677e343..3d710ffe 100644 --- a/libraries/RTC/examples/AlarmClock/AlarmClock.pde +++ b/libraries/RTC/examples/AlarmClock/AlarmClock.pde @@ -40,6 +40,7 @@ #include "LowPowerMelody.h" // I/O pins that are used by this sketch. +#define RADIO 11 #define BUZZER 12 #define SENSE_BATTERY A1 #define RTC_DATA A4 @@ -70,6 +71,9 @@ LowPowerMelody alarmMelody(BUZZER); uint8_t prevHour = 24; bool is24HourClock = false; RTCAlarm nextAlarm; +bool isRadioPlaying = false; +bool sawFirstClick = false; +unsigned long firstClickTime; // Create the main form and its fields. Form mainForm(lcd); @@ -84,6 +88,7 @@ SetMelody alarmSound(mainForm, "Alarm sound"); SetTime setTime(mainForm, "Set current time"); SetDate setDate(mainForm, "Set current date"); BoolField hourMode(mainForm, "Hour display", "24 hour clock", "12 hour clock", false); +BoolField radioActive(mainForm, "Radio", "On", "Off", false); void setup() { // Reduce power consumption on I/O pins we don't need. @@ -95,7 +100,6 @@ void setup() { unusedPin(1); unusedPin(2); unusedPin(10); - unusedPin(11); unusedPin(13); // Turn off peripherals we don't need. @@ -131,6 +135,11 @@ void setup() { frontScreen.setDate(date); findNextAlarm(); + // The radio is turned on or off with a relay connected to an output pin. + // Configure the pin and turn the radio off initially. + digitalWrite(RADIO, LOW); + pinMode(RADIO, OUTPUT); + // Show the main form for the first time. mainForm.show(); } @@ -184,6 +193,12 @@ void loop() { rtc.writeByte(SETTING_SNOOZE, (byte)snooze.value()); } else if (alarmSound.isCurrent()) { rtc.writeByte(SETTING_MELODY, (byte)alarmSound.value()); + } else if (radioActive.isCurrent()) { + isRadioPlaying = radioActive.value(); + if (isRadioPlaying) + digitalWrite(RADIO, HIGH); + else + digitalWrite(RADIO, LOW); } prevHour = 24; // Force an update of the main screen. findNextAlarm(); // Update the time of the next alarm event. @@ -201,6 +216,21 @@ void loop() { // No alarm playing, so put the device to sleep to save power. sleepFor(SLEEP_15_MS); } + + // Select once means radio off, double-click select means radio on. + if (event == LCD_BUTTON_SELECT) { + unsigned long ms = millis(); + if (sawFirstClick && (ms - firstClickTime) > 500) + sawFirstClick = false; + if (sawFirstClick) { + sawFirstClick = false; + turnRadioOn(); + } else { + firstClickTime = ms; + sawFirstClick = true; + turnRadioOff(); + } + } } inline int timeToAlarm(const RTCTime ¤tTime, const RTCAlarm &alarm) @@ -279,3 +309,17 @@ void findNextAlarm(const RTCTime ¤tTime, const RTCAlarm &alarm) nextAlarm = alarm; } } + +void turnRadioOn() +{ + isRadioPlaying = true; + digitalWrite(RADIO, HIGH); + radioActive.setValue(true); +} + +void turnRadioOff() +{ + isRadioPlaying = false; + digitalWrite(RADIO, LOW); + radioActive.setValue(false); +} diff --git a/libraries/RTC/examples/AlarmClock/LowPowerMelody.cpp b/libraries/RTC/examples/AlarmClock/LowPowerMelody.cpp index 07745a1a..492db727 100644 --- a/libraries/RTC/examples/AlarmClock/LowPowerMelody.cpp +++ b/libraries/RTC/examples/AlarmClock/LowPowerMelody.cpp @@ -24,9 +24,25 @@ #include extern void findNextAlarm(); +extern void turnRadioOn(); +extern void turnRadioOff(); + +bool LowPowerMelody::isPlaying() const +{ + if (radioMode) + return radioAlarmActive; + else + return Melody::isPlaying(); +} void LowPowerMelody::play() { + if (radioMode) { + turnRadioOn(); + radioAlarmActive = true; + return; + } + // Turn on Timer2. power_timer2_enable(); @@ -36,6 +52,9 @@ void LowPowerMelody::play() void LowPowerMelody::playOnce() { + if (radioMode) + return; + // Turn on Timer2. power_timer2_enable(); @@ -45,6 +64,12 @@ void LowPowerMelody::playOnce() void LowPowerMelody::stop() { + if (radioMode) { + turnRadioOff(); + radioAlarmActive = false; + return; + } + // Stop the melody playing. Melody::stop(); diff --git a/libraries/RTC/examples/AlarmClock/LowPowerMelody.h b/libraries/RTC/examples/AlarmClock/LowPowerMelody.h index 714a586a..4583382d 100644 --- a/libraries/RTC/examples/AlarmClock/LowPowerMelody.h +++ b/libraries/RTC/examples/AlarmClock/LowPowerMelody.h @@ -27,11 +27,20 @@ class LowPowerMelody : public Melody { public: - LowPowerMelody(uint8_t pin) : Melody(pin) {} + LowPowerMelody(uint8_t pin) + : Melody(pin), radioMode(false), radioAlarmActive(false) {} + bool isPlaying() const; void play(); void playOnce(); void stop(); + + bool isRadioMode() const { return radioMode; } + void setRadioMode(bool mode) { radioMode = mode; } + +private: + bool radioMode; + bool radioAlarmActive; }; #endif diff --git a/libraries/RTC/examples/AlarmClock/SetMelody.cpp b/libraries/RTC/examples/AlarmClock/SetMelody.cpp index 15c24f6b..5054454f 100644 --- a/libraries/RTC/examples/AlarmClock/SetMelody.cpp +++ b/libraries/RTC/examples/AlarmClock/SetMelody.cpp @@ -48,10 +48,12 @@ static byte sosLengths[] = {8, 8, 8, 8, 4, 4, 4, 8, 8, 8, 8, 2}; static const char item_FourBeeps[] PROGMEM = "Four beeps"; static const char item_Haircut[] PROGMEM = "Shave 'n haircut"; static const char item_SOS[] PROGMEM = "S.O.S."; +static const char item_Radio[] PROGMEM = "Radio"; static ListItem melodyNames[] PROGMEM = { item_FourBeeps, item_Haircut, item_SOS, + item_Radio, 0 }; @@ -79,12 +81,19 @@ void SetMelody::updateMelody() switch (value()) { case 0: default: alarmMelody.setMelody(defaultMelodyNotes, defaultMelodyLengths, sizeof(defaultMelodyLengths)); + alarmMelody.setRadioMode(false); break; case 1: alarmMelody.setMelody(haircutNotes, haircutLengths, sizeof(haircutLengths)); + alarmMelody.setRadioMode(false); break; case 2: alarmMelody.setMelody(sosNotes, sosLengths, sizeof(sosLengths)); + alarmMelody.setRadioMode(false); + break; + case 3: + alarmMelody.setMelody(defaultMelodyNotes, defaultMelodyLengths, sizeof(defaultMelodyLengths)); + alarmMelody.setRadioMode(true); break; } } diff --git a/libraries/RTC/examples/AlarmClock/alarm_circuit.fig b/libraries/RTC/examples/AlarmClock/alarm_circuit.fig index 9a36eff1..f529d96d 100644 --- a/libraries/RTC/examples/AlarmClock/alarm_circuit.fig +++ b/libraries/RTC/examples/AlarmClock/alarm_circuit.fig @@ -423,6 +423,54 @@ Single 2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 10575 4275 10480 4275 -6 +6 4815 6075 4950 6525 +5 1 0 1 0 -1 0 0 -1 0.000 1 1 0 0 4950.000 6390.000 4950 6345 4905 6390 4950 6435 +5 1 0 1 0 -1 0 0 -1 0.000 1 1 0 0 4950.000 6300.000 4950 6255 4905 6300 4950 6345 +5 1 0 1 0 -1 0 0 -1 0.000 1 1 0 0 4950.000 6210.000 4950 6165 4905 6210 4950 6255 +2 1 0 1 0 -1 0 0 -1 0.000 0 1 -1 0 0 2 + 4950 6435 4950 6525 +2 1 0 1 0 -1 0 0 -1 0.000 0 1 -1 0 0 2 + 4950 6165 4950 6075 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 4880 6165 4880 6435 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 4850 6165 4850 6435 +-6 +6 5355 6075 5445 6435 +2 1 0 1 0 -1 0 0 20 0.000 1 0 -1 0 0 4 + 5400 6210 5445 6300 5355 6300 5400 6210 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 5355 6210 5445 6210 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 5400 6300 5400 6435 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 5400 6210 5400 6075 +-6 +6 5085 6750 5265 7020 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 5160 7020 5190 7020 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 5125 6975 5225 6975 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 5085 6930 5265 6930 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 5175 6750 5175 6930 +-6 +6 5130 5805 5220 5895 +1 3 0 1 0 -1 0 0 20 0.000 1 0.0000 5175 5850 30 30 5175 5850 5175 5880 +-6 +6 5130 6705 5220 6795 +1 3 0 1 0 -1 0 0 20 0.000 1 0.0000 5175 6750 30 30 5175 6750 5175 6780 +-6 +6 4365 6120 4455 6210 +1 3 0 1 0 -1 0 0 20 0.000 1 0.0000 4410 6165 15 15 4410 6165 4425 6165 +-6 +6 4365 6255 4455 6345 +1 3 0 1 0 -1 0 0 20 0.000 1 0.0000 4410 6300 15 15 4410 6300 4425 6300 +-6 +6 4365 6390 4455 6480 +1 3 0 1 0 -1 0 0 20 0.000 1 0.0000 4410 6435 15 15 4410 6435 4425 6435 +-6 2 2 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 5 4500 1350 7425 1350 7425 5400 4500 5400 4500 1350 2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 3 @@ -506,6 +554,24 @@ Single 11475 3960 11475 4050 2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 2 10575 4275 11115 4275 +2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 4 + 4950 6075 4950 5850 5400 5850 5400 6075 +2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 4 + 4950 6525 4950 6750 5400 6750 5400 6435 +2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 2 + 5175 5850 5175 5400 +2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 4 + 4410 6165 4725 6165 4680 6210 4635 6165 +2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 4 + 4410 6435 4725 6435 4680 6390 4635 6435 +2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 2 + 4410 6300 4725 6210 +2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 1 0 2 + 1 1 1.00 60.00 120.00 + 4410 6300 3375 6300 +2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 1 0 2 + 1 1 1.00 60.00 120.00 + 4410 6435 3375 6435 4 0 0 50 -1 0 12 0.0000 4 135 450 4590 1710 Reset\001 4 0 0 50 -1 0 12 4.7124 4 135 405 8730 1710 GND\001 4 0 0 50 -1 0 12 4.7124 4 135 240 8955 1890 5V\001 @@ -561,3 +627,8 @@ Single 4 0 0 50 -1 0 12 0.0000 4 180 1215 11610 3735 33R (adjust for\001 4 0 0 50 -1 0 12 0.0000 4 180 840 11745 3960 LED type)\001 4 0 0 50 -1 0 12 0.0000 4 135 675 5535 2655 Arduino\001 +4 0 0 50 -1 0 12 0.0000 4 135 615 5535 6345 IN4001\001 +4 0 0 50 -1 0 12 0.0000 4 135 345 5040 5310 D11\001 +4 0 0 50 -1 0 12 0.0000 4 135 240 4455 6705 5V\001 +4 0 0 50 -1 0 12 0.0000 4 180 480 4365 6885 Relay\001 +4 0 0 50 -1 0 12 0.0000 4 135 765 2430 6435 To Radio\001 diff --git a/libraries/RTC/examples/AlarmClock/alarm_circuit.pdf b/libraries/RTC/examples/AlarmClock/alarm_circuit.pdf index 2f156893..17c4d4cd 100644 Binary files a/libraries/RTC/examples/AlarmClock/alarm_circuit.pdf and b/libraries/RTC/examples/AlarmClock/alarm_circuit.pdf differ diff --git a/libraries/RTC/examples/AlarmClock/alarm_circuit.png b/libraries/RTC/examples/AlarmClock/alarm_circuit.png index 2607c93d..4614b6f8 100644 Binary files a/libraries/RTC/examples/AlarmClock/alarm_circuit.png and b/libraries/RTC/examples/AlarmClock/alarm_circuit.png differ diff --git a/libraries/RTC/examples/AlarmClock/relay_control.fig b/libraries/RTC/examples/AlarmClock/relay_control.fig new file mode 100644 index 00000000..9c7db23f --- /dev/null +++ b/libraries/RTC/examples/AlarmClock/relay_control.fig @@ -0,0 +1,95 @@ +#FIG 3.2 Produced by xfig version 3.2.5b +Landscape +Center +Metric +A4 +100.00 +Single +-2 +1200 2 +6 4815 6075 4950 6525 +5 1 0 1 0 -1 0 0 -1 0.000 1 1 0 0 4950.000 6390.000 4950 6345 4905 6390 4950 6435 +5 1 0 1 0 -1 0 0 -1 0.000 1 1 0 0 4950.000 6300.000 4950 6255 4905 6300 4950 6345 +5 1 0 1 0 -1 0 0 -1 0.000 1 1 0 0 4950.000 6210.000 4950 6165 4905 6210 4950 6255 +2 1 0 1 0 -1 0 0 -1 0.000 0 1 -1 0 0 2 + 4950 6435 4950 6525 +2 1 0 1 0 -1 0 0 -1 0.000 0 1 -1 0 0 2 + 4950 6165 4950 6075 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 4880 6165 4880 6435 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 4850 6165 4850 6435 +-6 +6 5355 6075 5445 6435 +2 1 0 1 0 -1 0 0 20 0.000 1 0 -1 0 0 4 + 5400 6210 5445 6300 5355 6300 5400 6210 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 5355 6210 5445 6210 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 5400 6300 5400 6435 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 5400 6210 5400 6075 +-6 +6 5085 6750 5265 7020 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 5160 7020 5190 7020 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 5125 6975 5225 6975 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 5085 6930 5265 6930 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 5175 6750 5175 6930 +-6 +6 5130 5805 5220 5895 +1 3 0 1 0 -1 0 0 20 0.000 1 0.0000 5175 5850 30 30 5175 5850 5175 5880 +-6 +6 5130 6705 5220 6795 +1 3 0 1 0 -1 0 0 20 0.000 1 0.0000 5175 6750 30 30 5175 6750 5175 6780 +-6 +6 4365 6120 4455 6210 +1 3 0 1 0 -1 0 0 20 0.000 1 0.0000 4410 6165 15 15 4410 6165 4425 6165 +-6 +6 4365 6255 4455 6345 +1 3 0 1 0 -1 0 0 20 0.000 1 0.0000 4410 6300 15 15 4410 6300 4425 6300 +-6 +6 4365 6390 4455 6480 +1 3 0 1 0 -1 0 0 20 0.000 1 0.0000 4410 6435 15 15 4410 6435 4425 6435 +-6 +6 3960 6660 4140 6930 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 4035 6930 4065 6930 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 4000 6885 4100 6885 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 3960 6840 4140 6840 +2 1 0 1 0 -1 0 0 -1 0.000 0 0 -1 0 0 2 + 4050 6660 4050 6840 +-6 +2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 4 + 4950 6075 4950 5850 5400 5850 5400 6075 +2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 4 + 4950 6525 4950 6750 5400 6750 5400 6435 +2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 4 + 4410 6165 4725 6165 4680 6210 4635 6165 +2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 4 + 4410 6435 4725 6435 4680 6390 4635 6435 +2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 2 + 4410 6300 4725 6210 +2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 1 0 2 + 1 1 1.00 60.00 120.00 + 4410 6435 3375 6435 +2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 1 0 3 + 1 1 1.00 60.00 120.00 + 4410 6300 4050 6300 4050 5400 +2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 1 0 2 + 1 1 1.00 60.00 120.00 + 5175 5850 5175 5400 +2 1 0 1 0 7 50 -1 -1 0.000 0 0 -1 1 0 2 + 1 1 1.00 60.00 120.00 + 4050 6660 3375 6660 +4 0 0 50 -1 0 12 0.0000 4 135 615 5535 6345 IN4001\001 +4 0 0 50 -1 0 12 0.0000 4 135 240 4455 6705 5V\001 +4 0 0 50 -1 0 12 0.0000 4 180 480 4365 6885 Relay\001 +4 0 0 50 -1 0 12 0.0000 4 135 345 4995 5355 D11\001 +4 0 0 50 -1 0 12 0.0000 4 135 240 3960 5355 5V\001 +4 0 0 50 -1 0 12 0.0000 4 135 765 2520 6615 To Radio\001 diff --git a/libraries/RTC/examples/AlarmClock/relay_control.pdf b/libraries/RTC/examples/AlarmClock/relay_control.pdf new file mode 100644 index 00000000..af810d10 Binary files /dev/null and b/libraries/RTC/examples/AlarmClock/relay_control.pdf differ diff --git a/libraries/RTC/examples/AlarmClock/relay_control.png b/libraries/RTC/examples/AlarmClock/relay_control.png new file mode 100644 index 00000000..53b0bf49 Binary files /dev/null and b/libraries/RTC/examples/AlarmClock/relay_control.png differ