from machine import Pin
from time import sleep_ms
import _thread
coil=(Pin(8,Pin.OUT),Pin(9,Pin.OUT),Pin(17,Pin.OUT),Pin(19,Pin.OUT),Pin(16,Pin.OUT))
led=Pin(16,Pin.OUT)
out_coil=((1,0,0,0,0),(0,0,1,0,1),(0,0,0,1,0),(0,1,0,0,1),(1,1,1,1,1)) #1342 led at last out
cam = Pin(2, Pin.IN,Pin.PULL_UP) # enable internal pull-up resistor
tdc = Pin(3, Pin.IN,Pin.PULL_UP) # enable internal pull-up resistor
ign = Pin(13, Pin.IN,Pin.PULL_UP) #
out_count=0
sync_achieve=0
ign_on=0
def coil_out(num):
for i in range(5):
coil[i].value(out_coil[num][i])
def ign_detect(ign):
global out_count,ign_on
if ign.value()==1 and ign_on==0:
ign_on=1
coil_out(out_count)
elif ign.value()==0 and ign_on==1:
ign_on=0
out_count+=1
if out_count>3:
out_count=0
def tdc_detect(tdc):
global out_count,sync_achieve
if tdc.value()==1:
sync_achieve=1
out_count=3# starting spark sequence
tdc.irq(handler=None)
sleep_ms(3)
ign.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=ign_detect)
tdc.irq(trigger=Pin.IRQ_RISING, handler=tdc_detect)
while sync_achieve==0:
led.value(not led.value())
sleep_ms(300)