我正在处理Processing,将它连接到Arduino。
Arduino的代码在这里:
float temp;
int tempPin = 0;
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
temp = analogRead(tempPin);
temp = ((temp * 0.48828125)-32)*5/9;
Serial.println(temp);
delay(1000);
}
处理代码:
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
float maxTemp = 0;
float minTemp = 1000;
float fahrenheitTemp = 0;
void setup () {
// set the window size:
size(800, 600);
// List all the available serial ports
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0);
}
void draw () {
// everything happens in the serialEvent()
}
void serialEvent (Serial myPort) {
fill(0);
stroke(0, 0, 0);
rect(0, 0, 2000, 50);
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
fahrenheitTemp = Float.parseFloat(inString);
println(fahrenheitTemp);
if (fahrenheitTemp > maxTemp){
maxTemp = fahrenheitTemp;
}
if (fahrenheitTemp < minTemp){
minTemp = fahrenheitTemp;
}
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
println(inString);
// convert to an int and map to the screen height:
float inByte = float(inString) * 5;
inByte = map(inByte, 0, 1023, 0, height);
// draw the line:
stroke(0, 200, 255);
fill(255);
line(xPos, height, xPos, height - inByte);
textSize(48);
fill(255, 255, 255);
text(inString, 10, 50);
textSize(32);
fill(255, 0, 0);
text("Max: " + maxTemp, 180, 40);
fill(0, 0, 255);
text("Min: " + minTemp, 390, 40);
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// increment the horizontal position:
xPos++;
}
}
}
问题:在处理中运行脚本后,它只显示黑色窗口而没有绘制图形。请帮我解决这个问题,或者给我另一个解决方案。