We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey, I'm trying to receive data from the serial port, collect a sample group ('numReadings'), average it ('average'), and print it to screen only if the new average ('average') isn't equal to the old average ('previousAverage').
Below I have some really horrible code, trying to illustrate the idea, but i'm having trouble pulling it all together and amn't 100% if this is the best way to do it.
Would really appreciate if anyone was able to offer advice!
Cheers,
Ross
import processing.serial.*;
Serial myPort;
int numReadings = 512;
int readIndex = 0;
int total = 0;
int average = 0;
int previousAverage = 0;
int[] readings = new int[numReadings];
void setup() {
size(640, 640);
background(30);
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil('n');
}
void draw() {
}
void serialEvent (Serial MyPort) {
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
String inString = myPort.readStringUntil('\n');
if (inString != null){
inString = trim(inString);
}
total = total - readings[readIndex];
readings[readIndex] = inString;
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings) {
readIndex = 0;
}
average = total / numReadings;
if (average != previousAverage) {
println(average);
}
previousAverage = average;
}
Processing 2.2.1 (i need to update i know) Windows 8
Answers
@_vk thanks so much, this is exactly what I was trying to do. Will edit the code to feed data in from the serial port.
Cheers! :)