use common libraries and add voltage

This commit is contained in:
Nicu Hodos 2017-01-29 14:00:08 +01:00
parent c5860fdc9a
commit 72842a4b60
2 changed files with 30 additions and 116 deletions

View File

@ -1,21 +1,13 @@
#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
#include <TinyPower.h>
#include <TinySensor.h>
// 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
}

View File

@ -1,20 +1,12 @@
#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
#include <TinyPower.h>
#include <TinySensor.h>
// 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
}