rc-gateway/water_sensor/water_sensor.ino
Nicolae Hodos dde230013c add water sensor
change the code to the same range as window sensors
2016-05-26 22:41:50 +02:00

99 lines
2.1 KiB
C++

#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
#define CONTROLLER 4
RCSwitch mySwitch = RCSwitch();
char* CIRCUIT_OPEN = "00000000000000000000000001000011";
char* CIRCUIT_CLOSED = "00000000000000000000000001100011";
int counter = 0;
void setup() {
pinMode(SWITCH, INPUT_PULLUP);
pinMode(CONTROLLER, OUTPUT);
digitalWrite(CONTROLLER, HIGH);
mySwitch.enableTransmit(SENDER);
mySwitch.setProtocol(2);
sendWindowState();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
enable_pin_interrupts();
enableWdt();
}
void loop() {
sleep();
}
bool readState() {
digitalWrite(CONTROLLER, LOW);
bool state = digitalRead(SWITCH);
digitalWrite(CONTROLLER, HIGH);
return state;
}
void sendWindowState() {
if (readState()) {
mySwitch.send(CIRCUIT_OPEN);
} else {
mySwitch.send(CIRCUIT_CLOSED);
}
}
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) {
sendWindowState();
delay(5000);
sendWindowState();
}
ISR(WDT_vect) {
counter++;
if (counter % 220 == 0) {
counter = 0;
sendWindowState();
delay(10000);
sendWindowState();
}
}
//enable the wdt for 8sec interrupt
void enableWdt()
{
MCUSR = 0x00;
WDTCR |= _BV(WDCE) | _BV(WDE);
WDTCR = _BV(WDIE) | _BV(WDP3) | _BV(WDP0); //8192ms
}