serialization - 处理 Arduino 通信比较
问题描述
我的处理代码成功打印了从 Arduino 发送的字符串数据,但是我无法使用 IF/ELSE 进行比较。例如,在控制台上它打印“ic”但它不运行 if value ==“ic”代码。感谢所有帮助。
import processing.serial.*;
PImage k; // Declare variable "a" of type PImage
PImage y;
Serial myPort; // Create object from Serial class
String val;
void setup() {
size(1300, 800);
// The image file must be in the data folder of the current sketch
// to load successfully
k = loadImage("k_ray.png"); // Load the image into the program
y = loadImage("y_ray.png");
String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
}
void draw() {
if ( myPort.available() > 0)
{
val = myPort.readString();
println(val);
if (val == "ic"){
println("hop");
image(y, 398, 600);
}
else if(val == "ig"){
println("hop");
image(k, 398, 600);
}
}
}
所以它只输出“ic”和“ig”,但不输出“hop”。
这里也是 Arduino 代码:
int icount=0;
int rcount=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int bc = analogRead(A2);
int bg = analogRead(A3);
int rc = analogRead(A4);
int ic = analogRead(A5);
int ig = analogRead(A1);
if (ic < 100){
icount=icount-1;
rcount=rcount+1;
Serial.println("ic");
}
if (ig < 100){
icount=icount+1;
Serial.println("ig");
}
}
解决方案
推荐阅读
- javascript - 在javascript中从api映射第一个对象
- angular - 如何使用通过 datetimepicker 使用的 angular8 检测 formControl 的 valuechange
- python - 我试图在列表的同一列中组合日期时间和字符串
- chronicle-map - 如果值是一个类,我在创建 ChronicleMap 时为平均值设置什么?[编年史-地图]
- django - AWS 中的 Django 登录
- php - Laravel - 使用范围
- powershell - 尝试使用 Remove-ADGroupMember 时,PowerShell 中的“从服务器返回了引用”
- python - 使用混合的 int 和字符串对列表进行排序
- c++ - 为 std::string 释放内存的异常(尝试在 UE4 中使用 YOLO/Darknet)
- python - 无头铬无法检测元素(硒)