Hi!
Im working on my thesis project: Development of an electronic stethoscope with the pre-diagnostic capacity of mitral insufficiency. Part of this involves developing a code in Arduino that samples in real time an audio signal (cardiac/pulmonary sounds) and filters it depending on the mode selected. Im then trying to use a PCF8591 DAC to convert the filter digital signal to analog and send it to a speaker.
Im having trouble interfacing the DAC module with the arduino. DAC output is connected to a speaker and is just sounding like noise.
The PCF8591 is from adafruit, and the code is the following:
#include <Wire.h>
#include <TimerOne.h>
#include <Adafruit_PCF8591.h>
Adafruit_PCF8591 pcf = Adafruit_PCF8591();
#define pcf 0x48
int state = 0;
byte x[] = {0,0,0};
byte y[] = {0,0,0};
int Analog_signal = A1;
float b[] = {0.4448,0,-0.4448};
float a[] = {-1.0524,0.1104};
float w[] = {0.7071,0,-0.7071};
float z[] = {-0.3573,-0.4142};
void setup() {
pinMode (Analog_signal, INPUT);
Serial.begin(115200);
pinMode(2,INPUT);
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), changeState, RISING);
Wire.begin();
pcf.begin();
pcf.enableDAC(true);
}
void loop() {
if(state == 0){
}
if(state == 1){ //cardiaco
Timer1.initialize(500);
Timer1.attachInterrupt(filtroCardiaco);
pcf.analogWrite(y[0]);
}
if(state == 2){ //pulmonar
Timer1.initialize(500);
Timer1.attachInterrupt(filtroPulmonar);
pcf.analogWrite(y[0]);
}
}
void changeState(){
float x[] = {0,0,0};
float y[] = {0,0,0};
state = state + 1;
if(state == 3 ) {
state = 1;
}
}
void filtroCardiaco(){
x[0] = analogRead(Analog_signal);
y[0] = -a[0]*y[1] -a[1]*y[2] + b[0]*x[0] + b[2]*x[2];
y[2]=y[1];
y[1]=y[0];
x[2]=x[1];
x[1]=x[0];
Serial.println(y[0]);
}
void filtroPulmonar(){
x[0] = analogRead(Analog_signal);
y[0] = -z[0]*y[1] -z[1]*y[2] + w[0]*x[0] + w[2]*x[2];
y[2]=y[1];
y[1]=y[0];
x[2]=x[1];
x[1]=x[0];
Serial.println(y[0]);
}