From 72842a4b60d39b95b7cf95fd7081a6cd76720af0 Mon Sep 17 00:00:00 2001 From: Nicolae Hodos Date: Sun, 29 Jan 2017 14:00:08 +0100 Subject: [PATCH] use common libraries and add voltage --- window1/window1.ino | 73 ++++++++++----------------------------------- window2/window2.ino | 73 ++++++++++----------------------------------- 2 files changed, 30 insertions(+), 116 deletions(-) diff --git a/window1/window1.ino b/window1/window1.ino index 862bd6e..9c4db09 100644 --- a/window1/window1.ino +++ b/window1/window1.ino @@ -1,21 +1,13 @@ -#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 +#include +#include // Pins #define SWITCH 0 #define SENDER 2 #define CONTROLLER 4 +#define SENSOR_ID 1 -RCSwitch mySwitch = RCSwitch(); -char* WND_OPEN = "00000000000000000000000001000001"; -char* WND_CLOSED = "00000000000000000000000001100001"; +TinySensor sensor = TinySensor(SENSOR_ID, SENDER); int counter = 0; bool currentState; @@ -26,75 +18,40 @@ void setup() { pinMode(CONTROLLER, OUTPUT); digitalWrite(CONTROLLER, LOW); - mySwitch.enableTransmit(SENDER); - mySwitch.setProtocol(2); + sensor.setup(); updateState(); - sendWindowState(); - - set_sleep_mode(SLEEP_MODE_PWR_DOWN); - enable_pin_interrupts(); - enableWdt(); + sensor.sendWindowState(readState()); + TinyPower::setup(); } void loop() { - sleep(); + TinyPower::sleep(); } void updateState() { - currentState = digitalRead(SWITCH); + currentState = readState(); } -void sendWindowState() { - byte state = digitalRead(SWITCH); - if (state == HIGH) { - mySwitch.send(WND_OPEN); - } else { - mySwitch.send(WND_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 +byte readState() { + return digitalRead(SWITCH); } ISR(PCINT0_vect) { - sendWindowState(); + sensor.sendWindowState(readState()); } ISR(WDT_vect) { - bool state = digitalRead(SWITCH); + bool state = readState(); if (state != currentState) { - sendWindowState(); + sensor.sendWindowState(readState()); updateState(); return; } counter++; if (counter % 76 == 0) { - sendWindowState(); + sensor.sendWindowState(readState()); counter = 0; } } - -//enable the wdt for 8sec interrupt -void enableWdt() -{ - MCUSR = 0x00; - WDTCR |= _BV(WDCE) | _BV(WDE); - WDTCR = _BV(WDIE) | _BV(WDP3) | _BV(WDP0); //8192ms -} - diff --git a/window2/window2.ino b/window2/window2.ino index 25d27ff..d450336 100644 --- a/window2/window2.ino +++ b/window2/window2.ino @@ -1,20 +1,12 @@ -#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 +#include +#include // Pins #define SWITCH 0 #define SENDER 2 +#define SENSOR_ID 2 -RCSwitch mySwitch = RCSwitch(); -char* WND_OPEN = "00000000000000000000000001000010"; -char* WND_CLOSED = "00000000000000000000000001100010"; +TinySensor sensor = TinySensor(SENSOR_ID, SENDER); int counter = 0; bool currentState; @@ -23,75 +15,40 @@ void setup() { pinMode(SWITCH, INPUT_PULLUP); - mySwitch.enableTransmit(SENDER); - mySwitch.setProtocol(2); + sensor.setup(); updateState(); - sendWindowState(); - - set_sleep_mode(SLEEP_MODE_PWR_DOWN); - enable_pin_interrupts(); - enableWdt(); + sensor.sendWindowState(readState()); + TinyPower::setup(); } void loop() { - sleep(); + TinyPower::sleep(); } void updateState() { - currentState = digitalRead(SWITCH); + currentState = readState(); } -void sendWindowState() { - byte state = digitalRead(SWITCH); - if (state == HIGH) { - mySwitch.send(WND_OPEN); - } else { - mySwitch.send(WND_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 +byte readState() { + return digitalRead(SWITCH); } ISR(PCINT0_vect) { - sendWindowState(); + sensor.sendWindowState(readState()); } ISR(WDT_vect) { - bool state = digitalRead(SWITCH); + bool state = readState(); if (state != currentState) { - sendWindowState(); + sensor.sendWindowState(readState()); updateState(); return; } counter++; if (counter % 76 == 0) { - sendWindowState(); + sensor.sendWindowState(readState()); counter = 0; } } - -//enable the wdt for 8sec interrupt -void enableWdt() -{ - MCUSR = 0x00; - WDTCR |= _BV(WDCE) | _BV(WDE); - WDTCR = _BV(WDIE) | _BV(WDP3) | _BV(WDP0); //8192ms -} -