0

If I try to have my code sleep for an arbitrary non-integer value it appears that the sleep() function rounds to the nearest integer. For example:

GPIO.output(LED, GPIO.HIGH)
sleep(0.3)
GPIO.output(LED, GPIO.LOW)
sleep(0.3)

Results in my LED blinking in 1 second increments. Does sleep() take floating point values?

Thank you!

1
  • This is a general programming question and has nothing to do with Raspberry Pi.
    – Ingo
    Jan 25, 2020 at 18:22

1 Answer 1

2

You have not told us which sleep() function you are using.

If you are using the one in the time module then it does support fractional times.

https://docs.python.org/3/library/time.html#time.sleep

time.sleep(secs)

Suspend execution of the calling thread for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time. The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.

#!/usr/bin/env python

import time

now = time.time()

for i in range(10):
   time.sleep(0.2)
   print(time.time()-now)

Results in

$ ./st.py
0.200302839279
0.400612831116
0.600860834122
0.801117897034
1.00143289566
1.20173883438
1.4020190239
1.6023080349
1.80258703232
2.00286793709

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.