diff --git a/window1/README.md b/window1/README.md new file mode 100644 index 0000000..881097c --- /dev/null +++ b/window1/README.md @@ -0,0 +1,8 @@ +# Window1 +Sensor for mezzanine window + +## Schema +![](./wiki/schema.png) + +## Breadboard +![](./wiki/breadboard.png) \ No newline at end of file diff --git a/window1/wiki/breadboard.png b/window1/wiki/breadboard.png new file mode 100644 index 0000000..e395ce3 Binary files /dev/null and b/window1/wiki/breadboard.png differ diff --git a/window1/wiki/schema.png b/window1/wiki/schema.png new file mode 100644 index 0000000..0800272 Binary files /dev/null and b/window1/wiki/schema.png differ diff --git a/window1/wiki/window1.fzz b/window1/wiki/window1.fzz new file mode 100644 index 0000000..d0e47da Binary files /dev/null and b/window1/wiki/window1.fzz differ diff --git a/window1/window1.ino b/window1/window1.ino index 2e0ffa4..862bd6e 100644 --- a/window1/window1.ino +++ b/window1/window1.ino @@ -1,16 +1,24 @@ -#include -#include +#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 #define CONTROLLER 4 -ContactSensor sensor = ContactSensor(WINDOW1, SENDER); +RCSwitch mySwitch = RCSwitch(); +char* WND_OPEN = "00000000000000000000000001000001"; +char* WND_CLOSED = "00000000000000000000000001100001"; -volatile int counter = 0; -volatile bool shouldSend = true; -bool currentState = false; +int counter = 0; +bool currentState; void setup() { @@ -18,38 +26,75 @@ void setup() { pinMode(CONTROLLER, OUTPUT); digitalWrite(CONTROLLER, LOW); - sensor.setup(); + mySwitch.enableTransmit(SENDER); + mySwitch.setProtocol(2); + + updateState(); + sendWindowState(); + + set_sleep_mode(SLEEP_MODE_PWR_DOWN); + enable_pin_interrupts(); + enableWdt(); - TinyPower::setup(); - TinyPower::enableWdt(WDTO_8S); } void loop() { - if (shouldSend) { - shouldSend = false; - currentState = digitalRead(SWITCH); - sensor.sendState(currentState); + sleep(); +} + +void updateState() { + currentState = digitalRead(SWITCH); +} + +void sendWindowState() { + byte state = digitalRead(SWITCH); + if (state == HIGH) { + mySwitch.send(WND_OPEN); + } else { + mySwitch.send(WND_CLOSED); } - TinyPower::sleep(); +} + +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) { - shouldSend = true; - wdt_reset(); - counter = 0; - TinyPower::enableWdt(WDTO_8S); + sendWindowState(); } ISR(WDT_vect) { + bool state = digitalRead(SWITCH); + if (state != currentState) { + sendWindowState(); + updateState(); + return; + } counter++; - if (counter == 1) { - shouldSend = true; - return; - } - if (counter % 75 == 0) { - shouldSend = true; - } - if (!currentState && counter >= 225) { - TinyPower::disableWdt(); + if (counter % 76 == 0) { + sendWindowState(); + 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/README.md b/window2/README.md new file mode 100644 index 0000000..99748f8 --- /dev/null +++ b/window2/README.md @@ -0,0 +1,8 @@ +# Window2 +Sensor for kids room window + +## Schema +![](./wiki/schema.png) + +## Breadboard +![](./wiki/breadboard.png) \ No newline at end of file diff --git a/window2/wiki/breadboard.png b/window2/wiki/breadboard.png new file mode 100644 index 0000000..103bbb2 Binary files /dev/null and b/window2/wiki/breadboard.png differ diff --git a/window2/wiki/schema.png b/window2/wiki/schema.png new file mode 100644 index 0000000..b161723 Binary files /dev/null and b/window2/wiki/schema.png differ diff --git a/window2/wiki/window2.fzz b/window2/wiki/window2.fzz new file mode 100644 index 0000000..3ecaed5 Binary files /dev/null and b/window2/wiki/window2.fzz differ