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.
#define BLYNK_TEMPLATE_ID "XXX"
#define BLYNK_DEVICE_NAME "XXX"
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);
Serial.begin(9600);
Blynk.begin(auth);
timer.setInterval(60000L, sensorDataSend);
delay(5000);
}
void loop() {
Blynk.run();
timer.run();
// 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: ");
Serial.println(duration);
// 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(distanceInches);
Serial.println(" ");
Serial.print("Average in inches: ");
Serial.println(AverageDistance);
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) {
break;
}
// wait a short time before attempting again -- the primary failure mode is very slow - 187 ms echo pulse
delay(200);
}
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);
delay(5);
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
delay(20);
duration = endPulse - startPulse;
detachInterrupt(EchoPin);
return duration;
}
void ultrasonicISR(void)
{
if(digitalRead(EchoPin) == HIGH)
startPulse = micros();
else
endPulse = micros();
}
</>
edited to adhere to policy guidelines, my appologies, I am a little new at this