From 671b38233b2d987b4687101f27f401e629be2dcc Mon Sep 17 00:00:00 2001 From: Nicolae Hodos Date: Thu, 26 May 2016 22:42:35 +0200 Subject: [PATCH] add lamp switch for bedroom --- lamp_switch/lamp_switch.ino | 67 +++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 lamp_switch/lamp_switch.ino diff --git a/lamp_switch/lamp_switch.ino b/lamp_switch/lamp_switch.ino new file mode 100644 index 0000000..374f241 --- /dev/null +++ b/lamp_switch/lamp_switch.ino @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +// 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 +#define SENDER 2 + +RCSwitch mySwitch = RCSwitch(); + +bool stateOn = false; + +void setup() { + + pinMode(SWITCH, INPUT_PULLUP); + + mySwitch.enableTransmit(SENDER); + mySwitch.setProtocol(1); + + set_sleep_mode(SLEEP_MODE_PWR_DOWN); + enable_pin_interrupts(); + +} + +void loop() { + sleep(); +} + +void sendCommand() { + byte state = digitalRead(SWITCH); + if (state == LOW) { + if (stateOn) { + mySwitch.switchOff((char*)"11111", 4); + } else { + mySwitch.switchOn((char*)"11111", 4); + } + stateOn = !stateOn; + } +} + +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 +} + +ISR(PCINT0_vect) { + sendCommand(); +} +