I have a script that runs on an infinite loop collecting data from an Arduino and adds this information to a json file. when I stop the program with cnt c that cuts the while loop and thus but doesn't properly finish sending the data to the json file.
I have looked at other stack overflow questions. like this one "[How to stop an infinite loop safely in Python?"][1] which I used to implement in my code but it only sometimes properly terminates the json data.
my while loop looks like so :
interrupted = False
while True:
serialDataDic= ArduinoSerial(arduinoSerial,68,startTimeofData)# gather x amount of samples of data at 8.9kHz
# print(serialDataDic)
decompositionCoeffiecients=DWT(serialDataDic['voltage'],'haar',2)
# print(decompositionCoeffiecients)
#looping through each of our functions and placing that info in our dictionary to go to our json file
for key in serialDataDic:
plottingDicData[key].append(serialDataDic[key])
for key in decompositionCoeffiecients:
plottingDicData[key].append(decompositionCoeffiecients[key])
# print(plottingDicData)
#sending out sensor data into a json file
jsonfileData=JsonData(fileName,plottingDicData)
if interrupted:
print("Gotta go")
break
how do I properly terminate this while loop so it will complete the json data collection before terminating?