Smoothing analog sensor values: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Leoni (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
Leoni (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
||
| (5 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) | |||
| Zeile 1: | Zeile 1: | ||
If the sensor value on your analog pin has some unwanted amplitudes, you can smooth it with some tricks. | If the sensor value on your analog pin has some unwanted amplitudes, you can smooth it with some tricks. | ||
=smoothing analog sensor values #1 = | ==smoothing analog sensor values #1== | ||
Tremors can come from fluctuating power supply, interference or a circuit inside the Arduino chip. | Tremors can come from fluctuating power supply, interference or a circuit inside the Arduino chip. | ||
| Zeile 8: | Zeile 8: | ||
>> https://www.youtube.com/watch?v=AsQ4G1lckhY | >> https://www.youtube.com/watch?v=AsQ4G1lckhY | ||
< | <source lang="js" line start="2" highlight="4-6"> | ||
int sensorValue | |||
int sensorValue; | |||
void setup(){ | void setup(){ | ||
| Zeile 17: | Zeile 18: | ||
void loop(){ | void loop(){ | ||
sensorValue = analogRead(A0); | sensorValue = analogRead(A0); | ||
smoothValue = 0.6 | smoothValue = 0.6 * smoothValue + 0.4 * sensorValue; //you can tray with the values you multiply. It is just always under 1 | ||
Serial.print(sensorValue); | Serial.print(sensorValue); | ||
Serial.print("\t"); //TAB | Serial.print("\t"); //TAB | ||
| Zeile 24: | Zeile 25: | ||
</source> | </source> | ||
==smoothing analog sensor values #2: running average== | |||
To smooth your sensor value, a simple line of code you can add to calculate the running average is: | |||
averageSensorValue = (averageSensorValue * (average-1) + currentSensorValue) / average; | |||
>> https://www.kobakant.at/DIY/?p=7968 | |||
Version vom 24. Mai 2022, 14:31 Uhr
If the sensor value on your analog pin has some unwanted amplitudes, you can smooth it with some tricks.
smoothing analog sensor values #1
Tremors can come from fluctuating power supply, interference or a circuit inside the Arduino chip.
In this example, we simply take a portion of the new measurement and combine it with a portion of the value measured last time. The part of the old value is always a bit bigger than the new value and so the new value influences the measurement but we overwrite the tremor.
>> https://www.youtube.com/watch?v=AsQ4G1lckhY
int sensorValue;
void setup(){
Serial.begin(9600);
}
void loop(){
sensorValue = analogRead(A0);
smoothValue = 0.6 * smoothValue + 0.4 * sensorValue; //you can tray with the values you multiply. It is just always under 1
Serial.print(sensorValue);
Serial.print("\t"); //TAB
Serial.print(smoothValue);
}smoothing analog sensor values #2: running average
To smooth your sensor value, a simple line of code you can add to calculate the running average is: averageSensorValue = (averageSensorValue * (average-1) + currentSensorValue) / average;