Hi,
I've been away from processing for a long time. Coming back to do a quick o-scope project that involves plotting numbers coming from serial port. I'm using serial event handler according to an ancient post on arduino's website. The code seems to work in Processing 2, but not in processing 3 or old 1.x. In 3, the data are coming in and the line functions are called but there seems to be a lack of actual drawing so the screen is blank. What am I doing wrong?
Here is the code. Code tag seems to not work on my computer.
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
float oldinByte=0;
float oldinFloat=0;
float inFloat=0;
float trigger_threshold=0.5;
String trigger_mode="POS EDGE";
void setup ()
{
// set the window size:
size(320, 240);
//println("Height is"+height);
// List all the available serial ports
// if using Processing 2.1 or later, use Serial.printArray()
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[1], 250000);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0);
}
void draw ()
{
}
void mouseClicked()
{
if (trigger_mode=="POS EDGE") trigger_mode="AUTO";
else trigger_mode="POS EDGE";
}
void serialEvent (Serial myPort)
{
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
oldinFloat=inFloat;
inFloat = float(inString);
float inByte = map(inFloat, -10, 10, 0, height);
//println(inByte);
// draw the line:
//Trigger
if (trigger_mode=="POS EDGE")
{
if (xPos==0)
{
if(!((oldinFloat<trigger_threshold)&&(inFloat>trigger_threshold)&&(inFloat>oldinFloat))) return;
}
}
stroke(0,0,0);
line(xPos,0,xPos,height);
line(xPos+1,0,xPos+1,height);
stroke(0,255,0);
if (xPos==0)
{
line(xPos, height-inByte, xPos, height-inByte);
}
else line(xPos, height-inByte, xPos-2, height-oldinByte);
oldinByte=inByte;
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
text(trigger_mode,width-70,height-10);
xPos = 0;
//background(0);
} else {
// increment the horizontal position:
xPos+=2;
}
}
}