/ / Adrunio-LEDコントロール - python、arduino

Adrunio-LEDコントロール - python、arduino

LEDを点滅させるコードを書き込もうとしていました3これはPythonのArduinoに関連しています。私はそうすることができましたが、Pythonコードが実行を停止した後でさえも、LEDは点滅し続けます。コードが添付されています、何か提案はありますか?

Pythonコード

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コード

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");

} }

回答:

回答№1は2

条件付き Serial.available() 常に(同じ古い)データが利用可能であるため、常にtrueです。バッファからデータを削除するには、データを読み込む必要があります。