Ggf. musst Du mit der Auslöseschwelle etwas spielen, also statt dem Originalcode eher so:
Code:
void low_pass_filter() {
// http://en.wikipedia.org/wiki/Low-pass_filter#Continuous-time_low-pass_filters
// I will use fixed point arithmetics with 5 decimals
const uint16_t decimal_offset = 10000;
static uint32_t smoothed = 0*decimal_offset;
const uint32_t input = digitalRead(sample_pin) * decimal_offset;
// compute N such that the smoothed signal will always reach 50% of
// the input after at most 50 samples (=50ms).
// N = 1 / (1- 2^-(1/50)) = 72.635907286
const uint16_t N = 72;
smoothed = ((N-1) * smoothed + input) / N;
// introduce some hysteresis
static uint8_t square_wave_output = 0;
if ((square_wave_output == 0) == (smoothed >= decimal_offset/3)) {
// smoothed value more >= 33%
// ==> switch output
square_wave_output = 1-square_wave_output;
// ==> max the smoothed value in order to introduce some
// hysteresis, this also ensures that there is no
// "infinite memory"
smoothed = square_wave_output? decimal_offset: 0;
}
digitalWrite(filtered_pin, square_wave_output);
}
// http://en.wikipedia.org/wiki/Low-pass_filter#Continuous-time_low-pass_filters
// I will use fixed point arithmetics with 5 decimals
const uint16_t decimal_offset = 10000;
static uint32_t smoothed = 0*decimal_offset;
const uint32_t input = digitalRead(sample_pin) * decimal_offset;
// compute N such that the smoothed signal will always reach 50% of
// the input after at most 50 samples (=50ms).
// N = 1 / (1- 2^-(1/50)) = 72.635907286
const uint16_t N = 72;
smoothed = ((N-1) * smoothed + input) / N;
// introduce some hysteresis
static uint8_t square_wave_output = 0;
if ((square_wave_output == 0) == (smoothed >= decimal_offset/3)) {
// smoothed value more >= 33%
// ==> switch output
square_wave_output = 1-square_wave_output;
// ==> max the smoothed value in order to introduce some
// hysteresis, this also ensures that there is no
// "infinite memory"
smoothed = square_wave_output? decimal_offset: 0;
}
digitalWrite(filtered_pin, square_wave_output);
}
In Hardware brauchst Du stattdessen einfach einen Kondensator und einen Widerstand. Wie man die Zeitkonstanten ausrechnet findest Du im Netz ("RC Filter").

