0

I am trying to use the sleep function in lieu of delay(), but when I include sleep(), I get Sweep.cpp:60:10: error: ‘sleep’ was not declared in this scope

I read some place, about #include <avr/sleep.h> but that is a fail.

#include <avr/sleep.h>
void setup() {                
}

void loop() {
  digitalWrite(13, HIGH);   // set the LED on
  sleep(1);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  sleep(1000);              // wait for a second
}

I was able to locate sleep.h:

$ locate sleep.h | grep avr
/usr/lib/avr/include/avr/sleep.h

So then I started reading avr/sleep.h, and it seems like I'm doing it right, according to the examples. So, why might this be failing?

4
  • formating baffles me in these site please help!
    – j0h
    Commented Oct 24, 2014 at 1:49
  • "I am trying to use the sleep function in leu of delay()" Why? Commented Oct 24, 2014 at 2:05
  • delay halts all other processes, I need to not do that. sample code is sample.
    – j0h
    Commented Oct 24, 2014 at 2:42
  • 1
    There are no "all other processes", there is only the rest of the code. Commented Oct 24, 2014 at 2:55

1 Answer 1

4

"sleep()" is not a function on arduino. The sleep.h file contains functions for the hardware sleep functionality used to reduce power consumption when processing isn't necessary. "delay()" is the right thing to use.

AVR only has one thread, and so there is no meaning to pausing execution by deferring to another thread. Hardware interrupts will always interrupt the current code no matter what it is doing, they are not equivalent to threads (although they can introduce similar bugs).

If you want to wait and still do other calculations you will need to write code that can wait for a time without blocking. This can be done many ways, one way being to use the arduino library function "millis()" to repeatedly check if it is time to do a certain action, and doing something else for a while if not.

Also, vanilla arduino should be using its own packaged avr lib, for example:

arduino/hardware/tools/avr/avr/include/avr/sleep.h

instead of the one located in your usr/lib directory.

1
  • The Arduino IDE as packaged by a distro will more than likely use the distro's AVR Libc, of course. Commented Oct 24, 2014 at 3:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.