2017-06-04 89 views
0

我写了一个闪烁链接到Python的指示灯的示例代码。该代码不会引发任何错误,但LED不闪烁。有什么建议么?闪烁通过Arduino-Python链接的指示灯

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; 
} 
} 

回答

2

在Arduino的代码,你叫

digitalWrite(50,LOW); 

digitalWrite(255,HIGH); 

但digitalWrite的第一个参数是引脚编号,您将其定义为引脚10.只需将50和255更改为10,因为这是您希望将低电平信号和高电平信号输出到的位置。

+0

非常感谢。 –