My partner and I were able to write a txt file that recorded when we were and were not pushing a button down. We now want to read this file and make an LED blink when the txt file is read for "Button is pressed" and we want to make the LED turn off when the txt file is read for "Button is not pressed". We can get the txt file to print with all the correct information in it, but we cannot get the LED to turn on after everything we have tried.
We have tried using all the suggestions on the Python for Beginners page (https://www.pythonforbeginners.com/files/reading-and-writing-files-in-python) and String Comparison (String comparison in Python: is vs. ==). We have also tried using a string but we don't know if we implemented it the correct way. We also tried using this problem for guidance (Python Read file to turn on LED: String comparison in Python: is vs. ==). This is the code we have mainly been coming back to:
from gpiozero import LED
from time import sleep
blue = LED(17)
file = open("/home/pi/Documents/buttonfile.txt","r")
while True:
myfile = (file.readline())
sleep(1)
print (myfile)
if myfile == ("Button is pressed"):
print (myfile)
blue.on()
sleep (1)
else:
blue.off()
file.close()
From this, we are getting the Raspberry Pi to print the correct information that is in the .txt file, but the LED is doing nothing.