闪烁通过 Arduino-Python 链接的 LED
Blinking an LED linked through Arduino-Python
我写了一个示例代码来闪烁链接到 Python 的 LED。代码没有给我任何错误,但 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()
print('Program is done')
ser.close()
Arduino 代码:
void setup() {
Serial.begin(9600);
pinMode(10,OUTPUT);
Serial.write('1');
}
void loop() {
if(Serial.available()>0){
digitalWrite(255,HIGH);
delay(50);
digitalWrite(50,LOW);
delay(50);
digitalWrite(255,HIGH);
delay(50);
digitalWrite(50,LOW);
delay(50);
digitalWrite(255,HIGH);
delay(50);
digitalWrite(50,LOW);
Serial.read();
}
else{
Serial.available()==0;
}
}
在arduino代码中,你调用
digitalWrite(50,LOW);
和
digitalWrite(255,HIGH);
但 digitalWrite 的第一个参数是引脚号,您定义为引脚 10。只需将 50 和 255 更改为 10,因为这是您希望低信号和高信号输出到的位置。
我写了一个示例代码来闪烁链接到 Python 的 LED。代码没有给我任何错误,但 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()
print('Program is done')
ser.close()
Arduino 代码:
void setup() {
Serial.begin(9600);
pinMode(10,OUTPUT);
Serial.write('1');
}
void loop() {
if(Serial.available()>0){
digitalWrite(255,HIGH);
delay(50);
digitalWrite(50,LOW);
delay(50);
digitalWrite(255,HIGH);
delay(50);
digitalWrite(50,LOW);
delay(50);
digitalWrite(255,HIGH);
delay(50);
digitalWrite(50,LOW);
Serial.read();
}
else{
Serial.available()==0;
}
}
在arduino代码中,你调用
digitalWrite(50,LOW);
和
digitalWrite(255,HIGH);
但 digitalWrite 的第一个参数是引脚号,您定义为引脚 10。只需将 50 和 255 更改为 10,因为这是您希望低信号和高信号输出到的位置。