Back to Parent

#define micPin A0
#define micCancelPin A4

const int sampleWindow = 10; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
unsigned int sample2;

double volts=0;
double volts2=0;

//average ON sample
const int numOnSamples=2;
int sampleOnCount=0;
double alpha=0.5; //pick a val between 0 and 1
double averageVal=0;
double averageValPub=0;
double avgLongTime=0;

//average OFF sample
const int numOffSamples=250;
int sampleOffCount=0;
double currentAvg=0;
double currentAvgPub=0;

//publish events to cloud
const double ambientNoiseLevel=0.6;

unsigned long lastTimeStamp=0;

void setup()
{
 //Serial.begin(9600);
 Spark.variable("volts",&volts,DOUBLE);
 Spark.variable("currentAvgPub",&currentAvgPub,DOUBLE);
 pinMode(D0, OUTPUT);
 pinMode(D1, OUTPUT);
 pinMode(D2, OUTPUT);
}


void loop()
{
 unsigned long startMillis= millis();  // Start of sample window
 unsigned int peakToPeak = 0;   // peak-to-peak level
  unsigned int peakToPeak2 = 0;

 unsigned int signalMax = 0;
 unsigned int signalMin = 4095; //1024

  unsigned int signalMax2 = 0;
 unsigned int signalMin2 = 4095; //1024

 // collect data for 50 mS
 while (millis() - startMillis < sampleWindow)
 {
    sample = analogRead(micPin);
    sample = analogRead(micCancelPin);
    if (sample < 4095)  // toss out spurious readings
    {
       if (sample > signalMax)
       {
          signalMax = sample;  // save just the max levels
          signalMax2= sample2;
       }
       else if (sample < signalMin)
       {
          signalMin = sample;  // save just the min levels
          signalMin2 = sample2;
       }

    }
 }
 peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
 volts = (peakToPeak * 3.3) / 4095;  // convert to volts

 peakToPeak2 = signalMax2 - signalMin2;  // max - min = peak-peak amplitude
 volts2 = (peakToPeak2 * 3.3) / 4095;  // convert to volts

 volts=volts-volts2;
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0