Skip to main content
2 of 2
added 296 characters in body; edited title
Leo Ervin
  • 127
  • 2
  • 3
  • 9

make Arduino tell when the Python program stops reading from the Serial (crash, etc)

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.

Leo Ervin
  • 127
  • 2
  • 3
  • 9