Arduino code techniques: Unterschied zwischen den Versionen
Aus www.wiki.ardumower.de
(→Mean filter) |
(→Mean filter) |
||
| Zeile 2: | Zeile 2: | ||
=Mean filter= | =Mean filter= | ||
| − | Problem: you have multiple ADC measurements (battery, ultrasonic etc.) that can contain outliers. What value is the 'most seen value'? | + | *Problem: you have multiple ADC measurements (battery, ultrasonic etc.) that can contain outliers. What value is the 'most seen value'? |
| − | Solution: This code will sort and choose the center value. | + | *Solution: This code will sort and choose the center value. |
| − | Example: | + | *Example: |
| − | Measurements: 32,7,7,1,6,8,7,9,23,7,8,9,7 | + | *Measurements: 32,7,7,1,6,8,7,9,23,7,8,9,7 |
| − | Sorted: 1,6,7,7,7,7,7,8,8,9,9,23,32 | + | *Sorted: 1,6,7,7,7,7,7,8,8,9,9,23,32 |
| − | Returned: 7 (center value) | + | *Returned: 7 (center value) |
unsigned int ADCmedian(int pin, int count) { | unsigned int ADCmedian(int pin, int count) { | ||
Version vom 11. September 2014, 15:51 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 outliers. What value is the 'most seen value'?
- Solution: This code will sort and choose the center value.
- Example:
- Measurements: 32,7,7,1,6,8,7,9,23,7,8,9,7
- Sorted: 1,6,7,7,7,7,7,8,8,9,9,23,32
- Returned: 7 (center value)
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.
}