I am writing a class that filters out some values, for better readings and its having some issues. The original sketch works perfect with one mic, but alas I need several, so I am starting with two and hence need a class.
here is the original:
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
void setup()
{
Serial.begin(9600);
pinMode(12, OUTPUT);
}
void loop()
{
unsigned long startMillis= millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
// collect data for 50 mS
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(0);
if (sample < 1024) // toss out spurious readings
{
if (sample > signalMax)
{
signalMax = sample; // save just the max levels
}
else if (sample < signalMin)
{
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
double volts = (peakToPeak * 3.3) / 1024; // convert to volts
if(volts > 0.16){
digitalWrite(0, HIGH);
}
else { digitalWrite(0, LOW); }
Serial.println(volts);
}
and here is the one with classes I am writing now:
main.ino:
#include "mic.h"
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
const int threshold = 0.07;
Mic micOne;
Mic micTwo;
void setup()
{
Serial.begin(9600);
//pinMode(11, OUTPUT);
//pinMode(12, OUTPUT);
micOne.setPin(0);
micTwo.setPin(1);
}
void loop()
{
micOne.setMicLevel();
micTwo.setMicLevel();
Serial.print(micOne.getMicLevel());
Serial.print(", ");
Serial.println(micTwo.getMicLevel());
}
mic.h:
#ifndef MIC_H
#define MIC_H
class Mic {
public:
Mic();
int pin;
void setPin(int);
void setMicLevel();
double getMicLevel();
double currentMicLevel;
double volts;
private:
unsigned long startMillis;
unsigned int peakToPeak; // peak-to-peak level
unsigned int signalMax;
unsigned int signalMin;
};
#endif
mic.ino:
#include "mic.h"
Mic::Mic(){
currentMicLevel = 0;
volts = 0;
}
void Mic::setPin(int _pin) {
pin = _pin;
Serial.print("pin set to: "); Serial.println(pin);
}
void Mic::setMicLevel() {
unsigned long startMillis= millis();
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
// collect data for 50 mS
//
while (millis() - startMillis < sampleWindow)
{
//Serial.println("test");
sample = analogRead(pin);
//Serial.println(sample);
if (sample < 1024) // toss out spurious readings
{
if (sample > signalMax)
{
signalMax = sample; // save just the max levels
}
else if (sample < signalMin)
{
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
volts = (peakToPeak * 3.3) / 1024; // convert to volts
}
double Mic::getMicLevel(){
return volts;
}
I am getting some really weird output from it that doesn't seem to make sense here is idle reading:
0.15, 0.18
0.14, 0.15
0.19, 0.17
0.15, 0.14
0.11, 0.16
0.15, 0.18
0.17, 0.22
0.17, 0.13
0.20, 0.15
0.14, 0.17
0.14, 0.21
0.19, 0.23
then a reading from when mics are peaking:
3.30, 3.29
3.29, 3.30
3.30, 3.30
3.30, 3.30
3.29, 3.29
3.30, 3.29
3.30, 3.30
3.29, 3.30
3.30, 3.30
Problem is that they seem to both be linked, when one goes up they both do, in example above I am blowing into only one mic.
here is how I have it wired: