Make battery reading more efficient

This commit is contained in:
Felix Rusu 2014-11-03 12:02:11 -05:00
parent 39f873d1b5
commit b977159efd
1 changed files with 25 additions and 12 deletions

View File

@ -51,13 +51,14 @@
//#define FREQUENCY RF69_868MHZ
#define FREQUENCY RF69_915MHZ
#define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
#define IS_RFM69HW //uncomment only for RFM69HW! Remove/comment if you have RFM69W!
//#define IS_RFM69HW //uncomment only for RFM69HW! Remove/comment if you have RFM69W!
//*********************************************************************************************
#define ACK_TIME 30 // max # of ms to wait for an ack
#define ONBOARDLED 9 // Moteinos have LEDs on D9
#define EXTLED 5 // MotionOLEDMote has an external LED on D5
#define BATT_MONITOR A7 // Sense VBAT_COND signal (when powered externally should read ~3.25v/3.3v (1000-1023), when external power is cutoff it should start reading around 2.85v/3.3v * 1023 ~= 880 (ratio given by 10k+4.7K divider from VBAT_COND = 1.47 multiplier)
#define BATT_CYCLES 30 //read and report battery voltage every this many wakeup cycles (ex 30cycles * 8sec sleep = 240sec/4min)
#define MOTIONPIN 1 //hardware interrupt 1 (D3)
//#define SERIAL_EN //comment this out when deploying to an installed SM to save a few KB of sketch size
@ -98,15 +99,12 @@ void motionIRQ()
motionDetected=true;
}
byte batteryReportCycles=0;
void loop() {
checkBattery();
if (motionDetected)
{
digitalWrite(EXTLED, HIGH);
//TODO: read several samples and take average
//TODO: read analog only every N cycles
batteryVolts = analogRead(A7) * 0.00322 * 1.42;
dtostrf(batteryVolts, 3,2, BATstr);
//sprintf(sendBuf, "MOTION ............................................ BAT:%sv", BATstr);
sprintf(sendBuf, "MOTION BAT:%sv", BATstr);
sendLen = strlen(sendBuf);
@ -114,6 +112,7 @@ void loop() {
{
DEBUG("MOTION ACK:OK! RSSI:");
DEBUG(radio.RSSI);
batteryReportCycles = 0;
}
else DEBUG("MOTION ACK:NOK...");
@ -121,17 +120,31 @@ void loop() {
DEBUGln(BATstr);
radio.sleep();
//Blink(EXTLED,50); //Blink(ONBOARDLED,3);
motionDetected=false;
digitalWrite(EXTLED, LOW);
}
else if (batteryReportCycles == BATT_CYCLES)
{
sprintf(sendBuf, "BAT:%sv", BATstr);
sendLen = strlen(sendBuf);
radio.send(GATEWAYID, sendBuf, sendLen);
radio.sleep();
batteryReportCycles=0;
}
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
batteryReportCycles++;
}
void Blink(byte PIN, int DELAY_MS)
byte cycleCount=BATT_CYCLES;
void checkBattery()
{
pinMode(PIN, OUTPUT);
digitalWrite(PIN,HIGH);
delay(DELAY_MS);
digitalWrite(PIN,LOW);
if (cycleCount++ == BATT_CYCLES) //only read battery every BATT_CYCLES sleep cycles
{
unsigned int readings=0;
for (byte i=0; i<10; i++) //take 10 samples, and average
readings+=analogRead(BATT_MONITOR);
batteryVolts = (readings / 10.0) * 0.00322 * 1.42;
dtostrf(batteryVolts, 3,2, BATstr); //update the BATStr which gets sent every BATT_CYCLES or along with the MOTION message
cycleCount = 0;
}
}