/* LED Blink, Teensyduino Tutorial #1 http://www.pjrc.com/teensy/tutorial.html */ #include #define LED_PIN 3 #define SWITCH_PIN 2 #define BUZZER_PIN 1 #define ALARM_LED_PIN 4 #define WAKEUP_STATUS RISING #define SWITCH_ALARM_STATUS LOW #define SWITCH digitalRead(SWITCH_PIN) #define ALARM_ON digitalWrite(ALARM_LED_PIN, HIGH); #define ALARM_OFF digitalWrite(ALARM_LED_PIN, LOW); #define LED_ON digitalWrite(LED_PIN, HIGH); #define LED_OFF digitalWrite(LED_PIN, LOW); #define ALARM_DELAY_SEC 10 // how long has the switch to be active to start the alarm? #define ALARM_CYCLE_MAX_SEC 82800 // 82800 == 23 hours, so if the user doesn't recognize the alarm, it will be audible every 23 hours static int i; void setup() { pinMode(SWITCH_PIN, INPUT); pinMode(LED_PIN, OUTPUT); pinMode(BUZZER_PIN, OUTPUT); pinMode(ALARM_LED_PIN, OUTPUT); LED_ON ALARM_ON tone(BUZZER_PIN, 50, 200); delay(260); tone(BUZZER_PIN, 500, 200); delay(260); tone(BUZZER_PIN, 2000, 200); delay(1000); LED_OFF ALARM_OFF delay(2000); } void loop() { /*io_init(); timers_init(); // wait 3 s to stabilize timer2 clock*/ while(1) { sleepNow(); // check how long the switch if open and activate buzzer if (switch_active_long()) { alarm(); } // switch is closed now, go to sleep again in next loop instruction } } // check if switch is active long enough bool switch_active_long(void) { int sec = 0; while (SWITCH == SWITCH_ALARM_STATUS) { led_blink(1,200,800); sec++; if (ALARM_DELAY_SEC == sec) { return true; } } return false; } void alarm(void) { unsigned long alarm_cycle = 1; unsigned long tick; while (SWITCH == SWITCH_ALARM_STATUS) { // buzz buzzer_blink(5, 500,500); ALARM_ON // calculate current alarm cycle time alarm_cycle = 10; if (alarm_cycle > ALARM_CYCLE_MAX_SEC) { alarm_cycle = ALARM_CYCLE_MAX_SEC; } // count down alarm cycle tick = alarm_cycle; while ((SWITCH == SWITCH_ALARM_STATUS) && (tick--)) { led_blink(1,200,800); } } ALARM_OFF } void sleepNow() // here we put the arduino to sleep { /* Now is the time to set the sleep mode. In the Atmega8 datasheet * http://www.atmel.com/dyn/resources/prod_documents/doc2486.pdf on page 35 * there is a list of sleep modes which explains which clocks and * wake up sources are available in which sleep mode. * * In the avr/sleep.h file, the call names of these sleep modes are to be found: * * The 5 different modes are: * SLEEP_MODE_IDLE -the least power savings * SLEEP_MODE_ADC * SLEEP_MODE_PWR_SAVE * SLEEP_MODE_STANDBY * SLEEP_MODE_PWR_DOWN -the most power savings * * For now, we want as much power savings as possible, so we * choose the according * sleep mode: SLEEP_MODE_PWR_DOWN * */ ALARM_OFF LED_OFF set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here sleep_enable(); // enables the sleep bit in the mcucr register // so sleep is possible. just a safety pin /* Now it is time to enable an interrupt. We do it here so an * accidentally pushed interrupt button doesn't interrupt * our running program. if you want to be able to run * interrupt code besides the sleep function, place it in * setup() for example. * * In the function call attachInterrupt(A, B, C) * A can be either 0 or 1 for interrupts on pin 2 or 3. * * B Name of a function you want to execute at interrupt for A. * * C Trigger mode of the interrupt pin. can be: * LOW a low level triggers * CHANGE a change in level triggers * RISING a rising edge of a level triggers * FALLING a falling edge of a level triggers * * In all but the IDLE sleep modes only LOW can be used. */ attachInterrupt(0,wakeUpNow, WAKEUP_STATUS); // use interrupt 0 (pin 2) and run function // wakeUpNow when pin 2 gets LOW sleep_mode(); // here the device is actually put to sleep!! // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP sleep_disable(); // first thing after waking from sleep: // disable sleep... detachInterrupt(0); // disables interrupt 0 on pin 2 so the // wakeUpNow code will not be executed // during normal running time. } void wakeUpNow() // here the interrupt is handled after wakeup { // execute code here after wake-up before returning to the loop() function // timers and code using timers (serial.print and more...) will not work here. // we don't really need to execute any special functions here, since we // just want the thing to wake up buzzer_blink(1, 500,500); } inline void led_blink(short count, short pause, short delayafterblink) { for (i = 0; i < count; i++) { LED_ON delay(pause); LED_OFF delay(delayafterblink); } } inline void buzzer_blink(short count, short pause, short delayafterblink) { for (i = 0; i < count; i++) { tone(BUZZER_PIN,4000,pause); delay(delayafterblink); } }