Narcoleptic issues

Hello everyone,

I’m starting to play around with this library using the Narcoleptic.delay() function.
It seems to work in parts (the delay itself is working) but in the other hand the serial trows some garbage that if I just replace the Narcoleptic.delay for the standard delay doesn’t happen.

The code is fairly simple:

 #include <Narcoleptic.h>  
#include <DHT.h>                                                                                                                     
#define DHTPIN 9
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float temp;
float humi;


void setup()
{
  Serial.begin(9600);
  pinMode(7, OUTPUT);
  pinMode(9, INPUT);
  digitalWrite(7, LOW);
  Serial.println("Setup");
}

void loop() {
  Serial.println("HIGH");
  digitalWrite(7, HIGH);
  Narcoleptic.delay(2000);  
  Serial.println(dht.readTemperature());
  Serial.println("LOW");
  digitalWrite(7, LOW);
  Narcoleptic.delay(8000);  
  
}

And the serial shows:
������5!*�!5!+�5
LOW
!*�!5!+�5
LOW
!*�!5!+�5
LOW
!*�!5!+�5
LOW

Replacing the narcoplectic.delay for the standard delay I get the expected output:
Setup
HIGH
Setup
HIGH
nan
LOW
HIGH
22.00
LOW
HIGH
22.00
LOW

Any clue on what is happening ?

Thanks

Hello,

Maybe try to put Serial.begin after each delay.

I think you should report this issue on Github.

Sorry, did not help :(

Narcoleptic.delay(8000); Serial.begin(9600);

The output from the serial remains with garbage.

There is something really weird here.

If I change the order of the Serial.prints I get the temperature properly printed (but still lot of garbage going to the serial).

void loop() {
  Serial.println("HIGH");
  digitalWrite(7, HIGH);
  Narcoleptic.delay(2000);  
  delay(200);
  Serial.begin(9600);
  delay(200);
  Serial.println("LOW");
  Serial.println(dht.readTemperature()); // Replace the order of the output with the print(LOW) and I see temperature now
  digitalWrite(7, LOW);
  Narcoleptic.delay(8000);  
  delay(200);
  Serial.begin(9600);
  delay(200);
  
}

Output

�&& �j *�!5 ���� 22.00 *�!5 ���� 22.00 *�!5 LOW �&& �j *�!5

Can't figure out what can be causing this.

Try to put Serial.flush() before delays.

But do you really need this library? There are other ways.

Serial.flush(); resolved the issue :)

Super thanks, what would be your recommendation for this ? I need to put arduino to sleep for about 5min (timer) I won't have an external interruption to bring it back.

Narcoleptic gotchas: When doing a Narcoleptic.delay(), the following systems are frozen: Serial communication PWM outputs millis() ticker All timer activity

Narcoleptic is best used when nothing is going on anyway, to simulate a power-off state.

I would simulate a power off state by powering off the MCU

seems like there is nearly no good reason to use this library... so it begs the question... why are you using it?

BulldogLowell: seems like there is nearly no good reason to use this library... so it begs the question... why are you using it?

It's easy to use and saves energy. I don't see a reason not to use it.

To the OP: The reason why you were seeing garbage is that you cut power from the serial port before the message was sent completely. Serial.flush() is like a delay that waits until the serial port has finished.

lg, couka

I agree, is simple and looks effective, for sure there are other alternatives, but I have 1 cycle every 4minutes and after that nothing is done, so it works fine, and after flushing the serial before the delays the problems are resolved :-)

Thank you all for the help here !