send voltage information

This commit is contained in:
Nicu Hodos 2017-01-29 12:04:57 +01:00
parent c6c2b2c93c
commit c5860fdc9a

View File

@ -13,9 +13,9 @@
#define SENDER 2
#define CONTROLLER 4
#define SENSOR_ID 3
RCSwitch mySwitch = RCSwitch();
char* CIRCUIT_OPEN = "00000000000000000000000001000011";
char* CIRCUIT_CLOSED = "00000000000000000000000001100011";
int counter = 0;
@ -48,11 +48,11 @@ bool readState() {
}
void sendWindowState() {
if (readState()) {
mySwitch.send(CIRCUIT_OPEN);
} else {
mySwitch.send(CIRCUIT_CLOSED);
}
unsigned long value = 0x70000000;
value |= readVcc() << 6;
value |= !readState() << 5;
value |= SENSOR_ID;
mySwitch.send(value, 32);
}
void sleep() {
@ -96,3 +96,29 @@ void enableWdt()
WDTCR = _BV(WDIE) | _BV(WDP3) | _BV(WDP0); //8192ms
}
long readVcc() {
// Read 1.1V reference against AVcc
// set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA,ADSC)); // measuring
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both
long result = (high<<8) | low;
result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
return result; // Vcc in millivolts
}