Panic, hard_fault

I’m seeing my Photon restart every few minutes with a Log message of “panic, hard_fault”, when it restarts.

It also struggles to restart cleanly with several cycles of flashing green then flashing red, before eventually coming back to life with breathing cyan.

Does anyone have any advice on how to diagnose this issue? Or any suggestions of what might be the problem?

thanks
Saul

Posting your code would be a good start :laughing:

1 Like

Yes, of course - I’d assumed it was more likely to be a hardware issue.

// This #include statement was automatically added by the Particle IDE.
#include <SparkIntervalTimer.h>

// This #include statement was automatically added by the Particle IDE.
#include <clickButton.h>

int greenLED = D1;
int greenness = 0;
int blueLED = D0;
int blueness = 128;
ClickButton inputBtn(D2, HIGH, CLICKBTN_PULLUP);
String alarmsJSON;
IntervalTimer fadeTimer;
bool pauseFade=false;


void setup() {
    pinMode(greenLED, OUTPUT);
    pinMode(blueLED, OUTPUT);

    analogWrite(greenLED, greenness);
    analogWrite(blueLED, blueness);

    fadeTimer.begin(fadeIncrement, 20, hmSec);
    
}


void loop() {
    
    inputBtn.Update();

    if(inputBtn.clicks != 0) {
        if(inputBtn.clicks == 1) {
            pauseFade=(!pauseFade);
            Particle.publish("pause", pauseFade);
        }
        if(inputBtn.clicks == 2) {
            pauseFade=true;
            setBlue(255);
            setGreen(255);
            Particle.publish("off");
        }
    }
    delay(10);

}

int fadeMatrix[3][2]={
    {0,0},
    {255,0},
    {0,255}
};
int fadeMatrix_i=0;

void fadeIncrement() {
    if(!pauseFade) {
        int destGreen=fadeMatrix[fadeMatrix_i][0];
        int destBlue=fadeMatrix[fadeMatrix_i][1];
        if(greenness<destGreen)
            setGreen(greenness+1);
        if(greenness>destGreen)
            setGreen(greenness-1);
        if(blueness<destBlue)
            setBlue(blueness+1);
        if(blueness>destBlue)
            setBlue(blueness-1);
        if(blueness==destBlue && greenness==destGreen)
            fadeMatrix_i=(fadeMatrix_i+1)%3;
    }
}




int setBlue(int b){
    if (b>255) 
        b=255;
    if (b<0)
        b=0;
    blueness=b;
    analogWrite(blueLED, blueness);
    return blueness;
}


int setGreen(int g){
    if (g>255) 
        g=255;
    if (g<0)
        g=0;
    greenness=g;
    analogWrite(greenLED, greenness);
    return greenness;
}

@saulcozens, I would use a Software Timer instead of SparkIntervalTimer which uses ISRs. You are calling a log of code in your timer ISR and a Software Timer might be a better choice. Besides, it’s built in to the system firmware! Also, any global variable that you expect to change in the timer callback should be declared of type volatile. Fixing these will most likely fix the problem.

You might also consider making a generic setColor() function instead of three versions.

2 Likes

That’s really helpful - I hadn’t considered using the ISR driven timers might be the problem, but that makes sense.

thanks

2 Likes