From bf5436ca68fdbeb7c9e1f02900ec27913fba8fae Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Fri, 23 Dec 2011 21:44:39 +0100 Subject: Better alarm ring via timer manipulation Timer procedure by Diego --- .../fuzzy_alarm_clock_ds1307.pde | 54 +++++++++++----------- 1 file changed, 28 insertions(+), 26 deletions(-) (limited to 'arduino_sketch') diff --git a/arduino_sketch/fuzzy_alarm_clock_ds1307/fuzzy_alarm_clock_ds1307.pde b/arduino_sketch/fuzzy_alarm_clock_ds1307/fuzzy_alarm_clock_ds1307.pde index 8a88b38..c08bd6a 100644 --- a/arduino_sketch/fuzzy_alarm_clock_ds1307/fuzzy_alarm_clock_ds1307.pde +++ b/arduino_sketch/fuzzy_alarm_clock_ds1307/fuzzy_alarm_clock_ds1307.pde @@ -3,7 +3,7 @@ #include // minutes of "dawn" before alarm -#define TIN 30 +#define TIN 2 // "dawn" + "daylight" #define TDAY 45 // "dawn" + "daylight" + blue blinding light @@ -13,17 +13,22 @@ #define NALARMS 4 // pins and addressed -#define RPIN 3 -#define YPIN 5 -#define BPIN 6 +#define RPIN 5 +#define YPIN 6 +#define BPIN 9 -#define APIN 9 +#define APIN0 3 +#define APIN1 11 int st = 0; // alarm status (minutes from alarm - TIN) char alarms[NALARMS][5]; // alarm settings char cmin; // current minute int a = -1; // current alarm char dbg = 0; // print debug informations +bool ringing = false; // sound the alarm + +#define NFREQ 9 +int freq[] = { 255, 192, 128, 96, 64, 96, 128, 192, 255 }; // frequencies for the alarm void setup () { Serial.begin(9600); @@ -33,6 +38,11 @@ void setup () { pinMode(YPIN,OUTPUT); pinMode(BPIN,OUTPUT); + pinMode(APIN0,INPUT); + pinMode(APIN1,INPUT); + TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM20); + TCCR2B = _BV(WGM22) | _BV(CS22); + digitalWrite(RPIN,255); digitalWrite(YPIN,255); digitalWrite(BPIN,0); @@ -90,9 +100,13 @@ void loop () { set_leds(); } if ( st == TIN ) { + ringing = true; + } else { + ringing = false; + } + if ( ringing ) { ring_alarm(); } - // wait about till the next second delay(1000); @@ -339,26 +353,14 @@ void set_leds() { // PC speaker // void ring_alarm() { - analogWrite(APIN,8); - delay(500); - analogWrite(APIN,0); - delay(500); - analogWrite(APIN,16); - delay(500); - analogWrite(APIN,0); - delay(500); - analogWrite(APIN,32); - delay(500); - analogWrite(APIN,0); - delay(500); - analogWrite(APIN,64); - delay(500); - analogWrite(APIN,0); - delay(500); - analogWrite(APIN,128); - delay(500); - analogWrite(APIN,0); - delay(500); + + pinMode(APIN1,OUTPUT); + for (int i=0; i