Array commands (newbie)

Hello everyone,
I currently have a program that records the the time between button presses. I would like to save these times to an array. (there is no fixed number of times i will measure. can be 10 or up to 200). Later i want to reference this array and retrieve the average, as well as the average of the last 5 times excluding the highest and lowest times.


So my questions are:

  1. Is there a function to add a value to an array?
  2. Is there a way to retrieve the length of an array?
  3. is there a way to retrieve the max and min values of an array?

FYI: the goal is to create a timer for Rubiks cubes
Attached is my current program

cubeTimer.ino (1.92 KB)

Yes and no.

Arrays are pretty simple in C/C++. Just a row of indexed variables.

You have to write your own routines that keep track of how many values you've inserted, where to insert new values. Then you have to write more routines to scan through the array to work out the minimum / mean / maximum.

It looks like you might be thinking about a dynamically allocated array which grows when you insert values. This can also be done but isn't a great idea in a memory restricted environment like an Arduino (heap fragmentation makes them run out of memory really fast).

Best way is to define a fixed array that's big enough and stick with that.

If you need to keep mean / max / min data over an arbitrarily long period you have to aggregate the data as you go along, rather than keeping every single value.

  1. Is there a function to add a value to an array?
  2. Is there a way to retrieve the length of an array?
  3. is there a way to retrieve the max and min values of an array?
  1. You can put a value into a previously declared array but extending an array is not possible, at least not in any easy way
  2. This will give the a count of the number of elements that an array can hold as declared
int arraySize = sizeof(thearray) / sizeof(theArray[0]);
  1. No, but you can easily write such functions
  1. No, but you can easily write such functions

Could you show me what that function would look like for this array:

Servo someServos[4];

This explains why there is no built-in function to do that..

Try this..

.h file

#ifndef runningAvg_h
#define runningAvg_h

class runningAvg {
  
  public:
    runningAvg(int inNumData);
    ~runningAvg(void);
    
    float addData(float inData);
    float getAve(void);
    float getMax(void);
    float getMin(void);
    float getDelta(void);
  
  protected:
    int    maxData;     // Total amount allowed.
    int    numValues;   // The amount we have.
    int    index;       // Write the next value, here.
    float  *theValues;  // The array of values.
    float  result;      // Just in case they ask, we'll keep a copy.
};

#endif

And the .cpp file

#include "runningAvg.h"
#include <stdlib.h>

runningAvg::runningAvg(int inNumData) {
  
  theValues = (float*) malloc(sizeof(float)*inNumData);
  maxData = inNumData;
  numValues = 0;
  index = 0;
  result = 0;
}

 
runningAvg::~runningAvg(void) {
  
  free(theValues);
  theValues = NULL;
}



float runningAvg::addData(float inData) {
  
  float sum;
  
  if (numValues<maxData) {          // Early stages while filling.
    theValues[index++] = inData;    // Never been full so index must be ok.
    numValues++;
  } else {
    if (index==maxData) {           // Meaning its pointing past the array.
      index = 0;                    // Cycle around.
    }
    theValues[index++] = inData;    // And stuff the value in.
  }
  sum = 0;
  for (int i=0;i<numValues;i++) {   // We loop up to numValues but not including numValues.
    sum = sum + theValues[i];
  }
  result = sum/numValues;
  return result;
}


float runningAvg::getAve(void) { return result; }


float runningAvg::getMax(void) {
  
  float result = theValues[0];
  
  for (int i=1;i<maxData;i++) {
    if (theValues[i]>result) {
      result = theValues[i];
    }
  }
  return result;
}


float runningAvg::getMin(void) {
  
  float result = theValues[0];
  
  for (int i=1;i<maxData;i++) {
    if (theValues[i]<result) {
      result = theValues[i];
    }
  }
  return result;
}


float runningAvg::getDelta(void) { return getMax()-getMin(); }

Its like an array that already does everything you're thinking about.

Make a big one for totla and a small one for the last 'x' values.

-jim lee