collecting, averaging & ignoring duplicate serial data

edited November 2015 in Arduino

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_vk
    edited November 2015 Answer ✓
    // I think the idea is something like  this.
    // I made a fake serial input
    // don't know the kind of data you get...
    // the equality is based in int values, but maybe
    // average should be a float... 
    
    
    int numReadings = 512;
    int average = 0;
    int previousAverage = 0;
    int[] readings = new int[numReadings];
    
    
    int index = 0;
    
    
    
    void setup() {
    
      size(64, 64);
      background(30);
    }
    
    void draw() {
    
      //convert to int to add to array
      readings[index] = int(serialMockup());
    
      // so we know it is happening
      if (index%50 == 0) {
        println("receiving data [" + index +"]");
      }
    
      // next slot
      index++;
      index%=readings.length;
    
    
    
      // a full buffer
      if (index == 0) {
    
        average = average(readings);
    
        // new average?
        if (average != previousAverage) {
          println("\nDifer! New average = "+ average + "   last average = " + previousAverage);
        } else {
          //the line below shouldn't be...
          println("\naverage  "  + average + " is equal to  last average = " + previousAverage + " shoudn't print");
        }
    
        previousAverage = average;
        average = 0;
        readings = new int[numReadings];
      }
    }
    
    int average(int[] a) {
      float tot = 0;
      for (int i = 0; i < a.length; i++) {
        tot+=a[i];
      }
      return round(tot/float(a.length));
    }
    
    
    
    String serialMockup() {
      return nf(int(random(100)), 2);
    }
    
  • @_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! :)

Sign In or Register to comment.