It happenes, when two subfunction interfere with each other.
I did a little bit of research in the last 2 days. I don’t know if I’m right, but I think a good solution to run two subroutines (subfunction) in a like simultaneous way is the use of interrupts:
I tried to understand how this project works:
“We use timer2 to regularly interrupt the CPU and call a special function to load…”
So they are already using interrupts and the processor should have enough time to calculate filter.
Consideration this I did further tests with this sketch and the filter sketch merged together:
// Arduino:
// Generating sine waves
//http://makezine.com/projects/make-35/advanced-arduino-sound-synthesis/
// Filter
// Turn a 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/
// and generate a Sinewave
/******** Load AVR timer interrupt macros ********/
//http://makezine.com/projects/make-35/advanced-arduino-sound-synthesis/
#include <avr/interrupt.h> // library for Sinewave
//Sine wave parameters
#define PI2 6.283185 // 2 * PI - saves calculating it later
#define AMP 127 // Multiplication factor for the sine wave
#define OFFSET 128 // Offset shifts wave to just positive values
// Lookup table
#define LENGTH 256 // The length of the waveform lookup table
byte wave[LENGTH]; // Storage for the waveform
#include <SignalFilter.h> // Filter Library
SignalFilter Filter;
const int ledPin = 13; // pin that the LED is attached to
const int threshold = 480; // an arbitrary threshold level that's in the range of the analog input
/* // Variable for time measurement
unsigned long time_start; // variable for time measurement start
unsigned long time_sampl; // variable for time measurement sample
int incomingByte = 0; // sets incoming Byte to 0 */
int data1; // variable for Analog In
int data2; // variable for filter
void setup()
{
// Filter Parameters
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
/* Serial.begin(9600); // used only for printing (debugging) */
Filter.begin();
Filter.setFilter('c'); // filter settings: c=Cebycheff b= Bessel
Filter.setOrder(2); // filter settings: order 1 or 2
// Sinewave Parameters
// Populate the waveform lookup table with a sine wave
for (int i=0; i<LENGTH; i++) { // Step across wave table
float v = (AMP*sin((PI2/LENGTH)*i)); // Calculate current entry
wave[i] = int(v+OFFSET); // Store value as integer
}
// Set timer1 for 8-bit fast PWM output
pinMode(9, OUTPUT); // Make timer's PWM pin an output
TCCR1B = (1 << CS10); // Set prescaler to full 16MHz
TCCR1A |= (1 << COM1A1); // PWM pin to go low when TCNT1=OCR1A
TCCR1A |= (1 << WGM10); // Put timer into 8-bit fast PWM mode
TCCR1B |= (1 << WGM12);
// Set up timer 2 to call ISR
TCCR2A = 0; // We need no options in control register A
TCCR2B = (1 << CS21); // Set prescaller to divide by 8
TIMSK2 = (1 << OCIE2A); // Set timer to call ISR when TCNT2 = OCRA2
OCR2A = 32; // sets the frequency of the generated wave (32=246.94Hz)
sei(); // Enable interrupts to generate waveform!
}
/*
Filter characteristics are stored in: “SignalFilter.cpp”.
Current filter settings and code: for “c” “2”
/// runChebyshev: Bandpass 220Hz; 1. Order; lower corner=215Hz; upper corner=225Hz; sampling=5000Hz; long type 8bit; enabled "-1 bit saturation";
_v[0] = _v[1];
_v[1] = _v[2];
long tmp = ((((data * 3360365L) >> 8) //= ( 6.2591678898e-3 * x)
+ ((_v[0] * -4141801L) >> 1) //+( -0.9874824279*v[0])
+ (_v[1] * 4009861L) //+( 1.9120508347*v[1])
)+1048576) >> 21; // round and downshift fixed point /2097152
_v[2]= (short)tmp;
return (short)((
(_v[2] - _v[0]))); // 2^
*/
void loop()
{
// Read Analog Input... you can connect accelerometer, gyro, temperature sensor, microphone etc to the channel
data1 = analogRead(0)-512; //Read Analog channel (value of microphone) and substract DC-Offset from microphone "BOB-09964"
data2 = Filter.run(data1); //Read the filtered signal
// http://arduino.cc/en/Tutorial/IfStatement
// if the filter value is high enough, turn on the LED:
if (data2 > threshold) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin,LOW);
}
/*
// time measurement for debugging:
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);
}
}*/
}
//Called every time TCNT2 = OCR2A
ISR(TIMER2_COMPA_vect) { // Called each time TCNT2 == OCR2A
static byte index=0; // Points to successive entries in the wavetable
OCR1AL = wave[index++]; // Update the PWM output
asm("NOP;NOP"); // Fine tuning
TCNT2 = 6; // Timing to compensate for time spent in ISR
}