Help with code on board and blynk reporting

Hey Guys, i need help with some code. I am using the JSN-SR04T sensor. I understand that this is a particle board, but the programming language is very similar and this forum is more active!

What i want to do is have the sensor fire 20 burst shots over about 10 seconds or so, calculate the average, then report to serial.

what is currently happening right now is it will constantly measure and report to serial and I can't have this with Cloud IOT unless i want to chew through all my resource limits. Any help is appreciated

Hardware: Particle Argon
Wiring Schematic not needed for issue at hand, working perfectly other than firing sequence.

// This #include statement was automatically added by the Particle IDE.
char auth = "XXX";

#include <blynk.h>
#define BLYNK_PRINT Serial

BlynkTimer timer;

Code for Cheap UltraSonic JSN SR04T with PHOTON.
Attach TRIG to D6, ECHO to D7, 5v to VIN, Gnd to Gnd.

Minimum Range is about 11", Max Range is about 10 Feet. Still Testing


volatile int StartMeasurement = D3;
volatile int EchoPin = D2;
volatile unsigned long startPulse = 0;
volatile unsigned long endPulse = 0;

int attemptDistanceMeasurementOnce(void);
int attemptDistanceUntilSuccess(void);
void ultrasonicISR(void);
double AverageDistance ;

void setup() {

pinMode(StartMeasurement, OUTPUT);
pinMode(EchoPin, INPUT);
timer.setInterval(60000L, sensorDataSend);


void loop() {;;

// make a new measurement about 4 times per second


void sensorDataSend()
int duration = 0;
float distanceInches = 0.0;

// get a distance measurement (might have to retry a few times -- hardware has been inconsistent)
duration = attemptDistanceMeasurementOnce();

Serial.print("Duration in microseconds: ");

// empirical conversion, your sensor may be different !
distanceInches = duration / 142.000;

AverageDistance = (0.95 * AverageDistance) + (0.05 * distanceInches);  // 20 shot rolling average

Serial.print("Distance in inches: "); 
Serial.println(" "); 

Serial.print("Average in inches: ");
Serial.println(" "); 

Blynk.virtualWrite(V2, AverageDistance);  // sending sensor value to Blynk app

Particle.publish("waterlevel", String(AverageDistance), PRIVATE);


int attemptDistanceUntilSuccess()
int duration;
int attempts = 1;

while(attempts < 10) {
    duration = attemptDistanceMeasurementOnce();
    if(duration > 0 && duration < 60000) {
    // wait a short time before attempting again -- the primary failure mode is very slow - 187 ms echo pulse

return duration;


int attemptDistanceMeasurementOnce(void)
int duration;

endPulse = startPulse = 0;
attachInterrupt(EchoPin, ultrasonicISR, CHANGE);

// pulse the sensor to make it get a distance reading
digitalWrite(StartMeasurement, HIGH);
digitalWrite(StartMeasurement, LOW);

// wait while we get both up and down edges of pulse (and interrupts)
// the interrupt service routine sets our start and end "times" in microseconds
duration = endPulse - startPulse;

return duration;


void ultrasonicISR(void)
if(digitalRead(EchoPin) == HIGH)
startPulse = micros();
endPulse = micros();


edited to adhere to policy guidelines, my appologies, I am a little new at this :slight_smile:

Why do users choose to ignore the advice on posting code ?

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code . Use code tags when posting code here to make it easier to read and copy for examination