I really need help. I have uploaded my program to an Arduino Uno, and it works perfectly. However, when I try it on a spark (and alter Serial.print to Spark.Publish), the spark generates 3 events right after Flash and never generates another event. The events are generated regardless of the fact that they do not adhere to the if statement in the code. I have changed the if statement to make it impossible to fire and the same behavior still happens on flash. I have no idea what I am doing wrong.
What I want to have happen: I have a sound sensor listening for input, when the sound reaches a certain decibel level, I want it to fire off an event to Twillio and send a text message (Everything with the message is working fine). Here is the code:
/* Constants -----------------------------------------------------------------*/
const char webhookName[] = "twilio";
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
char message[] = "Your alarm went off";
unsigned long lastAlert = 0;
unsigned int sample;
char soundSensor=A5;
void setup()
{
pinMode(soundSensor, INPUT);
Serial.begin(9600);
}
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(soundSensor);
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 > 5) {
Spark.publish(webhookName, message);
}
else {
return;
}
}