Arduino code techniques: Unterschied zwischen den Versionen

Aus www.wiki.ardumower.de
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „This page shows common code techniques that can be useful on the Arduino. =Mean filter= Problem: you have multiple ADC measurements (battery, ultrasonic etc.)…“)
(kein Unterschied)

Version vom 11. September 2014, 14:46 Uhr

This page shows common code techniques that can be useful on the Arduino.

Mean filter

Problem: you have multiple ADC measurements (battery, ultrasonic etc.) that can contain outlier. What value is the most seen value? Solution: This code will sort and choose the center value. Example: 1,5,6,7,32 => returns center value (6)

unsigned int ADCmedian(int pin, int count) {
  int values[count], last;
  uint8_t j, i = 0;
  values[0] = 0;
  while (i < count) {
    last = analogRead(pin);
    if (i > 0) {       
       for (j = i; j > 0 && values[j - 1] < last; j--) // Insertion sort loop.
   values[j] = values[j - 1]; // Shift ping array to correct position for sort insertion.
    } else j = 0;              // First ping is starting point for sort.
    values[j] = last;              // Add last ping to array in sorted position.
    i++;                       // Move to next ping.    
    if (i < count) delay(1); 
   }
   return (values[count/2]); // Return the median.
}