We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey Guys, bit of a noob here but if i could get help on this problem it would be greatly appreciated. I'm trying to make a graph of inString to run in three lines, one after one another, relative to the height of the canvas, and then refresh after xPos reaches bottom right corner on the third line. I tried making yPos a PVector, push/popping, and translating but I cant seem to get it to work.
import org.firmata.*; import cc.arduino.*;
import processing.serial.*;
Serial myPort; // create object from serial class String val; //data recieved from the serial port
boolean firstContact = false;
float xPos = 0; //horizontal position of the graph float yPos = height/3;
void setup() { size(400, 300); //canvas size myPort = new Serial(this, Serial.list()[0], 9600); myPort.bufferUntil('\n'); background(0); smooth(); }
void draw() { //everything happens in serialEvent }
void serialEvent(Serial myPort) {
String inString = myPort.readStringUntil('\n'); //put incoming data into a string
if (inString != null) {
inString = trim(inString); //trim whitespace
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);
println(inString);
stroke(127, 34, 255, 40); //draw the line
line(xPos, (height/3), xPos, (height/3) - (inByte/1.5));
if ( xPos> width) { //at the edge of the screen
translate(-width, height/3);
line(xPos, yPos, xPos, yPos - (inByte/1.5));
if (yPos>=height) { //at the bottom corner of the screen
yPos = 0; //back to beginning
background(0, 20); //put a veil over previous data
}
} else {
xPos++; //increment horizontal position
}
}
Answers
http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text
Since serialEvent() is run outside sketch's "Animation" Thread, it shouldn't directly draw to the canvas. More information about it: http://forum.processing.org/two/discussion/10781/java-heap-space-error
One advantage of following GoToLoop's advice is that you can decouple the serial code from the drawing code, so we can replace the serialEvent() function by another. Thus, we could run this code, even us without serial device.
Actually, from your description, I don't get what is wrong in your code.
It seems you thought that translate() would move the content of the screen, to continue drawing. It doesn't work like that (I suggest to read (again) the description of translate() in the Reference).