Photon Constantly Cycling Online/Offline After Flashing new Code

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

Nevermind. As it turns out, when you forget to power the ultrasonic sensor, the code doesn’t like when you try to tell it to get information.

In your while loop, you never increment attempts, so you were stuck in that loop forever if your device wasn’t returning a value.

1 Like

I had the same problem with IB. But I sent my question to Technical Support and received the answer.
It’s finally works again.

This is the message from TS.

Try to do the same steps:

It is possible that the firmware on the Internet Button is using sleep modes.

The easiest way to check is to see if the device will stay on in safe mode.

Hold down the RESET and SETUP buttons at the same time. Release RESET and continue to hold down SETUP until the status LED blinks magenta (red and blue at the same time), then release SETUP.

The normal sequence on the status LED is: white, blinking green, blinking cyan (light blue), fast blinking cyan, and finally breathing cyan. In safe mode, the last step will be breathing magenta instead of breathing cyan.

If the Photon stays powered up in safe mode, you simply need to reprogram it with different user firmware. For example, you could put the Tinker software back on the Photon: