add lamp switch for bedroom
This commit is contained in:
parent
dde230013c
commit
671b38233b
67
lamp_switch/lamp_switch.ino
Normal file
67
lamp_switch/lamp_switch.ino
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
#include <RCSwitch.h>
|
||||||
|
#include <avr/sleep.h>
|
||||||
|
#include <avr/power.h>
|
||||||
|
#include <avr/wdt.h>
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user