short press-on, long press-off
This commit is contained in:
parent
2c1fa781ab
commit
48bc0df2b2
@ -1,8 +1,18 @@
|
|||||||
#include <RCSwitch.h>
|
|
||||||
#include <avr/sleep.h>
|
#include <avr/sleep.h>
|
||||||
#include <avr/power.h>
|
#include <avr/power.h>
|
||||||
#include <avr/wdt.h>
|
#include <avr/wdt.h>
|
||||||
|
|
||||||
|
#define DEBUG 0
|
||||||
|
#define RC_SWITCH 1
|
||||||
|
|
||||||
|
#if RC_SWITCH
|
||||||
|
#include <RCSwitch.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
#include <SoftwareSerial_Tiny.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
// Utility macros
|
// Utility macros
|
||||||
#define adc_disable() (ADCSRA &= ~_BV(ADEN)) // disable ADC (before power-off)
|
#define adc_disable() (ADCSRA &= ~_BV(ADEN)) // disable ADC (before power-off)
|
||||||
#define adc_enable() (ADCSRA |= _BV(ADEN)) // re-enable ADC
|
#define adc_enable() (ADCSRA |= _BV(ADEN)) // re-enable ADC
|
||||||
@ -10,38 +20,82 @@
|
|||||||
|
|
||||||
// Pins
|
// Pins
|
||||||
#define SWITCH 0
|
#define SWITCH 0
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
#define RxD 0
|
||||||
|
#define TxD 1
|
||||||
|
SoftwareSerial AttinySerial(RxD,TxD);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if RC_SWITCH
|
||||||
#define SENDER 2
|
#define SENDER 2
|
||||||
|
|
||||||
RCSwitch mySwitch = RCSwitch();
|
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 stateOn = false;
|
||||||
|
bool timerTriggered = false;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
AttinySerial.begin(9600);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if RC_SWITCH
|
||||||
pinMode(SWITCH, INPUT_PULLUP);
|
pinMode(SWITCH, INPUT_PULLUP);
|
||||||
|
|
||||||
mySwitch.enableTransmit(SENDER);
|
mySwitch.enableTransmit(SENDER);
|
||||||
mySwitch.setProtocol(1);
|
mySwitch.setProtocol(1);
|
||||||
|
#endif
|
||||||
|
|
||||||
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
|
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
|
||||||
enable_pin_interrupts();
|
enable_pin_interrupts();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
sleep();
|
sleep();
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendCommand() {
|
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);
|
byte state = digitalRead(SWITCH);
|
||||||
if (state == LOW) {
|
if (state == LOW) {
|
||||||
if (stateOn) {
|
debug("state is low");
|
||||||
mySwitch.switchOff((char*)"11111", 4);
|
enableWdt();
|
||||||
} else {
|
} else {
|
||||||
mySwitch.switchOn((char*)"11111", 4);
|
debug("state is high");
|
||||||
|
disableWdt();
|
||||||
|
if (!timerTriggered) {
|
||||||
|
sendCommand(true);
|
||||||
}
|
}
|
||||||
stateOn = !stateOn;
|
|
||||||
}
|
}
|
||||||
|
timerTriggered = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ISR(WDT_vect) {
|
||||||
|
debug("wdt");
|
||||||
|
sendCommand(false);
|
||||||
|
timerTriggered = true;
|
||||||
|
disableWdt();
|
||||||
}
|
}
|
||||||
|
|
||||||
void sleep() {
|
void sleep() {
|
||||||
@ -61,7 +115,27 @@ void sleep() {
|
|||||||
sei(); // Enable interrupts
|
sei(); // Enable interrupts
|
||||||
}
|
}
|
||||||
|
|
||||||
ISR(PCINT0_vect) {
|
//enable the wdt for 1 sec interrupt
|
||||||
sendCommand();
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user