友情提示:380元/半年,儿童学编程,就上码丁实验室。
本示范如何使用Python跟Arduino连结.并简单的控制LED ON/OFF.
Python 是跨平台的程序语言所以可以安装在一般计算机的windows上,也可以安装在使用Linux的系统如树梅派上。以下的范例程序是在一般的计算机windows 里执行Python程序.
使用这样的连结的好处在于分工,例如我们可以把影像识别或是语音识别建立在执行速度比较快的CPU上,如一般PC或是树梅派,而把一些如机械手臂的动作或是数据感测建立在Arduino端,透过Python 的套件Pyserial 就可以从Python端下自定义的文字命令去控制Arduino ,或是得到从Arduino端回传过来的一些传感器数值数据.
为什么选择Python?因为Python 语法简单易学但是功能强大,并且世界上网络上都有开源的原始码可以参考.例如做影像识别的OPENCV或是语音识别都可以透过Python做到,而只要再透过Pyserial 就可以控制Arduino做出如机器人方面的应用.
另外Python本身也有支持强大的网络功能,所以要做到IOT物联网的功能,如从Arduino端得到传感器的数据再透过Python传到网络服务器或是从网络端得到指令透过Python传给Arduino作一些控制也都是可以的.
Python的功能还不只如此对于统计或是科学方面上的应用也是支持很多的套件可以使用,可以说Python 是功能全面性的程序语言但是又不如C++那样复杂难懂.最主要的是网络几乎都可以找到你想要的范例程序代码而且都是开源免费的。
以下是范例的Python &Arduino程序代码。
<Source code in Python>
##=======getchar========================
def getchar():
#Returns a single character from standard input
key = getch() ##Get byte ex: b’a’
key_num=ord(key) ##convert byte to integer 97
key_chr=chr(key_num) ##convertinteger to char ‘a’
return key_num
##====================================
##======Write Serial Command toarduino============
def SerialWrite(command):
ser.write(cmd)
rv=ser.readline()
#print (rv) # Read the newest output from theArduino
print (rv.decode(“utf-8″))
sleep(1) # Delay for one tenth of a second
ser.flushInput()
##====================================
##=======Get Ready================
print(“Connecting to Arduino…..”)
for i in range (1,10):
rv=ser.readline()
print(“Loading…”)
#Debug print (rv) # Read the newest output from the Arduino
print (rv.decode(“utf-8″))
ser.flushInput()
sleep(1) # Delay for one tenth of a secon
Str=rv.decode(“utf-8″)
#Debugprint(Str[0:5])
if Str[0:5]==”Ready”:
print(“Get Arduino Ready !”)
break
##——————————————————
print(“==================================”)
##counter = 65 # “A”
##ser.write(chr(counter).encode(‘utf-8′)) #Convert the decimal number to ASCII then send it to the Arduino
cmd=”Key in theCommand”.encode(“utf-8″)
SerialWrite(cmd)
##===Get char from keyboard then send toarduino and get it back to print in screen==
while True:
chr_num = getchar()
cmd=(chr(chr_num).encode(‘utf-8′))
SerialWrite(cmd)
if chr_num==27: ##ESC
break
ser.close()
<Source code in Arduino>
//*****************************************************************************
const int LedPin12=12;
const int LedPin11=11;
const int LedPin10=10;
String Str01=””;
void setup() {
Serial.begin(115200); // set the baud rate
Serial.println(“Ready”); // print”Ready” once
pinMode(LedPin12,OUTPUT);
pinMode(LedPin11,OUTPUT);
pinMode(LedPin10,OUTPUT);
digitalWrite(LedPin12,LOW);
digitalWrite(LedPin11,LOW);
digitalWrite(LedPin10,LOW);
}
void loop() {
if(Serial.available())
{
Str01=””;
delay(1);
while(Serial.available())
{
Str01+=(char)Serial.read();
}
Serial.println(Str01); // send the data back in a new line so that it isnot all one long line
}
if (Str01[0]==’a’)
{
digitalWrite(LedPin12,HIGH);
digitalWrite(LedPin11,LOW);
digitalWrite(LedPin10,LOW);
delay(500);
digitalWrite(LedPin12,LOW);
digitalWrite(LedPin11,HIGH);
digitalWrite(LedPin10,LOW);
delay(500);
digitalWrite(LedPin12,LOW);
digitalWrite(LedPin11,LOW);
digitalWrite(LedPin10,HIGH);
delay(500);
}
if (Str01[0]==’b’)
{
digitalWrite(LedPin12,HIGH);
digitalWrite(LedPin11,LOW);
digitalWrite(LedPin10,LOW);
delay(100);
digitalWrite(LedPin12,LOW);
digitalWrite(LedPin11,HIGH);
digitalWrite(LedPin10,LOW);
delay(100);
digitalWrite(LedPin12,LOW);
digitalWrite(LedPin11,LOW);
digitalWrite(LedPin10,HIGH);
delay(100);
}
if (Str01[0]==’c’)
{
digitalWrite(LedPin12,HIGH);
digitalWrite(LedPin11,HIGH);
digitalWrite(LedPin10,HIGH);
delay(300);
digitalWrite(LedPin12,LOW);
digitalWrite(LedPin11,LOW);
digitalWrite(LedPin10,LOW);
delay(300);
}
}
有兴趣的同学可以参考Youtube上的教学影片.
https://www.youtube.com/watch?v=_l2qn8haK-k