@tfmccarthy I modified the code to the above as you suggested, but nothing happened, motors do not run. Below what I change it to.
void OnDataRecv(const esp_now_recv_info_t *info, const uint8_t *incomingData, int len) {
const uint8_t *mac = info->src_addr; // Extract sender's MAC address
if (new_message) {
return;
if (memcmp(mac, sender2_MAC, 6) == 0) {
memcpy(&myData1, incomingData, sizeof(myData1));
Serial.println("Data received from Sender 2 (myData1)");
Serial.print("StringDOWN: "); Serial.println(myData1.Value_StringDOWN);
Serial.print("StringUP: "); Serial.println(myData1.Value_StringUP);
Serial.print("String1: "); Serial.println(myData1.Value_String1);
}
new_message = true;
}
}
void loop() {
// MotorA runs forward first message
if (!new_message) {
return;
}
if (myData1.Value_String1 == 1 && myData1.Value_StringUP == 1) {
Serial.println("MotorA Running Forward");
for (int pwm = 0; pwm <= 255; pwm++) { // ramp up forward.
motorA.motorGo(pwm);
motorRunningForward = true;
motorRunningBackward = false;
new_message = false; // allow new message
}
delay(10);
}
This doesn't match the changes I gave. I don't see the declaration of the variable new message, which is important. Let's start with OnDataRecv. This is what you started with,
void OnDataRecv(const esp_now_recv_info_t* info, const uint8_t* incomingData, int len) {
const uint8_t* mac = info->src_addr; // Extract sender's MAC address
if (memcmp(mac, sender2_MAC, 6) == 0) {
memcpy(&myData1, incomingData, sizeof(myData1));
Serial.println("Data received from Sender 2 (myData1)");
Serial.print("StringDOWN: "); Serial.println(myData1.Value_StringDOWN);
Serial.print("StringUP: "); Serial.println(myData1.Value_StringUP);
Serial.print("String1: "); Serial.println(myData1.Value_String1);
}
}
I said to make this change:
bool new_message{false};
void OnDataRecv( ... ) {
// ignore new messages until current message
// is processed
if (new_message) {
return;
}
//... get new message ...
new_message = true;
}
So you should have produced
bool new_message{false};
void OnDataRecv(const esp_now_recv_info_t* info, const uint8_t* incomingData, int len) {
// ignore new messages until current message
// is processed
if (new_message) {
return;
}
const uint8_t* mac = info->src_addr; // Extract sender's MAC address
if (memcmp(mac, sender2_MAC, 6) == 0) {
memcpy(&myData1, incomingData, sizeof(myData1));
Serial.println("Data received from Sender 2 (myData1)");
Serial.print("StringDOWN: "); Serial.println(myData1.Value_StringDOWN);
Serial.print("StringUP: "); Serial.println(myData1.Value_StringUP);
Serial.print("String1: "); Serial.println(myData1.Value_String1);
}
new_message = true;
}
Similarly, the loop function starts with
void loop() {
// MotorA runs forward first message
if (myData1.Value_String1 == 1 && myData1.Value_StringUP == 1) {
Serial.println("MotorA Running Forward");
for (int pwm = 0; pwm <= 255; pwm++) { // ramp up forward.
motorA.motorGo(pwm);
motorRunningForward = true;
motorRunningBackward = false;
}
}
delay(10);
}
I gave this change
void loop()
{
// if no new message, do nothing
if (!new_message) {
return;
}
// ... process message ...
new_message = false; // allow new message
}
This should produce
void loop() {
// if no new message, do nothing
if (!new_message) {
return;
}
// MotorA runs forward first message
if (myData1.Value_String1 == 1 && myData1.Value_StringUP == 1) {
Serial.println("MotorA Running Forward");
for (int pwm = 0; pwm <= 255; pwm++) { // ramp up forward.
motorA.motorGo(pwm);
motorRunningForward = true;
motorRunningBackward = false;
}
}
delay(10);
new_message = false; // allow new message
}
The one who has the most fun, wins!
@tfmccarthy I modified the code to the above as you suggested, but nothing happened, motors do not run. Below what I change it to.
Take a closer look at what you're telling it to do in OnDataRecv ... you start off with
if (new_message) {
return;
So if new_message is true then you return immediately without extracting the newly received message and return with new_message still being true.
However, if new_message is false, then the body of the if statement is not executed, so you STILL don't exact the message sent and don't reset the value of new_message.
So your code cycles endlessly without ever extracting the message data or changing new_message's value.
Try removing the outer if statement as in
void OnDataRecv(const esp_now_recv_info_t *info, const uint8_t *incomingData, int len) {
const uint8_t *mac = info->src_addr; // Extract sender's MAC address
if (memcmp(mac, sender2_MAC, 6) == 0) {
memcpy(&myData1, incomingData, sizeof(myData1));
Serial.println("Data received from Sender 2 (myData1)");
Serial.print("StringDOWN: ");
Serial.println(myData1.Value_StringDOWN);
Serial.print("StringUP: ");
Serial.println(myData1.Value_StringUP);
Serial.print("String1: ");
Serial.println(myData1.Value_String1);
new_message = true;
}
}
Anything seems possible when you don't know what you're talking about.
Try removing the outer if statement as in
For Wi-Fi, is onDataRecv called asynchronously, i.e., during the loop function?
The one who has the most fun, wins!
Thanks for your assistance, I'm learning as I go.