I am using pwm.write. with npulses=1 in a loop to write a number of pulses to an output pin all with different periods. For testing purposes the values I’m writing add up to 8 seconds (so I can see what’s happening in human time). According to the documentation pwm.write with positive npulses is blocking, so according to my understanding it should pause the program for 8 seconds. However it doesn’t seem to do this. What I get is this.
The loop completed in 21 milliseconds, not the 8000 I was expecting.
Here’s the code.
# test something # Created at 2020-03-21 01:09:13.603035 ########### import timers t = timers.timer() t.start() import pwm import streams streams.serial() print(" here we go...") PPMout = D15.PWM PPMtrig = D14 # D14 is physically connected to D15 with a connecting wire pinMode(PPMout, OUTPUT_PUSHPULL) pinMode(PPMtrig, INPUT_PULLUP) pinMode(LED0, OUTPUT) chan = [100, 200, 300, 400, 500, 600, 700, 800] def PPMgen(): print("PPMgen started at", t.get()) syncPulse = 0 for val in chan: syncPulse += val + 300 pwm.write(PPMout, val+300, 300, MILLIS, 1) print("chan val", val) pwm.write(PPMout, 8000-syncPulse, 300, MILLIS, 1) print("syncPulse", 8000-syncPulse) print("Leaving PPMgen at", t.get()) while True: print ("entering loop at", t.get()) PPMgen() digitalWrite(LED0, digitalRead(PPMout)) sleep(100) print ("back in main loop after PPMgen at", t.get())
All advice and guidance gratefully received.
PS I have this working fine using a Timer, but unfortunately timers only work in milliseconds, and a I need microseconds.