Hi, I am trying to filter some values read from my Arduino and sent to my Processing sketch, everything seems to work fine except that I am getting really ugly signals that just go up and down, but only in small differences, so what I wanted to do is to tell the sketch "only change the value if it is over a +/- 50 threshold", I did this code but It doesnt seem to work:
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
void setup () {
// set the window size:
size(800, 600);
// List all the available serial ports
myPort = new Serial(this, "COM4", 115200);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0);
}
void draw () {
// everything happens in the serialEvent()
}
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null && inString.length() > 0) {
// trim off any whitespace:
inString = trim(inString);
String [] inputStringArr = split(inString, ",");
// convert to an int and map to the screen height:
float [] inByte = new float[5];
inByte[0] = float(inputStringArr[0])-600;
inByte[1] = float(inputStringArr[1])-600;
inByte[2] = float(inputStringArr[2])-600;
inByte[3] = float(inputStringArr[3])-600;
inByte[4] = float(inputStringArr[4])-600;
inByte[0] = map(inByte[0], 0, 1023, 0, height);
inByte[1] = map(inByte[1], 0, 1023, 0, height);
inByte[2] = map(inByte[2], 0, 1023, 0, height);
inByte[3] = map(inByte[3], 0, 1023, 0, height);
inByte[4] = map(inByte[4], 0, 1023, 0, height);
float [] inByteOld = new float[5];
for (int i=0; i<5; i=i+1)
{
inByteOld[i] = inByte[i];
if ((inByteOld[i] - inByte[i]) > 50 || (inByteOld[i] - inByte[i]) < -50)
{
inByteOld[i]=inByte[i];
}
else
{
inByte[i]=inByteOld[i];
}
}
// Digital filter of noise
// draw the line:
stroke(127,34,255);
line(xPos, height, xPos, height - inByte[0]);
stroke(255,0,0);
line(xPos, height, xPos, height - inByte[1]);
stroke(0,255,0);
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// increment the horizontal position:
xPos++;
}
}
}
thats the whole code, the part I think should filter this treshold is this:
for (int i=0; i<5; i=i+1)
{
inByteOld[i] = inByte[i];
if ((inByteOld[i] - inByte[i]) > 50 || (inByteOld[i] - inByte[i]) < -50)
{
inByteOld[i]=inByte[i];
}
else
{
inByte[i]=inByteOld[i];
}
}
I am not sure if that is the correct way to do it tho, just wanted some help on this
thanks!
update moderator: added code tags