1
0
mirror of https://github.com/taigrr/arduinolibs synced 2025-01-18 04:33:12 -08:00

Introduce an alarm clock control for a radio

This commit is contained in:
Rhys Weatherley
2012-06-02 21:10:07 +10:00
parent 109a0ccc85
commit f5f4282e1a
11 changed files with 270 additions and 5 deletions

View File

@@ -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 &currentTime, const RTCAlarm &alarm)
@@ -279,3 +309,17 @@ void findNextAlarm(const RTCTime &currentTime, 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);
}

View File

@@ -24,9 +24,25 @@
#include <avr/power.h>
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();

View File

@@ -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

View File

@@ -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;
}
}

View File

@@ -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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

After

Width:  |  Height:  |  Size: 38 KiB

View File

@@ -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

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB