We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello! I have little expierence with programming and I had followed some instructions to make a light sensitive counter that would peak every time there's a sufficient change in ambient or artificial lighting. I took an example build of a heartrate sensor and adapted it for this purpose.
I was wondering If there's a way for processing to count signal position and display a calculated minute average of signals. For example 25 signals in 10 seconds multiplied by 6 to get signals per minute.
Example signal would be
The processing code i'm using is taken from an example about a heartrate sensor.
How would I go about making the code detect a signal like this and count it once it goes over a certain treshold?
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
Firstly, you would need a variable (Integer) count and a variable (Boolean) above_threshold. Then, each time serialEvent() is called, check in the end if it has gone above a certain threshold. If so, check if above_threshold is false. If so, then increment count and set above_threshold to true. Now, you also must check if the value in question has gone below the threshold. If so, then set above_threshold to false.
Try it and post your code here.
Do not issue draw commands to sketch's canvas outside the Animation Thread! [-X
From what I gather i assume it's pretty easy to do as Lord says, however I really have no programming background. The before mentioned code is taken just in order to display the peaks.
From what I could, i wrote only this, how can i to set the code to do an action after it compares if the value is above or below threshold?
This is not really anything I need for the project, it would be just a little added bonus for myself to actually automate it somewhat. Am I headed in the right direction? @Lord
Here is an idea how to count pulses. This program is an example that generates its own data. The data has a range of 0 to 60 and pulses are counted on the signal's leading edge right when the signal crosses the threshold. The threshold was set to be at 45, or at 75% of the signal.
This algorithm has been not been tested rigorously. So use it at your own risk in a way.
There are three steps you need to do to setup the PulseCounter engine:
1. Set the min,max and threshold using setYpar() function
2. Set the time resolution using setTimeRes() in Hz., the frequency of your incoming data.
3. Add data points to the PulseCounter object by using PulseConter::append() function.
Data is displayed in draw across the width of the sketch. When a sketch width is filled, the PulseCounter obj is reset (but not the calculated pulse rate measured in the window).
To get the pulse rate, there are two functions that you can call in draw():
1. getContinuousRate() is continuously updated during the sketch and reset every time the data displayed fills the width of the screen. it is an estimate and it becomes more accurate as more data is captured.
2. getFinalWinRate() is updated only after a full sketch'es width is full. Before the PulseCounter object is reset to display the next incoming data, the pulse rate in the whole window is calculated. Therefore this value is only updated after a reset is triggered by the system. This value will be more consistent to the true rate of the incoming signal.
Notice a pulse is counted only when it crosses the threshold. it has to become lower than threshold in order to be able to count a new pulse.
I hope this works for you.
Kf
Wow, really good answer from @kfrajer. Mine is just a simplistic, basic solution.