Go Down

Topic: Mean value for wind direction (Read 337 times) previous topic - next topic

Dawiinci

#15
Jul 30, 2014, 02:07 pm Last Edit: Jul 30, 2014, 04:54 pm by Dawiinci Reason: 1
I think I have figured out a rather easy way of doing it.

Content of one function
- Initialize int array filled with zeros, size 60
- Save value in array starting at 0 and increasing by 1 every time function is called, looping from 0 to 59
- Get mode from that array every time function is called
- Calculate quality ratio (number of mode numbers)/(60)

If it just booted up the reading will be 0 (with high quality), thats ok.

Init
Code: [Select]

#define ARRAY_SIZE_VANE 60
byte readingWindVane;
int windVaneMode;
byte windVaneArray[ARRAY_SIZE_VANE] = {0};
int windVaneNumber;
double timesVane;


Will be run every second:
Code: [Select]
void getModeWind() {
  int val;
  val = analogRead(PIN_VANE);
  val >>= 2;
  byte readingWindVane = val;

  windVaneArray[windVaneNumber] = readingWindVane;
  windVaneNumber++;
  if (windVaneNumber > ARRAY_SIZE_VANE) {
    windVaneNumber = 0;
  }

  int* ipRepetition = new int[ARRAY_SIZE_VANE];
  for (int i = 0; i < ARRAY_SIZE_VANE; ++i) {
    ipRepetition[i] = 0;
    int j = 0;
    bool bFound = false;
    while ((j < i) && (windVaneArray[i] != windVaneArray[j])) {
      if (windVaneArray[i] != windVaneArray[j]) {
        ++j;
      }
    }
    ++(ipRepetition[j]);
  }
  int iMaxRepeat = 0;
  for (int i = 1; i < ARRAY_SIZE_VANE; ++i) {
    if (ipRepetition[i] > ipRepetition[iMaxRepeat]) {
      iMaxRepeat = i;
    }
  }
  delete [] ipRepetition;
  windVaneMode = windVaneArray[iMaxRepeat];
  timesVane = 0; 
 
    for (int iQ = 0; iQ < ARRAY_SIZE_VANE; ++iQ) {
  if (windVaneArray[iQ] == windVaneMode) {
      timesVane++;
    }
  }
  timesVane = timesVane/ARRAY_SIZE_VANE;
}


This works. :)

Go Up