from machine import pin
from time import ticks_us
relay = pin(12,pin.out)
sw = pin(18,pin.in,pin.pull_up)
isactive = false
start = ticks_ms()
while true:
if ticks-ms()-start >= 300:
if sw.value() == 0:
isactive = not is active
relay.value(is active)
start = ticks_ms()