【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”);
}
}
}
复制代码

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的示例代码,演示了如何使用ProcessingArduino来实现测距报警: Arduino端代码: ```arduino const int trigPin = 2; const int echoPin = 3; const int buzzerPin = 4; void setup() { Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(buzzerPin, OUTPUT); } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); long duration = pulseIn(echoPin, HIGH); // 将距离转换为厘米 float distance = duration * 0.034 / 2; Serial.println(distance); if (distance < 10) { digitalWrite(buzzerPin, HIGH); delay(500); digitalWrite(buzzerPin, LOW); delay(500); } delay(100); } ``` Processing端代码: ```java import processing.serial.*; Serial arduino; void setup() { size(200, 200); // 根据需要修改串口号和波特率 arduino = new Serial(this, "COM3", 9600); } void draw() { // 接收从Arduino发送的距离数据 if (arduino.available() > 0) { String data = arduino.readStringUntil('\n'); if (data != null) { data = data.trim(); float distance = Float.parseFloat(data); // 根据距离进行报警 if (distance < 10) { background(255, 0, 0); // 红色背景表示报警状态 } else { background(0); // 黑色背景表示正常状态 } } } } ``` 要运行这个示例,你需要将Arduino的超声波传感器连接到引脚2(trigPin)和引脚3(echoPin),蜂鸣器连接到引脚4(buzzerPin)。然后,在Arduino IDE中上传Arduino代码,并在Processing中运行Processing代码。 这个示例将检测超声波传感器返回的距离,如果距离小于10厘米,则会触发报警,屏幕背景将变为红色。如果距离大于等于10厘米,则屏幕背景将保持黑色,表示正常状态。 你可以根据需要修改代码来适应你的具体硬件和需求。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值