Hello all,
Relative newb here, I am a hardware guy who has been playing with the Arduino in a few projects, but I have no real formal software background.
My project is a remote sensor that monitors battery voltage, solar voltage, temperature, humidity, wind speed and direction, UV levels, and a few other things. It then reports results over a short range UHF radio link.
Exact timing is not critical, and update rates are slow. To that end I have used delays vs millis or an RTC, to time things such as when reports are sent.
Because the sensor is in a location that is a pain to get to I have over implemented protections. The watchdog timer is in place, the system can reset itself and enter energy saving modes based on battery conditions, and I have an external reset based on a repeating pulse and a monostable multivibrator that will hardware reset the system if the hardware or software fails to respond in a specific amount of time. Also the hardware is reset every 24 hours, even if working perfectly.
I have been testing the system and everything works well, every loop, every report, every condition, for over a month. The system is battery powered, and power consumption does need to be minimized. So after a month of operation I decided that during some of the extended delays (the hardware is really only active a few minutes each hour) I could shut things down. Looking around I found the Narcoleptic lib.
Implementing Narcoleptic.delay(ms) instead of delay(ms) along with a few other techniques did significantly reduce my battery consumption.
And when I did this my 24 hour hardware reset stopped functioning, it was based on resetting the system after 8640000 millis or more was detected. So I started looking at why.
I suspected that Narcoleptic must mess up millis count. So I wrote a small chunk of code to just exercise the portions of the effort that were no longer working, resetting the hardware after 30 seconds and adding some Serial.prints to aid in debugging. It seems that is the case, Narcoleptic is messing up the millis count, but it also appears as if Narcoleptic messes up Serial.prints.
The following code works:
/*
Test of millis count, print, and Narcoleptic.delay
*/
#include <Narcoleptic.h>
int KeepAlive = 12; // sets pin 12 as output for check pulses to 74123
unsigned long time;
void setup() {
pinMode(KeepAlive, OUTPUT);
Serial.begin(9600);
}
void loop()
{
Keep_Alive(); // to void that generates pulse to detect hung system
Serial.println( );
Serial.print("time = "),Serial.print(time);
Serial.println( );
delay(3000);
}
void Keep_Alive() // void that checks for activity and resets for a veriety of reason
{
digitalWrite(KeepAlive,HIGH); // Rest pulse to 74123 missing pulse detect
digitalWrite(KeepAlive,LOW);
time = millis();
if (time >= 30000)
{
Serial.println( );
Serial.print("millis >= 30000 "),Serial.print("time"),Serial.print(" = "),Serial.println(time);
Serial.print("so IF arguement worked");
Serial.println( );
delay(7000); // This stops the process for about 7 seconds, causing the 74123 to time out and hardware reset occurs
}
}
But this code does not:
/*
Test of millis count, print, and Narcoleptic.delay
*/
#include <Narcoleptic.h>
int KeepAlive = 12; // sets pin 12 as output for check pulses to 74123
unsigned long time;
void setup() {
pinMode(KeepAlive, OUTPUT);
Serial.begin(9600);
}
void loop()
{
Keep_Alive(); // to void that generates pulse to detect hung system
Serial.println( );
Serial.print("time = "),Serial.print(time);
Serial.println( );
Narcoleptic.delay(3000);
}
void Keep_Alive() // void that checks for activity and resets for a veriety of reason
{
digitalWrite(KeepAlive,HIGH); // Rest pulse to 74123 missing pulse detect
digitalWrite(KeepAlive,LOW);
time = millis();
if (time >= 30000)
{
Serial.println( );
Serial.print("millis >= 30000 "),Serial.print("time"),Serial.print(" = "),Serial.println(time);
Serial.print("so IF arguement worked");
Serial.println( );
Narcoleptic.delay(7000); // This stops the process for about 7 seconds, causing the 74123 to time out and hardware reset occurs
}
}
The only difference between them is that in the second example delay(ms) is replaced with Narcoleptic.delay(ms). Not only is the millis tracking messed up, that I can kind of understand (but am looking for a way around), but the Serial.prints are also messed up.
Two questions.
The first is just for my information, can anyone explain what is happening here to millis and to Serial.prints?
The second is, does anyone have a method to delay similar to Narcoleptic delay that reduces power used during the delay, but that does not kill the millis count?
Thanks much,
T!