Hello,
now I wrote a sketch like described in my previous posts.
Switch a LED on if amplitude from filtered signal reaches an arbitrary level:
// Arduino Signal Filtering and LED on if filter amplitude reaches an arbitrary threshold.
// 2014
// Arduino Signal Filtering Library
// Copyright 2012-2013 Jeroen Doggen (jeroendoggen@gmail.com)
//http://jeroendoggen.github.io/Arduino-signal-filtering-library/
//http://www.schwietering.com/jayduino/filtuino/
#include <SignalFilter.h> // Filter Library
SignalFilter Filter;
//const int analogPin = A0; // pin that the sensor is attached to
const int ledPin = 13; // pin that the LED is attached to
const int threshold = 1000; // an arbitrary threshold level that's in the range of the analog input
unsigned long time_start; // variable for time measurement start
unsigned long time_sampl; // variable for time measurement sample
unsigned int data1;
unsigned int data2;
int incomingByte = 0;
void setup()
{
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
Serial.begin(9600);
Filter.begin();
Filter.setFilter('c'); // filter settings: c=Cebycheff b= Bessel
Filter.setOrder(2); // filter settings: order 1 or 2
}
/*
Filter characteristics are stored in: “SignalFilter.cpp”.
Current filter settings and code: for “c” “2”
/// runChebyshev: Low Pass; 2nt Order; cutoff =300Hz; sampling=5000Hz; long type 8bit;
“-1bit saturation” enabled;
_v[0] = _v[1];
_v[1] = _v[2];
long tmp = ((((data * 3009876L) >> 6) //= ( 2.2425325955e-2 * x)
+ ((_v[0] * -3295982L) >> 1) //+( -0.7858234574*v[0])
+ (_v[1] * 3557026L) //+( 1.6961221536*v[1])
)+1048576) >> 21; // round and downshift fixed point /2097152
_v[2]= (short)tmp;
return (short)((
(_v[0] + _v[2])
+2 * _v[1])); // 2^
*/
void loop()
{
//Read Analog Input... you can connect accelerometer, gyro, temperature sensor, microphone etc to the channel
// int data1 = analogRead(analogPin);
data1 = analogRead(0); //Read Analog channels
data2 = Filter.run(data1); //Read the filtered signal
time_start = micros(); //time measurement
data1 = analogRead(0); // for Analog Pin A0
data2 = Filter.run(data1); // for filter calculation
time_sampl = micros() - time_start; //time measurement math
if (Serial.available() > 0) {
incomingByte = Serial.read();
if (incomingByte == 'm') { // enter and sent "m" in serial monitor window and read time in microsec for Filter.run(data1) and analog read
Serial.print("Time Sample = ");
Serial.println(time_sampl, DEC);
}
}
/*
http://arduino.cc/en/Tutorial/IfStatement
the variable called analogValue is used to store the data collected from a Analog Pin A0.
This data is then compared to a threshold value.
If the analog value is found to be above the set threshold the LED connected to digital pin 13 is turned on.
If analogValue is found to be < threshold, the LED remains off. */
// read the value of the microphone:
// int analogValue = analogRead(analogPin);
// if the filter value is high enough, turn on the LED:
if (data2 > threshold) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin,LOW);
}
// print the analog value:
// Serial.print("In: ");
// Serial.print(data1);
// Serial.print(" - Out: ");
// Serial.println(data2);
// delay(500); // delay in between reads for stability
}
To check if the sketch works, firstly I did some test directly on A0. I set threshold level to 1000 and it works fine: If I speak in the microphone the LED turns on, if not the LED remains dark.
Then I made tests with the filtered signal. Insert data2 instead of data1 at code:
if (data2 > threshold) {
digitalWrite(ledPin, HIGH);…..
but now the LED switches on and off randomly! ![]()
I think the filter gives out incorrect numbers. But before Im goning to check this I have another question: Are the input signals correctly for filter computation? Because the microphone gives out a value off ca. 512 (1024/2) if its quite, and when it's loud a signal of 1024 or 0. In the microphone circuit is implemented a DC offset. So the microphone waves doesn't oscillate around 0V but around 2,5V. See also:
Here the schematic, of my microphone:
a plot from Maxy-B:
(see also comments on: SparkFun Electret Microphone Breakout - BOB-12758 - SparkFun Electronics)
This should be correct because you can work whit the whole wave of the input signal.
But are these signals also ok for filter calculation?