I recently started playing with a JSN-SR04T and flashed the following code to my photon to test it out. Since then, my photon is connected to cloud for a few seconds (breathing cyan) then goes network only (breathing green). Any time I try to flash new code to it, it times out. My event history shows the device come online for 30s and then offline and back online.
/*
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 aoout 10 Feet. Still Testing
*/
volatile int StartMeasurement = D6;
volatile int EchoPin = D7;
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);
}
void loop() {
int duration = 0;
float distanceInches = 0.0;
// get a distance measurement (might have to retry a few times -- hardware has been inconsistent)
duration = attemptDistanceUntilSuccess();
Serial.print("Duration in microseconds: ");
Serial.println(duration);
// empirical conversion, your sensor may be different !
distanceInches = duration / 127.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(" ");
// make a new measurement about 4 times per second
delay(250);
}
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();
}