Here's the Arduino code:
void setup() {
Serial.begin(9600);
pinMode(7, INPUT_PULLUP);
}
void loop() {
int val = digitalRead(7);
if (val == HIGH) {
Serial.print("1");
} else {
Serial.print("0");
}
Serial.println(""); // new line
}
Python code:
import time
import serial
# setup Arduino USB communication
try:
arduinoSerialData = serial.Serial('com3', 9600)
except: # not connected/damaged
pass
while True:
if arduinoSerialData.inWaiting() > 0:
datastr = arduinoSerialData.readline()
print datastr
time.sleep(1)
WHat I want to add is, if Python program exits, or crashes, let the Arduino know somehow. More specifically turn on a LED in pin 8.