0

I am trying to create a game on a raspberry pi using a breadboard, LEDs and a couple buttons. What I would like the code to do is turn 2 LEDs on at the same time (one for each player) then take the reaction time and average it to see who has the lowest average at the end of the round. The issue I am running into is that running the for loop the way I have it now always has to have button 1 (btn1) pressed first before moving down in the code. Is there a way to be able to read both buttons simultaneously?

from gpiozero import Button, LED
from time import time, sleep
from random import randint

btn1 = Button(27)

led1 = LED (17)

total1 = 0

btn2= Button(26)

led2 = LED(13)

total2 = 0 

for x in range(3):
    sleep(randint(1,2))
    start = time()
    led1.on()
    led2.on()
    btn1.wait_for_press()
    led1.off()
    end1 = time()
    btn2.wait_for_press()
    led2.off()
    end2 = time()
    reaction_time1 = end1-start
    reaction_time2 = end2-start
    print('PLayer 1: you took', reaction_time1,'seconds')
    total1=total1+reaction_time1
    print('Player 1 average is', total1/(x+1), 'seconds')
    reaction_time1=end1-start
    print('PLayer 2: you took', reaction_time2,'seconds')
    total2=total2+reaction_time2
    print('Player 2 average is', total2/(x+1), 'seconds')
    reaction_time2=end2-start
CS_Teach
  • 1
  • 1
  • Use [multithreading](https://www.tutorialspoint.com/python/python_multithreading.htm). You can wait for both buttons to be pressed simultaneously. – Sin Han Jinn Nov 19 '19 at 03:31
  • I think you might want to spend some quality time with the `gpiozero` documentation. For example, the [Button class](https://gpiozero.readthedocs.io/en/stable/recipes.html#button) has a `.when_pressed` method that may get you closer to what you want. – larsks Nov 19 '19 at 04:09
  • [Multiprocessing vs Threading Python](https://stackoverflow.com/questions/3044580/multiprocessing-vs-threading-python) This is the topic you're looking for. Your process is I/O bound. You *will* need to study multithreading to ensure you do it correctly, but going into all those details is probably too much for a single answer and you would get little out of someone else solving all the issues for you. – jpmc26 Nov 19 '19 at 05:00

1 Answers1

0

This should work:

from gpiozero import Button, LED
from time import time, sleep
from random import randint

def time1():
    led1.off()
    end1 = time()

def time2():
    led2.off()
    end2 = time()

btn1 = Button(27)

led1 = LED (17)

total1 = 0

btn2= Button(26)

led2 = LED(13)

total2 = 0 

for x in range(3):
    sleep(randint(1,2))
    start = time()
    led1.on()
    led2.on()
    end = 0
    end1 = 0
    end2 = 0
    while(end == 0):
        if(end1 == 0):
            btn1.when_pressed = time1
        if(end2 == 0):
            btn2.when_pressed = time2
        if(end1 != 0 and end2 != 0):
            end = 1
    reaction_time1 = end1-start
    reaction_time2 = end2-start
    print('PLayer 1: you took', reaction_time1,'seconds')
    total1=total1+reaction_time1
    print('Player 1 average is', total1/(x+1), 'seconds')
    reaction_time1=end1-start
    print('PLayer 2: you took', reaction_time2,'seconds')
    total2=total2+reaction_time2
    print('Player 2 average is', total2/(x+1), 'seconds')
    reaction_time2=end2-start

Let me know if it does.

Cheers

VTMExpor
  • 26
  • 5