1
0
mirror of https://github.com/skaringa/water-counter.git synced 2024-11-22 01:14:24 +01:00

Low pass filter added

This commit is contained in:
Martin Kompf 2019-02-09 19:05:53 +01:00
parent 2f638128fa
commit 7eede54912

View File

@ -5,7 +5,7 @@
* and communicates with a master computer (Raspberry Pi) * and communicates with a master computer (Raspberry Pi)
* over USB serial. * over USB serial.
* Copyright 2015 Martin Kompf * Copyright 2015-19 Martin Kompf
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -29,6 +29,14 @@ const int ledOutPin = 12; // Signal LED output pin
int sensorValueOff = 0; // value read from the photo transistor when ir LED is off int sensorValueOff = 0; // value read from the photo transistor when ir LED is off
int sensorValueOn = 0; // value read from the photo transistor when ir LED is on int sensorValueOn = 0; // value read from the photo transistor when ir LED is on
int sensorValue = 0; // difference sensorValueOn - sensorValueOff
float filteredValue; // filtered sensor value
// definitions for low pass filter
#define FILTER_LEN 6
float buffer[FILTER_LEN];
int startBuf = 0;
int lenBuf = 0;
// command line // command line
#define MAX_CMD_LEN 80 #define MAX_CMD_LEN 80
@ -47,8 +55,8 @@ char mode = 'D';
char dataOutput = 'T'; char dataOutput = 'T';
// trigger state and level // trigger state and level
int triggerLevelLow; float triggerLevelLow;
int triggerLevelHigh; float triggerLevelHigh;
boolean triggerState = false; boolean triggerState = false;
// Address of trigger levels in EEPROM // Address of trigger levels in EEPROM
@ -87,8 +95,8 @@ void setTriggerLevels() {
* Read trigger levels from EEPROM * Read trigger levels from EEPROM
*/ */
void readTriggerLevels() { void readTriggerLevels() {
triggerLevelLow = (int)eeprom_read_word(&triggerLevelLowAddr); triggerLevelLow = (float)eeprom_read_word(&triggerLevelLowAddr);
triggerLevelHigh = (int)eeprom_read_word(&triggerLevelHighAddr); triggerLevelHigh = (float)eeprom_read_word(&triggerLevelHighAddr);
Serial.print("Trigger levels: "); Serial.print("Trigger levels: ");
Serial.print(triggerLevelLow); Serial.print(triggerLevelLow);
Serial.print(" "); Serial.print(" ");
@ -98,7 +106,7 @@ void readTriggerLevels() {
/** /**
* Detect and print a trigger event * Detect and print a trigger event
*/ */
void detectTrigger(int val) { void detectTrigger(float val) {
boolean nextState = triggerState; boolean nextState = triggerState;
if (val > triggerLevelHigh) { if (val > triggerLevelHigh) {
nextState = true; nextState = true;
@ -163,6 +171,24 @@ void doCommand() {
cmdComplete = false; cmdComplete = false;
} }
/*
* Low pass filter to eleminate spikes
*/
float lowpass(int value) {
if (lenBuf < FILTER_LEN) {
lenBuf++;
}
buffer[startBuf++] = (float) value;
if (startBuf >= FILTER_LEN) {
startBuf = 0;
}
float sum = 0;
for (int i = 0; i < lenBuf; ++i) {
sum += buffer[i];
}
return sum / lenBuf;
}
/** /**
* Setup. * Setup.
*/ */
@ -204,15 +230,19 @@ void loop() {
delay(10); delay(10);
// read the analog in value: // read the analog in value:
sensorValueOn = analogRead(analogInPin); sensorValueOn = analogRead(analogInPin);
sensorValue = sensorValueOn - sensorValueOff;
filteredValue = lowpass(sensorValue);
switch (dataOutput) { switch (dataOutput) {
case 'R': case 'R':
// print the raw data to the serial monitor // print the raw data to the serial monitor
Serial.println(sensorValueOn - sensorValueOff); Serial.print(sensorValue);
Serial.print(' ');
Serial.println(filteredValue, 3);
break; break;
case 'T': case 'T':
// detect and output trigger // detect and output trigger
detectTrigger(sensorValueOn - sensorValueOff); detectTrigger(filteredValue);
break; break;
} }
} }