Core sending 3 events on flash and failing to send more when prompted

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;
    }
    
}

@BarryAllen, the ADC on the Core/Photon is 12bits and returns a value of 0-4095, not 0-1024 like an Arduino. I am not sure I understand the if (volts > 5) since, if the calculation is correct, the maximum value you get will be 3.3 :smile:

1 Like

Dude… Thank you. I have been doing a lot of swearing at my computer. I still have one question, Why would that have caused the event to fire 3 times immediately on flash?

As an answer to your puzzle: "Why three and no more?"

The docs state

NOTE: Currently, a device can publish at rate of about 1 event/sec, with bursts of up to 4 allowed in 1 second. Back to back burst of 4 messages will take 4 seconds to recover.

When hitting the "speed limit", you're pulled over and will only be released when you stop speeding :wink:

And for the immediate fire:
Since you are considering only samples less than 1024 and I guess you initially mostly get higher ones (due to the 12bit max) your calculation of peakToPeak = signalMax - signalMin; will happen with the initial values of 0 - 1024 which will be a rather high value due to peakToPeak being unsigned.

@BarryAllen, the first publish right after flash may be the start-up publish event (I would need to see the log). As for the other 2 publishes, as @ScruffR said, unsigned substraction of 0-1024 “wraps around” giving you a large positive number. :smile: