In following code. the code does run al individual lines. the Interval 1 line wil run between 21.00 and 21.05hr the Interval 2 line wil run between 22.00 and 22.05hr The standard pulse line wil run on al other timeframes.
Problem: the code does i.e. not hop from interval 1 -> standard pulse -> interval 2 etc. it keeps running the timeframe when the code started to run.
Can someone help me with this python time issue?
This is the code:
from __future__ import division
from datetime import datetime, time
# Import the PCA9685 module.
import Adafruit_PCA9685
now = datetime.now()
# Initialise the PWM device using the default address
pwm = Adafruit_PCA9685.PCA9685()
# Note if you'd like more debug output you can instead run:
#pwm = PWM(0x40, debug=True)
servo_min = 300 # Min pulse length out of 4096
servo_max = 600 # Max pulse length out of 4096
def setServoPulse(channel, pulse):
pulseLength = 1000000 # 1,000,000 us per second
pulseLength /= 60 # 60 Hz
print "%d us per period" % pulseLength
pulseLength /= 4096 # 12 bits of resolution
print "%d us per bit" % pulseLength
pulse *= 1000
pulse /= pulseLength
pwm.set_pwm(channel, 0, pulse)
# Set frequency to 60hz, good for servos.
pwm.set_pwm_freq(60)
while True:
if now.time() >= time(21, 00, 00) and now.time() <= time(21, 05, 0):
print "Interval 1"
pwm.set_pwm(0, 0, servo_min)
elif now.time() >= time(22, 00, 0) and now.time() <= time(22, 05, 0):
print "Interval 2"
pwm.set_pwm(0, 0, servo_min)
else:
print "Standard pulse"
pwm.set_pwm(0, 0, servo_max)
By the documentation,
datetime.now()
returns current time, so innow
variable is always stored only time you started your program. Try puttingnow = datetime.now()
atop in your while loop.