【发布时间】:2019-06-29 16:03:04
【问题描述】:
我正在使用 HC-05 蓝牙设备和 Arduino Mega(不是重点,但我想我会包括它 )。
我想通过单击processing 中的按钮来打开和关闭 LED。我的processing 代码是:
import processing.serial.*;
Serial myPort;
String state = "Turn On";
void setup() {
size(600, 600);
myPort = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
background(255);
fill(0, 150, 150);
rectMode(CENTER);
rect(width/2, height/2, 200, 75, 50);
textSize(32);
fill(0);
text(state , width/2 - textWidth(state) / 2, height/2 + 16);
}
void mousePressed() {
if (mouseX < 400 && mouseX > 200 && mouseY < 333.5 && mouseY > 266.5) {
if (state == "Turn On") {
state = "Turn Off";
myPort.write('1');
} else {
state = "Turn On";
myPort.write('0');
}
}
}
我的 arduino 代码是:
const int ledPin = 7;
char ledState;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {
ledState = Serial.read();
}
if (ledState == '0') {
digitalWrite(ledPin, LOW);
ledState = '3';
}
if (ledState == '1') {
digitalWrite(ledPin, HIGH);
ledState = '3';
}
}
然而,LED 永远不会亮起。我知道蓝牙信号通过了,因为 Arduino 上的 RX 灯闪烁,这意味着它通过了,但灯从未亮起。
代码有什么问题?我怀疑我可能在 Arduino 程序中读取了错误的数据,除了这个方法在几天前有效......
提前致谢,干杯!
【问题讨论】:
标签: bluetooth arduino processing