/ / Adrunio-LED-Steuerung - Python, Arduino

Adrunio-LED-Steuerung - Python, Arduino

Ich habe versucht, einen Code zum Blinken einer LED zu schreiben. 3mal was mit einem Arduino von Python verbunden ist. Ich konnte dies tun, aber auch wenn der Python-Code nicht mehr läuft, blinkt die LED weiter. Codes sind beigefügt, irgendwelche Vorschläge?

Python-Code

import serial #import the pyserial library
connected = False #this will represent whether or not ardunio is connected to the system
ser = serial.Serial("COM3",9600) #open the serial port on which Ardunio is connected to, this will coommunicate to the serial port where ardunio is connected, the number which is there is called baud rate, this will provide the speed at which the communication happens
while not connected: #you have to loop until the ardunio gets ready, now when the ardunio gets ready, blink the led in the ardunio
serin = ser.read()
connected = True
ser.write("1") #this will blink the led in the ardunio
while ser.read() == "1": #now once the led blinks, the ardunio gets    message back from the serial port and it get freed from the loop!
ser.read()
ser.close()

Ardunio-Code

void setup() {
Serial.begin(9600);
pinMode(10,OUTPUT);
Serial.write("1");
}
void loop() {
if(Serial.available()>0){
digitalWrite(10,HIGH);
delay(500);
digitalWrite(10,LOW);
delay(500);
digitalWrite(10,HIGH);
delay(500);
digitalWrite(10,LOW);
delay(500);
digitalWrite(10,HIGH);
delay(500);
digitalWrite(10,LOW);
Serial.write("0");

} }

Antworten:

2 für die Antwort № 1

Der Zustand mit Serial.available() ist immer wahr, da immer (dieselben alten) Daten verfügbar sind. Sie müssen die Daten lesen, um sie aus dem Puffer zu entfernen.