Narcoleptic.delay, millis, and prints, not playing nice together

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!

I'm using the narcoleptic library in a project. I did change the library to allow unsigned long for the delay.
My project is a barebone ATmega (or a Pro Mini with the voltage regulator and led removed), that transmits every 5 minutes the temperature, light and so on. I don't care if the interval is 4, 5 or 6 minutes.

It runs at 8MHz on three AA batteries for more than a year.

The narcoleptic library shuts the chip down, and uses the watchdog timer to wake up. That watchdog timer is very inaccurate, but that's okay for me.
It is most often used at the end of loop().
That means that all timers stop working, and millis() can not be used over the narcoleptic delay.

But if your project is like my project, why would you still want millis() after the narcoleptic delay ? If you want a delay, just get a new millis().

In a "debug" mode, I use the Serial output, and use Serial.flush() to be sure that everything is transmitted before I shut down the chip with the narcoleptic library.
For normal mode, I don't use the Serial output. If you use the Serial for the transmitter, then you have to wait or use the Serial.flush() of course.

Using sleep modes is not very useful for a Arduino Uno. There are other components on that board that still require power. Which Arduino do you use ?

Very good power saving tutorial : Gammon Forum : Electronics : Microprocessors : Power saving techniques for microprocessors

If you want to keep the timers running and still want to use a sleep mode, then use one of the 'lighter' sleeping modes. That makes it possible to write some kind of delay() function that sleeps.

Koepel:
I'm using the narcoleptic library in a project. I did change the library to allow unsigned long for the delay.
My project is a barebone ATmega (or a Pro Mini with the voltage regulator and led removed), that transmits every 5 minutes the temperature, light and so on. I don't care if the interval is 4, 5 or 6 minutes.

None of my intervals are that long, about 30 seconds is the longest I need, and often only a couple of seconds. However the tolerances are pretty much what you are saying, my 30 second delay I don't care if it is 25 or 35 seconds.

Koepel:
The narcoleptic library shuts the chip down, and uses the watchdog timer to wake up. That watchdog timer is very inaccurate, but that's okay for me.
It is most often used at the end of loop().
That means that all timers stop working, and millis() can not be used over the narcoleptic delay.

But if your project is like my project, why would you still want millis() after the narcoleptic delay ? If you want a delay, just get a new millis().

That pretty much answers that for me. millis and Narcoleptic are not simpatico.

One of my protection modes does a hardware resets of the Arduino about every 24 hours or so. To do this I looked at millis. When I was using delay(ms) I knew millis reset to zero when the hardware was reset, so I periodically, every measurement cycle, check the millis, and when they exceeded 8640000 (about 24 hours after reset) I reset the hardware again.

Like I said, everything except this 24 hour reset is working for me (and it worked fine when I was using delay(ms)), and I suspected the millis and Narcoleptic were incompatible.

So I will either need to not do this 24 hour reset, find another way to do it, or find a sleep / low power type thing that does not hurt the millis count.

Koepel:
In a "debug" mode, I use the Serial output, and use Serial.flush() to be sure that everything is transmitted before I shut down the chip with the narcoleptic library.
For normal mode, I don't use the Serial output. If you use the Serial for the transmitter, then you have to wait or use the Serial.flush() of course.

I'll give this a try. The Serial.prints I added only to debug, there are none in the normal mode of operation for this project. I would not have known about this issue if I was not trying to debug.

Koepel:
Using sleep modes is not very useful for a Arduino Uno. There are other components on that board that still require power. Which Arduino do you use ?

Very good power saving tutorial : Gammon Forum : Electronics : Microprocessors : Power saving techniques for microprocessors

If you want to keep the timers running and still want to use a sleep mode, then use one of the 'lighter' sleeping modes. That makes it possible to write some kind of delay() function that sleeps.

Right now, during development, I am using an Arduino Uno Rev 3, but my intent is to use another model for the final version. As it is I have the Arduino Uno, slightly modified, running at about 17 mA, which is quite a reduction from where it started. As it is the hardware I have works great on the solar powered battery I have, even after 4 or 5 days of clouds in mid winter I am still fine, of course reducing the power further is gravy.

Thanks for the response,

T!

sleep and millis() are not compatible.

millis() is derived from timer0 - it's overflow interrupt is used to update the count of millis (and the value of the timer is used for micros(). timer0 is clocked off the system oscillator (after prescaling). The oscillator is shut down in sleep mode powerdown (which is what people mean when they talk about putting the chip into sleep)

Reseting the Arduino for protection means that something is wrong. Can you fix that ? so the Arduino does not have to be reset anymore ?

The narcoleptic library shuts down the chip, but output pins stay the way they are and variables keep their value. You could use a variable and add all the delay by narcoleptic. Very inaccurate of course, but it will work.

y += x;
Narcoleptic.delay( x);

In an average sketch, I can cut the current for the ATmega in half by using a sleep mode instead of a delay.

// Probably only SLEEP_MODE_IDLE will work for the mode.
// Sometimes the SLEEP_MODE_ADC will work, but that is often too much sleep.
void delaySleep( unsigned long t, byte mode)
{
  unsigned long ul_startMillis = millis();
  unsigned long ul_currentMillis;

  set_sleep_mode( mode);

  do
  { 
    sleep_mode();
    ul_currentMillis = millis();
  }
  while( ul_currentMillis - ul_startMillis < t);
}

The Timer0 for millis() wakes the chip and start running the code, that is why the sleep mode is entered over and over again until enough time has elapsed. The "SLEEP_MODE_IDLE" is the "lightest" sleep mode. The narcoleptic library uses "SLEEP_MODE_PWR_DOWN" and that is the "heaviest" sleep mode.

There are very efficient DC-DC converters. But 17mA is not a lot for a (large) battery. Even a led is 20mA.

Koepel:
Reseting the Arduino for protection means that something is wrong. Can you fix that ? so the Arduino does not have to be reset anymore ?

The 24 hour reset was for several reasons external to the Arduino, but also about 10 or 12 versions of the software ago (this has been a project I have returned to off and on for the last 18 months) the Arduino would periodically stop. This seemed to always happen sometime after 14 or 15 days, and I never figured out what was causing it.

I found nothing in my code that could explain it.

So I started resetting the unit after 24 hours, then the problem never showed up. Since that time I have extensively re-written the code and I should probably just remove that function. It exist today more from momentum than anything else I think.

Koepel:
There are very efficient DC-DC converters. But 17mA is not a lot for a (large) battery. Even a led is 20mA.

In this case the battery is quite large, at 12 AH, but there is a lot besides the Arduino going on here, and I am trying to trim current usage in multiple places, including the Arduino. It all adds up

T!

Show us the sketch, and we can tell what is wrong (maybe).
Perhaps you have used a 'long' instead of an 'unsigned long' with millis().

You can't be using millis() except for timing between Narcoleptic calls.
Any timing across a Narcoleptic call has to be timed some other way to at the least account for missing millis.

Have you seen this?