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
}
Above is the code I'm using.
I have hooked up my Arduino to a potentiometer
and then hooked that up to one of the analog input pins, effectively creating a variable voltage divider
. This code successfully pulls the first integer piece of data, and graphs it as a continuous horizontal line, failing to get any other data. I have checked, and it does enter the if (myPort.availible > 0 )
but newval
doesn't change. The Arduino is behaving perfectly and I checked the serial data via the Arduino IDE and that appears to be behaving fine, therefore it must be a program issue. Please help!
EDIT: I have kept trying. One suggestion was to implement a serialevent function. That yielded identical results.
Arduino code:
#define PIN A0
void setup()
{
Serial.begin(9600);
}
void loop ()
{
Serial.print(analogRead(PIN));
Serial.print('\n');
}
New (working) Arduino code:
#define PIN A0
void setup()
{
Serial.begin(9600);
}
void loop ()
{
Serial.print(analogRead(PIN));
Serial.print('\n');
delay(20);
}