Arduino | 第二個Arduino程式:使用Serial port(串口通訊)
第二個Arduino程式
第二個Arduino程式,只要有電腦、Arduino開發板和燒錄線即可,透過燒錄線將Arduino開發板連接至電腦,達成serial port通訊的功能。程式碼
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
時間:2018/05/25 | |
作者:黑修斯 (陳建仲) | |
版本:v01 | |
說明:用於serial port教學說明,輸入1亮起LDE,輸入0關閉LED。5/25有整理為ez_test2的程式 | |
*/ | |
int LED = 13; //定義LED的腳位 | |
// the setup function runs once when you press reset or power the board | |
void setup() { | |
// 初始化設定 | |
pinMode(LED, OUTPUT); | |
Serial.begin(9600); //設定的鮑率 | |
Serial.println("start up!!"); //初起化完成透過Serial送出(start up!!)字串。 | |
} | |
void loop() { | |
if(Serial.available()){ //接受是否有訊號傳入 | |
int val; | |
val = Serial.read(); //將val設為傳入的數值 | |
if(val == '0'){ //如果收到字元0 ,關閉LED燈 | |
digitalWrite(LED, LOW); | |
Serial.println("LED LOW"); | |
} | |
if(val == '1'){ //如果收到字元1 ,開啟LED燈 | |
digitalWrite(LED, HIGH); | |
Serial.println("LED HIGH"); | |
} | |
} | |
} |
0 留言
不一定能即時回覆問題,有時間會盡量答覆。