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;

  if(sampleOnCount<numOnSamples)
  {
      //if(volts>averageVal)
      //averageVal=volts;
      averageVal=alpha*volts + (1-alpha)*averageVal;
      avgLongTime=alpha*volts + (1-alpha)*avgLongTime;
      sampleOnCount++;
  }
  else
  {
      averageValPub=averageVal; //publish or return the val of averageVal before resetting
      averageVal=0;
      sampleOnCount=0;
  }

   if(sampleOffCount<numOffSamples)
  {
      if (averageValPub>currentAvg)
      {
          currentAvg=averageValPub;
          currentAvgPub=currentAvg;
      }
      sampleOffCount++;
  }
  else
  {
      sampleOffCount=0;
      currentAvg=0;
  }

   if(millis()-lastTimeStamp>10000)
   {
       Spark.publish("updateEvent", "%d",avgLongTime);
       lastTimeStamp=millis();
   }

   checkForPublish();
  //Serial.println(volts);
}

void checkForPublish()
{
   if (currentAvgPub<(ambientNoiseLevel))
   {Spark.publish("valueChange", "0");
   digitalWrite(D0,LOW); digitalWrite(D1,LOW); digitalWrite(D2,LOW);
   }
   if (currentAvgPub<(1.0)&&currentAvgPub>(ambientNoiseLevel))
   {Spark.publish((const char *)"valueChange", (const char *)"1");
   digitalWrite(D0,HIGH); digitalWrite(D1,LOW); digitalWrite(D2,LOW);
   }
   if (currentAvgPub<(1.1)&&currentAvgPub>(1.0))
   {Spark.publish((const char *)"valueChange", (const char *)"2");
   digitalWrite(D0,HIGH); digitalWrite(D1,HIGH); digitalWrite(D2,LOW);
   }
   if (currentAvgPub>(1.1))
   {Spark.publish((const char *)"valueChange", (const char *)"3");
   digitalWrite(D0,HIGH); digitalWrite(D1,HIGH); digitalWrite(D2,HIGH);
   }
}
Click to Expand

Content Rating

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

0