I'm trying to read the status of a PushButton with python via pyserial.
In the loop function of Arduino Uno I have this:
void loop() {
if(digitalRead(BUTTON) == HIGH){
Serial.print("E\n");
while(1){}
}
So, if my pushbutton is pressed, it will send this message "E" via serial and then it will enter in an infinite loop so that nothing else happens.
But I want to detect this message in the python code. So this is my python code:
import serial
import time
try:
arduino = serial.Serial('/dev/ttyACM0', 9600)
except:
print "Failed to connect on /dev/ttyACM0"
try:
print arduino.readline()
while True:
if arduino.readline() == 'E':
print("Detected!\n")
except:
print ("Failed to read!")
The first print before the infinite While is working: it is printing in the ubuntu terminal "Program Initiated", which is a message that I'm sending in the void Setup function of Arduino.
Then, it gets stucked in the line:
if arduino.readline() == 'E':
I mean, I press several times my pushbutton, and nothing is happening.
Any help? Thanks!