【发布时间】:2013-12-07 21:13:35
【问题描述】:
import processing.serial.*;
int newval = 0; //Varible for feeding values onto the end of the
Serial myPort;
String temp = "0"; //Temporary varible for pulling Strings off the serial connection
float[] vals; //Array of data to be graphed
void setup() {
//Setting up size and the serial port
size(1920,1080);
smooth();
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
//Initilizing vals
vals = new float [width];
//Setting everything in vals to 0
for (int i = 0; i < vals.length - 1;i++)
{
vals [i] = 0;
}
}
void draw() {
background(0);
//Drawing function
for (int i = 0; i < vals.length - 1; i += 1)
{
stroke(255);
strokeWeight(1);
line(i,vals[i], i+1, vals[i+1]);
}
//Push everything in vals back one slot
for (int i = 0; i < vals.length - 1; i++)
{
vals[i] = vals[i+1];
}
//Pull data from the serial connection
if ( myPort.available() > 0)
{
temp = (new String(myPort.readBytesUntil('\n'))).trim(); //Take a string off of the serial
//port and remove any spaces
newval = Integer.parseInt(temp); //Convert temp to an integer
}
vals[vals.length - 1] = newval; //Set the last space in vals to newval
println(newval); //Print newval for debugging purposes
}
上面是我正在使用的代码。
我已经连接了我的Arduino to a potentiometer,然后将其连接到一个模拟输入引脚,从而有效地创建了一个variable voltage divider。此代码成功提取了第一个整数数据,并将其绘制为连续水平线,未能获取任何其他数据。我已经检查过了,它确实输入了if (myPort.availible > 0 ),但newval 没有改变。 Arduino 表现完美,我通过 Arduino IDE 检查了串行数据,看起来表现良好,因此它一定是程序问题。请帮忙!
编辑:我一直在努力。一个建议是实现一个 serialevent 函数。这产生了相同的结果。
Arduino 代码:
#define PIN A0
void setup()
{
Serial.begin(9600);
}
void loop ()
{
Serial.print(analogRead(PIN));
Serial.print('\n');
}
新的(工作的)Arduino 代码:
#define PIN A0
void setup()
{
Serial.begin(9600);
}
void loop ()
{
Serial.print(analogRead(PIN));
Serial.print('\n');
delay(20);
}
【问题讨论】:
-
您能否也发布您的 Arduino 代码,或者至少发布您将数据写入串行的部分,以便更容易测试/调试?
标签: java serial-port arduino processing