当时给了两段代码,一个是arduino的一个是processing的,两个丢报错了,arduino的我改了但是不确定对不对,processing的不会改,前四张processing后两张arduino(我自己改过的,最后是商家给的使用教程








当时给了两段代码,一个是arduino的一个是processing的,两个丢报错了,arduino的我改了但是不确定对不对,processing的不会改,前四张processing后两张arduino(我自己改过的,最后是商家给的使用教程








关注引自免费微信小程序:皆我百晓生
从提供的图片来看,主要问题是缺少分号、括号不匹配以及一些逻辑错误。下面是修正后的代码:
import processing.serial.*;
PFont font;
Scrollbar scaleBar;
Serial port;
int sensor;
int IBI;
int BPM;
int[] rawY;
int[] scaledY;
int[] rate;
float zoom;
float offset;
color eggshellColor = color(255,253,248);
int heart = 0;
int pulseWindowWidth = 490;
int pulseWindowHeight = 512;
int bpmWindowWidth = 180;
int bpmWindowHeight = 340;
boolean beat = false;
void setup() {
size(700, 600);
frameRate(100);
font = loadFont("Arial-BoldMT-24.vlw");
textFont(font);
textAlign(CENTER);
rectMode(CENTER);
ellipseMode(CENTER);
scaleBar = new Scrollbar(400, 575, 180, 12, 0.5, 1.0);
rawY = new int[pulseWindowWidth];
scaledY = new int[pulseWindowWidth];
rate = new int[bpmWindowWidth];
zoom = 0.75;
for (int i = 0; i < rate.length; i++) {
rate[i] = 555;
}
for (int i = 0; i < rawY.length; i++) {
rawY[i] = height / 2;
}
println(Serial.list());
port = new Serial(this, Serial.list()[0], 115200);
port.clear();
port.bufferUntil('\n');
}
void draw() {
background(0);
noStroke();
fill(eggshellColor);
rect(255, height / 2, pulseWindowWidth, pulseWindowHeight);
rect(600, 385, bpmWindowWidth, bpmWindowHeight);
// 这里应该有从Arduino接收数据并更新Sensor的逻辑,但未给出
// 假设Sensor已经正确赋值
rawY[rawY.length-1] = (1023 - sensor) - 212;
zoom = scaleBar.getPos();
offset = map(zoom, 0.5, 1, 150, 0);
for (int i = 0; i < rawY.length-1; i++) {
rawY[i] = rawY[i+1];
float dummy = rawY[i] * zoom + offset;
scaledY[i] = constrain((int)dummy, 44, 556);
}
stroke(250, 0, 0);
noFill();
beginShape();
for (int x = 1; x < scaledY.length-1; x++) {
vertex(x + 10, scaledY[x]);
}
endShape();
}
Arduino代码中的主要问题是逻辑错误和括号不匹配。修正后的代码如下:
int pulsePin = 0;
int blinkPin = 13;
int fadePin = 5;
int fadeRate = 0;
int i;
volatile int BPM;
volatile int signal;
volatile int IBI = 600;
volatile boolean Pulse = false;
volatile boolean QS = false;
void setup() {
pinMode(blinkPin, OUTPUT);
pinMode(fadePin, OUTPUT);
Serial.begin(115200);
}
void loop() {
for(i = 0; i < 5000; i++) {
if(Serial.available() > 0) {
char data = Serial.read();
// 注意:这里假设sendDataToProcessing函数已定义
// 否则你需要提供或实现这个函数
sendDataToProcessing('s', signal);
if (QS == true) {
fadeRate = 255;
sendDataToProcessing('B', BPM);
sendDataToProcessing('Q', IBI);
QS = false;
ledFadeToBeat();
delay(20);
}
}
}
}
void ledFadeToBeat() {
fadeRate -= 15;
fadeRate = constrain(fadeRate, 0, 255);
analogWrite(fadePin, fadeRate);
}
void sendDataToProcessing(char symbol, int data) {
Serial.print(symbol);
Serial.println(data);
}
请确保在实际应用中,sendDataToProcessing函数已正确定义并实现,以确保Arduino能够正确与Processing进行通讯。此外,由于传感器数据读取和处理的具体逻辑未给出,您可能需要根据实际情况调整上述代码中的相关部分。