We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello! First time poster, new to Processing.
I was following the following tutorial on making a Pulse Sensor: https://www.youtube.com/watch?v=psTa5ZrqAyo
I was able to see output from a scope. I can therefore conclude output is being sent to the Arduino.
The code provided does not draw any pulse in Processing 3. I added a few lines, they are:
noFill();
stroke(0,255,0);
strokeWeight(4);
On lines 40-42. The rest is original code from the tutorial.
It is as follows:
// 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()[0], 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();
int currentHeartrate = int(inString);
// draw the Heartrate BPM Graph.
float heartrateHeight = map(currentHeartrate, 0, 1023, 0, height);
noFill();
stroke(0,255,0);
strokeWeight(4);
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++;
}
}
}
Any ideas what lines I need to append to get the output to display?
Any help is much appreciated, thanks in advance.
Answers
Do not "draw" outside the "Animation Thread". In your case, do not "draw" under serialEvent()! [-X
I'm not exactly sure what that means @GoToLoop
I have modified the code to be the following:
Note, last night I had better output on the screen.. today I see output but it looks like a bunch of noise (Will check the circuit again) and occasionally when I run this code it either works, or tells me I have a NulPointerException error on line
The modified code is as follows:
https://www.Reddit.com/r/processing/comments/51vdxt/trouble_with_arduino_serial_communication