【processing與arduino互動編程】第二章 Processing與Arduino通信


Processing向串口發送數據

 1 import processing.serial.*;
 2 Serial port;
 3 String message;
 4 void setup() {
 5   message = "c";
 6   port = new Serial(this, "COM3", 9600);
 7 }
 8 
 9 void draw() {
10   port.write(message);
11 }

arduino連接的是com3,執行程序后Arduino板載的RX燈一直亮起,說明有數據在不停地發送過去。關閉程序后,RX燈熄滅。

2.3Arduino串口編程

通過頭文件HardwareSerial.h中定義一個HardwareSerial類的對象serial,然后直接使用類的成員函數來實現的。

Serial.begin()  用於設置串口的比特率,除以8可得到每秒傳輸的字節數

Serial.end()   停止串口通信

Serial.available()  用來判斷串口是否收到數據,該函數返回值為int型,不帶參數

示例:從串口輸出“The char I havr received:" 字符

 1 int c = 0;
 2 
 3 void setup()
 4 {
 5   Serial.begin(9600);
 6 }
 7 
 8 void loop()
 9 {
10   if (Serial.available()){
11     c = Serial.read();
12     Serial.print("The char I have received:");
13     Serial.println(c, DEC);
14   }
15   delay(200);
16 }

打開Arduino界面的串口監視器,輸入任意字符,單片機收到會返回該字符的ASCII碼。一次輸入多個字符,會返回多個ASCII碼。

 

2.4 Process與Arduino通信編程

實例一:在Processing界面上畫一個矩形,當用鼠標單擊矩形內的時候,Arduino板載的LED燈點亮,單擊矩形外的時候,Arduino板載的LED熄滅。

processing代碼

 1 import processing.serial.*;
 2 Serial port;
 3 
 4 void setup() {
 5   port = new Serial(this, "COM3", 9600);
 6   size(300, 300);
 7 }
 8 
 9 void draw() {
10   rect(100, 100, 50, 50);
11 }
12 
13 void mouseClicked() {
14   if ((mouseX >= 100) & (mouseX <= 150) & (mouseY >= 100) & (mouseY <= 150)) {
15     println("LED turn ON!");
16     port.write("a");
17   } else {
18     println("LED turn OFF!");
19     port.write("b");
20   }
21 }

Arduino代碼

int c = 0;

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
}

void loop() {
  if(Serial.available()) {
    c = Serial.read();
    if (c == 97)
      digitalWrite(13, HIGH);
    else
      digitalWrite(13, LOW);
  }
}

 實例二:將一個開關連接到Arduino上的+引腳,信號線連接在D8,長按開關,Processing上的圓形變成紅色;松開開關,Processing上的圓變成綠色。初始為綠色。

processing代碼:

 1 import processing.serial.*;
 2 Serial myPort;
 3 
 4 void setup() {
 5   size(300, 300);
 6   fill(0, 255, 0);
 7   ellipse(100, 100, 100, 100);
 8   myPort = new Serial(this, "COM3", 9600);
 9 }
10 
11 void draw() {
12   while (myPort.available() > 0) {
13     char inByte = myPort.readChar();
14     println(inByte);
15     switch (inByte) {
16       case 'a': fill(0, 255, 0);
17                 ellipse(100, 100, 100, 100);
18                 break;
19       case 'b': fill(255, 0, 0);
20                 ellipse(100, 100, 100, 100);
21                 break;
22       default: break;
23     }
24   }
25 }

Arduino代碼:

 1 boolean button;
 2 
 3 void setup() {
 4   button = false;
 5   pinMode (8, INPUT);
 6   Serial.begin(9600);
 7 }
 8 
 9 void loop() {
10   button = digitalRead(8);
11   if (button) {
12     Serial.write("a");
13   } else {
14     Serial.write("b");
15   }
16 }
17     

 實例三:在Processing里畫一個矩形,當在矩形區域單擊時,向Arduino發送字母a,Arduino收到后點亮板載的LED燈,同時向Processing發送”The light trun ON"; Processing收到后顯示在屏幕的左上角。當單擊矩形框外時,Processing向Arduino發送字母b,Arduino板載的LED燈熄滅,與此同時Arduino向Processing發送“The light turn OFF", Processing收到這串字符后顯示在屏幕的左上角。

Processing代碼:

 1 import processing.serial.*;
 2 Serial myPort;
 3 int lf = 10;
 4 String myString = null;
 5 
 6 void setup() {
 7   size (300, 300);
 8   background (125);
 9   myPort = new Serial (this, "COM3", 9600);
10   myPort.clear();
11 }
12 
13 void draw() {
14   rect(100, 100, 50, 50);
15   while (myPort.available() > 0) {
16     myString = myPort.readStringUntil(lf);
17     if (myString != null) {
18       background(125);
19       text (myString, 10, 30);
20     }
21   }
22 }
23 
24 void mouseClicked() {
25   if ((mouseX >= 100) & (mouseX <= 150) & (mouseY >= 100) & (mouseY <= 150)) {
26     myPort.write("a");
27   } else {
28     myPort.write("b");
29   }
30 }

Arduino代碼

int c = 0;

void setup() {
    Serial.begin(9600);
    pinMode (13, OUTPUT);
    digitalWrite(13, LOW);
}

void loop() {
    if (Serial.available()) {
        c = Serial.read();
        if (c == 97) {
            digitalWrite(13, HIGH);
            Serial.println("The light turn ON");
        } else {
            digitalWrite(13, LOW);
            Serial.println("The light turn OFF");
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM