Photon Hard Fault! (Variable randomly changes value and overflows)

I have a device setup to retrieve information from varies sensors.

For example:

double PCBTemp = 0, AcColdTemp = 0, AcHeatTemp = 0, mainLEDTemp = 0, frontLEDTemp = 0, rearLEDTemp = 0;
int ac_status = 0;
int pump_spd = 0;

They are being updated once every 2 seconds.

Upon creating a lot of variables with many different types(approximately 100+ variables), without any function modifying a variable such as “pump_spd”. When printing, the variable “pump_spd” would display the initial value 0. Without a repeatable pattern, it would suddenly display a value such as 93765147083248.29 when I am debugging using

printf("pump_spd: %d", pump_spd);

Then the photon would flash the red SOS for HARD FAULT.

Will appreciate if anyone could think of the possible reason why this would occur!

Hard faults typically indicate that you write a variable past its boundary. It sounds like a variable is being modified past the variable memory boundary. You say you don’t have any functions modifying pump_spd; does your code modify any other variable? For example, are you writing to an array past the defined array boundary and inadvertently modifying pump_spd by that other variable update?

By the way, the variable you typed “93765147083248.29” has a decimal place… is that true? When using %d in printf, it is valid for both decimal and integer according to this reference… but I’m not sure how would get a decimal if declared as an integer. Perhaps you have defined two variables with the same name… maybe even one global and one local?

1 Like

Just a point of curiosity: Since the Photon by default hasn’t set a stdout device to write to, where would you expect printf() to print to?

‘’‘Serial.printf()’’’ was what I meant and using a serial console from the computer to read it.