import RPi.GPIO as gpio
import time as zeit
import random as wuerfel

def init():
    zustand = 0
    led1 = 5
    t2 = 16
    pause = 0.01
    gpio.setwarnings(False)
    gpio.cleanup()
    gpio.setmode(gpio.BCM)
    gpio.setup(led1, gpio.OUT)
    gpio.setup(t2, gpio.IN)
    return  (led1, t2, pause, zustand)

def main():
    (led1, t2, pause, zustand) = init()
    while(True):
        if zustand == 0:
            gpio.output(led1, gpio.HIGH)
            zeit.sleep(wuerfel.randint(1,5))
            gpio.output(led1, gpio.LOW)
            t = zeit.time()
            zustand = 1
        elif zustand == 1:
            zeit.sleep(pause)
            if gpio.input(t2) == 1:
                print round(zeit.time() - t,3)
                zustand = 2
        else:
            zeit.sleep(pause)
            if gpio.input(t2) == 0:
                zustand = 0

main()
    
