繁体   English   中英

Arduino和Python

[英]Arduino and Python

我正在尝试通过串行通信控制从Python连接到Arduino的LED。 我将两个代码都附加在Arduino和python中。 但是当我在Python中运行代码时,尽管没有出现任何错误,但我没有从LED收到任何响应。 也许我在语法上犯了一些错误?

import serial
import time
arduino=serial.Serial('COM3',250000,timeout=5.0)
m=[]
commands=open('1.txt','r')
lines=commands.readlines()                  
for line in lines:                       
    m.append(line)
commands.close()
s=0
while s!=len(m):
    m[s]=float(m[s])
    s+=1
s=0

def delay():
    x=0
    y=0
    while x!=y:
        x+=1
while s!=len(m):
    c=str(m[s])
    if m[s]==1:
        arduino.write(b'c')
        time.sleep(2)
        print('1on')

    elif m[s]==-1:
        arduino.write(b'c')
        time.sleep(2)
        print('1off')
        delay()
    elif m[s]==2:
        arduino.write(b'c')
        time.sleep(2)
        print('2on')

    elif m[s]==-2:
        arduino.write(b'c')
        time.sleep(2)
        print('2off')

    elif m[s]==3:
        arduino.write(b'c')
        time.sleep(2)
        print('3on')

    elif m[s]==-3:
        arduino.write(b'c')
        time.sleep(2)
        print('3off')

    s+=1

这是从Arduino中的Python控制LED的代码。 Arduino代码在下面

int led1=2;
int led2=3;
int led3=4;
void setup()
{
  Serial.begin(250000);
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
}
void loop()
{
  if(Serial.available())
  {
    int v=Serial.parseInt();
    if(v==1)
    {
      digitalWrite(led1,HIGH);
      delay(1000);
     }
    else if(v==-1)
    {
      digitalWrite(led1,LOW);
      delay(1000);
    }
    else if(v==2)
    {
      digitalWrite(led2,HIGH);
      delay(1000);
    }
    else if(v==-2)
    {
      digitalWrite(led2,LOW);
      delay(1000);
    }
    else if(v==3)
    {
      digitalWrite(led3,HIGH);
      delay(1000);
    }
    else if(v==-3)
    {
      digitalWrite(led3,LOW);
      delay(1000);
    }
  }
}

我不了解Python,但问题似乎出在以下地方:arduino.write(b'c')。 您继续发送“ c”字符。 它不应该发送c变量中的任何内容吗?

正如Blurry Sterk所说,您要发送字符'c'而不是变量c 除此之外,您的代码有太多重复和delay功能,什么也不做

例如,您的python代码可能像这样更简单:

import serial
import time
arduino=serial.Serial('COM3',250000,timeout=5.0)
m=[]
commands=open('1.txt','r')

lines=commands.readlines()                  
for line in lines:                       
    m.append(float(line)) #Just convert to float at the moment you read it

commands.close()

for c in m:
    arduino.write(str(c).encode())
    time.sleep(2)
    print(abs(c), 'on' if c>0 else 'off') #First two lines are the same in every if sentence, the last one just prints the number of led (abs(c)) and if it's on or off depending if it's negative or positive

如果您使用LED数组并使用绝对值访问数组的索引,那么您的arduino代码也可以更容易阅读,类似于python中print语句中的逻辑

正如E先生所指出的,延迟功能没有任何作用。 也许您想要这样:

def delay(y=0):
if not isinstance(y, int):
    raise ValueError('y must be integer')
if y<0:
    raise ValueError('If y is negative the loop is infinite.')
x=0
while x!=y:
    x+=1

实际上,这将延迟指令的处理。 这是实现这一目标的最佳方法吗? 我不知道。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM