I am building a TPMS (Tire Pressure Monitoring System) using Arduino Pro Mini with Atmega328P, 3.3V, 8MHz. The data from or related to the TPMS sensors are collected in this structure:
struct TPMS_entry
{
uint32_t TPMS_ID;
bool isValid;
uint16_t secSinceLastUpdated;
byte TPMS_Status;
double TPMS_Pressure;
double TPMS_LowPressureLimit;
double TPMS_HighPressureLimit;
boolean LowPressure;
boolean HighPressure;
int16_t TPMS_Temperature;
int16_t RSSIdBm;
boolean AudibleAlarmActive;
unsigned short time;
} TPMS[TYRECOUNT + 1];
Here TYRECOUNT = 4
, and the last structure member is for a stray tire, which is not in the list.
The matter of interest is the first member -- TPMS_ID
-- carrying the unique sensor ID. These IDs are initialized from the list stored in the non-volatile memory, and I carefully checked that they are initialized correctly and then never written to.
All the sensors but the first are handled properly. When an RF packet is received from one of the sensors, it is checked against the values in the TPMS[].TPMS_ID
structure members.
In particular, the first member is:
TPMS[0].TPMS_ID = 0x80CDBC58
When this structure value is read to compare with the incoming RF packet, it is read consistently as 0x80CDBC00
; the last byte is always corrupted by 0.
After many hours of investigation and a sleepless night, I decided to put a diagnostic message into the loop()
to see, at what moment this TPMS[0].TPMS_ID
becomes corrupted:
void loop() {
if(TPMS[0].TPMS_ID != 0x80CDBC58L) {
Serial.print("Loop TPMS[0].TPMS_ID "); Serial.println(TPMS[0].TPMS_ID);
}
When I ran it, the 'if' condition was never true, meaning that the TPMS[0].TPMS_ID
value seemed to be always correct, the print statement never printed, and the system worked perfectly correctly! But, when I commented out the print statement, making the body of the 'if' statement completely empty, the wrong value with zero LSB was read again!
Simply put, the presence or absence of the print statement here affect the execution, even though this statement is never executed, no matter if it is commented out or not!
I thought about a possible stack corruption due to insufficient memory, but I only use 800 bytes of RAM out of 2048.
I am desperate, and you -- the very smart guys -- are my only hope in my unavailing attempts to fix this bug...