Quantcast
Channel: Arduino - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 747

collecting, averaging & ignoring duplicate serial data

$
0
0

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


Viewing all articles
Browse latest Browse all 747

Latest Images

Trending Articles



Latest Images