#include #include #include #include #include #define DEBUG 0 #define RC_SWITCH 1 // Utility macros #define adc_disable() (ADCSRA &= ~_BV(ADEN)) // disable ADC (before power-off) #define adc_enable() (ADCSRA |= _BV(ADEN)) // re-enable ADC #define enable_pin_interrupts() (GIMSK |= _BV(PCIE)) // Enable Pin Change Interrupts // Pins #define SWITCH 0 #if DEBUG #define RxD 0 #define TxD 4 SoftwareSerial AttinySerial(RxD,TxD); #endif #if RC_SWITCH #define SENDER 2 RCSwitch mySwitch = RCSwitch(); #endif void sleep(); void enableWdt(); void disableWdt(); void debug(const char* msg); char* group = "11111"; int number = 4; bool stateOn = false; bool timerTriggered = false; void setup() { #if DEBUG AttinySerial.begin(9600); #endif #if RC_SWITCH pinMode(SWITCH, INPUT_PULLUP); mySwitch.enableTransmit(SENDER); mySwitch.setProtocol(1); #endif set_sleep_mode(SLEEP_MODE_PWR_DOWN); enable_pin_interrupts(); } void loop() { sleep(); } void sendCommand(boolean on) { if (on) { #if RC_SWITCH mySwitch.switchOn((char*)"11111", 4); #endif debug("switch on"); } else { #if RC_SWITCH mySwitch.switchOff((char*)"11111", 4); #endif debug("switch off"); } } ISR(PCINT0_vect) { byte state = digitalRead(SWITCH); if (state == LOW) { debug("state is low"); enableWdt(); } else { debug("state is high"); disableWdt(); if (!timerTriggered) { sendCommand(true); } } timerTriggered = false; } ISR(WDT_vect) { timerTriggered = true; debug("wdt"); sendCommand(false); disableWdt(); } void sleep() { PCMSK |= _BV(PCINT0); // Use PB0 as interrupt pin adc_disable(); sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT) sei(); // Enable interrupts sleep_cpu(); // sleep cli(); // Disable interrupts PCMSK &= ~_BV(PCINT0); // Turn off PB0 as interrupt pin sleep_disable(); // Clear SE bit adc_enable(); sei(); // Enable interrupts } //enable the wdt for 1 sec interrupt void enableWdt() { cli(); MCUSR = 0x00; WDTCR |= _BV(WDCE) | _BV(WDE); WDTCR = _BV(WDIE) | _BV(WDP2) | _BV(WDP1); sei(); } void disableWdt() { cli(); wdt_reset(); MCUSR = 0x00; WDTCR |= _BV(WDCE) | _BV(WDE); WDTCR = 0x00; sei(); } void debug(const char* msg) { #if DEBUG AttinySerial.println(msg); #endif }