In the following code, I'm measuring the voltage measurements being inputted by a Micro Bit and storing it to a dictionary. I then have the code comparing the newest and previous data entries, and if the comparison strikes a certain limit, it returns "True". From here, once the Serial Reader function is True, the next set of code will activate.
Unfortunately, this is not occuring. Apologies if this is too extensive, but I'm not certain which part of my code is causing the issue. I've simplified it as much as possible:
<pre>
import random
import serial
import time
import threading
from threading import Thread
biology_dic = {
physics_dic = {
chemistry_dic = {
voltage_dic = {}
voltage_check = False
play = False
counter = 0
gameround = 0
totalQS = 0
correctQS = 0
change_over1 = "Don't Change"
change_over2 = "Don't Change"
change_over3 = "Don't Change"
sub_list = 'chemistry', 'physics', 'biology'
timer_message = ''
serial_count = 0
read_volts = True
serialPort = serial.Serial(port="COM3", baudrate=115200, bytesize=8, timeout=2, stopbits=serial.STOPBITS_ONE)
serialString = ""
t = 0
def checkVoltage(voltage_dic, serial_count):
serial_count = int(serial_count)
if serial_count > 1:
serial_count_old = serial_count - 1
previous_value = voltage_dic.get(serial_count_old)
current_value = voltage_dic.get(serial_count)
previous_value = int(previous_value)
current_value = int(current_value)
voltage_compare = previous_value - current_value
voltage_compare = int(voltage_compare)
if voltage_compare > 8:
print('good to go')
return True
else:
return False
def backgroundCountdown(t):
while t:
mins, secs = divmod(t, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print(timer, end="\r")
time.sleep(1)
t -= 1
return False
def backgroundSerialReader(serial_count, voltage_dic):
while read_volts == True:
if (serialPort.in_waiting > 0):
serialString = serialPort.readline()
reading = serialString.decode('Ascii')
reading = reading.strip('\r\n')
reading = reading.strip()
reading = reading.strip('voltage:')
reading = int(float(reading))
serial_count = serial_count + 1
voltage_dic[serial_count] = reading
checkVoltage(voltage_dic, serial_count)
time.sleep(1)
def foreground(chemistry_dic, biology_dic, physics_dic, counter, correctQS, totalQS):
btimer.start(t)
print(timer_message)
while btimer != False:
return totalQS, correctQS, 'done'
def askColour(t):
colour = input('What colour light did you just activate (W or B)? ')
colour_lower = colour.lower()
if colour_lower == 'w':
t = 30
timer_message = 'You have 0:30. GO!'
if colour_lower == 'b':
t = 65
timer_message = 'You have 1:05. GO!'
return True, t, timer_message
def endRound(totalQS,gameround):
if totalQS > 0:
gameround = gameround + 1
if gameround < 4:
if totalQS > 0:
proportion = correctQS / totalQS * 100
else:
proportion = 0
if proportion < 25:
message = 'Better luck in the next round!'
if proportion <= 50 and proportion < 75:
message = "You didn't do too bad!"
if proportion > 75:
message = 'You did brilliantly!'
print(f'Okay! Round {gameround} of 4 completed! Your score so far is... {counter}!')
print(f'In that round, you answered {correctQS} of a total {totalQS} correctly. {message}')
elif gameround >= 4:
print(f'Game Over! Your score in the end is... {counter}. Not bad!')
print('I hope you enjoyed the game! Have a nice day!')
exit()
class ThreadWithReturnValue(Thread):
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, *, daemon=None):
Thread.__init__(self, group, target, name, args, kwargs, daemon=daemon)
self._return = None
def run(self):
if self._target is not None:
self._return = self._target(*self._args, **self._kwargs)
def join(self):
Thread.join(self)
return self._return
bvolts = ThreadWithReturnValue(target=backgroundSerialReader, args=(serial_count, voltage_dic))
btimer = ThreadWithReturnValue(target=backgroundCountdown, args=(t))
foreground = ThreadWithReturnValue(target=foreground,
args=(chemistry_dic, biology_dic, physics_dic, counter, correctQS, totalQS))
endGame = ThreadWithReturnValue(target=endRound, args=(totalQS,gameround))
colourAsk = ThreadWithReturnValue(target=askColour, args=(t))
bvolts.start()
print('Hi! Welocme to... uh... This game! Are you ready to play? (Y/N)')
readytoplay = input('>> ')
while readytoplay != 'Y':
print("Oh. Okay. I'll wait...")
readytoplay = input('>> ')
if readytoplay == 'Y':
print('Great! Let's start!')
if bvolts == True:
colourAsk.start()
while play == True and voltage_check == True:
foreground.start()
if foreground == 'done':
endGame.start()
What I have tried:
As you might note, I've inserted a return string of 'good to go' into the checkVoltage function, just to test if any value at all is being returned. This line is indeed printed at the desired moment. However, the next code doesn't initiate.
Thanks in advance for any assistance that you may be able to offer!