We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello! I recently got my own Arduino and decided to use it to make a pulse sensor from a tutorial. The tutorial also suggested using Processing to display peaks of pulse. I used their code but after returning few values and a peak of the graph the process stops, encountering a null value and giving the "disabling serialEvent() for COM3" error. Does anyone know how to deal with this error? I would love to learn more about it!
The code looks like this:
// Based on examples from Arduino's Graphing Tutorial and OscP5 documentation import processing.serial.*; Serial myPort; // The serial port int xPos = 1; // horizontal position of the graph float oldHeartrateHeight = 0; // for storing the previous reading
void setup () {
// set the window size:
size(600, 400);
frameRate(25);
// List available serial ports.
println(Serial.list());
// Setup which serial port to use.
// This line might change for different computers.
myPort = new Serial(this, Serial.list()[1], 9600);
// set inital background:
background(0);
}
void draw () {
}
void serialEvent (Serial myPort) {
// read the string from the serial port.
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int
println(inString);
int currentHeartrate = int(inString);
// draw the Heartrate BPM Graph.
float heartrateHeight = map(currentHeartrate, 0, 1023, 0, height);
stroke(0,255,0);
line(xPos - 1, height - oldHeartrateHeight, xPos, height - heartrateHeight);
oldHeartrateHeight = heartrateHeight;
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
} else {
// increment the horizontal position:
xPos++;
}
}
}
Answers
https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
Do not modify sketch's canvas outside the "Animation" Thread! :-@
Event functions aren't the place to draw() anything! [-X
...so, you could follow GoToLoop's advice by:
draw()
loop, either indraw()
directly by having draw call a custom function during its loop e.g.drawEvents()
and drawing there.They should really change their tutorial...
Here is one of the other threads with the same problem: https://forum.processing.org/two/discussion/comment/57867/