I would truly appreciate some help on this, I've been working it for hours and am thinking I'm missing something fundamental.
Below I've summarized and abbreviated my code as the actual sketch is about 700 lines; regardless the code below is a representation of what I am facing ...
In my Arduino sketch I have the following defined globally:
enum transmission_Type {
a,
b,
c,
};
enum device_Status {
blocked,
unblocked,
unknown,
};
typedef struct transmission_Structure {
transmission_Type transmission;
device_Status status;
} transmission_Structure;
device_Status myDeviceStatus = unknown;
My ESP-NOW's OnDataRecv routine is coded as follows:
void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len) {
transmission_Structure incomingMessage;
memcpy(&incomingMessage, incomingData, sizeof(incomingData));
if (incomingMessage.transmission == a) {
if (incomingMessage.status == blocked)
Serial.print("status is blocked");
else
Serial.println("status is unblocked");
myDeviceStatus = incomingMessage.status;
if (myDeviceStatus == blocked)
Serial.print("confirming blocked");
else
Serial.println("confirming unblocked");
};
}
and I have a routine that is called from within the Arduino loop() routine as follows:
void reportStatus() {
Serial.println(myDeviceStatus);
if (myDeviceStatus == unblocked)
Serial.println("here the status is unblocked");
else if (myDeviceStatus == blocked)
Serial.println("here the status is blocked");
else if (myDeviceStatus == unknown)
Serial.println("here the status is unknown");
else
Serial.println("here the status is error");
};
In general the sketch runs fine, except for the reportStatus sub routine.
When data is received, what is printed to the serial monitor is, as an example:
status is blocked
confirming blocked
-1163045256
here the status is error
the last two lines are puzzling me, they should be (I would think)
blocked
here the status is blocked
In my real code, I need to take actions in the reportStatus subroutine based on the value of myDeviceStatus, which I cannot do because of the way the results are coming out.
I've also tried adding 'volatile' to the global myDeviceStatus variable definition:
volatile device_Status myDeviceStatus = unknown;
but it didn't make a difference and the value for myDeviceStatus is only updated one place in my program - so not something I thought I would need anyway.
Any help would be appreciated.