Automation HAT timing


#1

I am trying to use the Automation HAT as a pulse generator and the timing seems to be quite erratic when I attach a scope. The code below should generate pulses every 1s and displaying the time in the debug window seems to confirm the timing but when I measure the pulses with a storage scope the timing is up to half a second out.
Sorry about the indentation. It gets stripped out when I hit post.

My code is
#!/usr/bin/env python

import time
import math
import automationhat

if automationhat.is_automation_hat():
automationhat.light.power.write(1)

lTriggers = 0
starttime = time.clock()
trigger_on = 0

while True:
time_ms = 1000.0 * (time.clock() - math.floor(time.clock()))
if trigger_on == 0:
if time_ms > 900:
trigger_on = 1
automationhat.output.one.on()
if trigger_on == 1:
if time_ms < 50:
trigger_on = 0
automationhat.output.one.off()
lTriggers = lTriggers + 1
if lTriggers >= 100:
break


#2

You can fix indentation by embedding your code in three backticks:

```
Like this!
```

Try something like this, and see if it gives you more precise timings:

while True:
    state = int(time.time()) % 2
    automationhat.output.one.write(state)
    time.sleep(0.001)