python和arduino的接口

时间:2017-10-07 19:12:11

标签: python python-2.7 arduino

我们正在连接ardiuno和python,我们需要在不同的时间间隔之后将信号从python发送到ardiuno,因为我们使用循环来发送数据并接收数据,但我们没有从python获取信号在循环中。 但是当我们使用相同的代码时,为了在不使用循环的情况下将信号从python发送到ardiuno,它运行正常。

如何在循环中使用它 使用arduino uno

这是我们的代码

 int ra = 3;
 int ya = 4;
 int ga = 5;
 int rb = 6;
 int yb = 7;
 int gb = 8;
 int x1=0;
 int x2=0;
 int x=0;
 int led=10;

void setup() {
Serial.begin(9600);
pinMode(ra,OUTPUT);
pinMode(ya,OUTPUT);
pinMode(ga,OUTPUT);
pinMode(rb,OUTPUT);
pinMode(yb,OUTPUT);
pinMode(gb,OUTPUT);
pinMode(led,OUTPUT);
}
void loop() {
if(Serial.available()){
x2=Serial.parseInt();
 //x2++;

Serial.end();}



if ((x2>=0)&&(x2<=9))
 { 
   x=x2;
   switch(x)
   {
    case 0 :
    digitalWrite(ga,HIGH);
    digitalWrite(rb,HIGH);

    delay(8000);
    digitalWrite(ga,LOW);
    digitalWrite(ya,HIGH);
    digitalWrite(yb,HIGH);
    delay(3000);
    digitalWrite(ya,LOW);
    digitalWrite(yb,LOW);
    digitalWrite(rb,LOW);
    //digitalWrite(ra,HIGH);
    delay(50);

     digitalWrite(gb,HIGH);
    digitalWrite(ra,HIGH);

    delay(12000);
    digitalWrite(gb,LOW);
    digitalWrite(yb,HIGH);
    digitalWrite(ya,HIGH);
    delay(3000);
    digitalWrite(yb,LOW);
    digitalWrite(ya,LOW);
    digitalWrite(ra,LOW);
    delay(50);



    break;


    case 1:
    digitalWrite(ga,HIGH);
    digitalWrite(rb,HIGH);

    delay(5000);
    digitalWrite(ga,LOW);
    digitalWrite(ya,HIGH);
    digitalWrite(yb,HIGH);
    delay(3000);
    digitalWrite(ya,LOW);
    digitalWrite(yb,LOW);
    digitalWrite(rb,LOW);
    //digitalWrite(ra,HIGH);
    delay(50);
     digitalWrite(gb,HIGH);
    digitalWrite(ra,HIGH);

    delay(5000);
    digitalWrite(gb,LOW);
    digitalWrite(ya,HIGH);
    digitalWrite(yb,HIGH);
    delay(3000);
    digitalWrite(ya,LOW);
    digitalWrite(yb,LOW);
    digitalWrite(ra,LOW);
    break;

    case 2:
    digitalWrite(ga,HIGH);
    digitalWrite(rb,HIGH);

    delay(9000);
    digitalWrite(ga,LOW);
    digitalWrite(ya,HIGH);
    digitalWrite(yb,HIGH);
    delay(3000);
    digitalWrite(ya,LOW);
    digitalWrite(yb,LOW);
    digitalWrite(rb,LOW);
   delay(50);
    digitalWrite(gb,HIGH);
    digitalWrite(ra,HIGH);

    delay(9000);
    digitalWrite(gb,LOW);
    digitalWrite(ya,HIGH);
    digitalWrite(yb,HIGH);
    delay(3000);
    digitalWrite(ya,LOW);
    digitalWrite(yb,LOW);
    digitalWrite(ra,LOW);
    break;

    case 3:
    digitalWrite(ga,HIGH);
    digitalWrite(rb,HIGH);

    delay(12000);
    digitalWrite(ga,LOW);
    digitalWrite(ya,HIGH);
    digitalWrite(yb,HIGH);
    delay(3000);
    digitalWrite(ya,LOW);
    digitalWrite(yb,LOW);
    digitalWrite(rb,LOW);
    delay(50);
    digitalWrite(gb,HIGH);
    digitalWrite(ra,HIGH);

    delay(12000);
    digitalWrite(gb,LOW);
    digitalWrite(ya,HIGH);
    digitalWrite(yb,HIGH);
    delay(3000);
    digitalWrite(ya,LOW);
    digitalWrite(yb,LOW);
    digitalWrite(ra,LOW);
    break;

    case 4:
    digitalWrite(ga,HIGH);
    digitalWrite(rb,HIGH);

    delay(15000);
    digitalWrite(ga,LOW);
    digitalWrite(ya,HIGH);
    digitalWrite(yb,HIGH);
    delay(3000);
    digitalWrite(ya,LOW);
    digitalWrite(yb,LOW);
    digitalWrite(rb,LOW);
      delay(50);
      digitalWrite(gb,HIGH);
    digitalWrite(ra,HIGH);

    delay(15000);
    digitalWrite(gb,LOW);
    digitalWrite(ya,HIGH);
    digitalWrite(yb,HIGH);
    delay(3000);
    digitalWrite(ya,LOW);
    digitalWrite(yb,LOW);
    digitalWrite(ra,LOW); 
    break;

    case 5:
    digitalWrite(ga,HIGH);
    digitalWrite(rb,HIGH);


    delay(18000);
    digitalWrite(ga,LOW);
    digitalWrite(ya,HIGH);
    digitalWrite(yb,HIGH);
    delay(3000);
    digitalWrite(ya,LOW);
    digitalWrite(yb,LOW);
    digitalWrite(rb,LOW);
    delay(50);
    digitalWrite(gb,HIGH);
    digitalWrite(ra,HIGH);


    delay(18000);
    digitalWrite(gb,LOW);
    digitalWrite(ya,HIGH);
    digitalWrite(yb,HIGH);
    delay(3000);
    digitalWrite(ya,LOW);
    digitalWrite(yb,LOW);
    digitalWrite(ra,LOW);
    break;

    case 6:
    digitalWrite(ga,HIGH);
    digitalWrite(rb,HIGH);

    delay(2100);
    digitalWrite(ga,LOW);
    digitalWrite(ya,HIGH);
    digitalWrite(yb,HIGH);
    delay(3000);
    digitalWrite(ya,LOW);
    digitalWrite(yb,LOW);
    digitalWrite(rb,LOW);
   delay(50);
   digitalWrite(gb,HIGH);
    digitalWrite(ra,HIGH);

    delay(2100);
    digitalWrite(gb,LOW);
    digitalWrite(yb,HIGH);
    digitalWrite(ya,HIGH);
    delay(3000);
    digitalWrite(yb,LOW);
    digitalWrite(ya,LOW);
    digitalWrite(ra,LOW);

    break;

    case 7:
    digitalWrite(ga,HIGH);
    digitalWrite(rb,HIGH);

    delay(24000);
    digitalWrite(ga,LOW);
    digitalWrite(ya,HIGH);
    digitalWrite(yb,HIGH);
    delay(3000);
    digitalWrite(ya,LOW);
    digitalWrite(yb,LOW);
    digitalWrite(rb,LOW);
   delay(50);
   digitalWrite(gb,HIGH);
    digitalWrite(ra,HIGH);

    delay(24000);
    digitalWrite(gb,LOW);
    digitalWrite(yb,HIGH);
    digitalWrite(ya,HIGH);
    delay(3000);
    digitalWrite(yb,LOW);
    digitalWrite(ya,LOW);
    digitalWrite(ra,LOW);


    break;

    case 8:
    digitalWrite(ga,HIGH);
    digitalWrite(rb,HIGH);

    delay(28000);
    digitalWrite(ga,LOW);
    digitalWrite(ya,HIGH);
    digitalWrite(yb,HIGH);
    delay(3000);
    digitalWrite(ya,LOW);
    digitalWrite(yb,LOW);
    digitalWrite(rb,LOW);
   delay(50);
   digitalWrite(gb,HIGH);
    digitalWrite(ra,HIGH);

    delay(28000);
    digitalWrite(gb,LOW);
    digitalWrite(yb,HIGH);
    digitalWrite(ya,HIGH);
    delay(3000);
    digitalWrite(yb,LOW);
    digitalWrite(ya,LOW);
    digitalWrite(ra,LOW);
    break;

    case 9:
    digitalWrite(ga,HIGH);
    digitalWrite(rb,HIGH);

    delay(30000);
    digitalWrite(ga,LOW);
    digitalWrite(ya,HIGH);
    digitalWrite(yb,HIGH);
    delay(3000);
    digitalWrite(ya,LOW);
    digitalWrite(yb,LOW);
    digitalWrite(rb,LOW);
   delay(50);
   digitalWrite(gb,HIGH);
    digitalWrite(ra,HIGH);

    delay(30000);
    digitalWrite(gb,LOW);
    digitalWrite(yb,HIGH);
    digitalWrite(ya,HIGH);
    delay(3000);
    digitalWrite(yb,LOW);
    digitalWrite(ya,LOW);
    digitalWrite(ra,LOW);
     break;
     }}
  }     

1 个答案:

答案 0 :(得分:0)

在使用循环函数内部的Serial.end()从中读取数据后,您正在关闭Arduino的串行端口。 如果删除/注释Serial.end()语句,您将继续在Arduino的串行端口上接收数据。

参考https://www.arduino.cc/en/Serial/End

另外,我相信你在Python中使用pyserial库进行串行通信(它让我们的生活变得更轻松):))

告诉我们是否有效。