I have working code but as you'll see I have to type in the msgTable the same commands that I have already typed. For example, I've defined charPosition, but in the msgTable, I need to use "Position". I'd like to use charPosition and all the other char definitions in the msgTable but when I try I get problems with invalid conversion from 'char*' to 'char' Is there a way to define using the already defined chars?
msg msgTable[] = {
// only messages that comes from stepper
{charPosition, MSG_Position},
{charDownLimitTrip, MSG_DownLimitTrip},
{charDownLimitOK, MSG_DownLimitOK},
{charUpLimitTrip, MSG_UpLimitTrip},
{charUpLimitOK, MSG_UpLimitOK},
{charMSG_NONE, MSG_NONE} // Command not found in the list
};
#include <Arduino.h>
const int MSG_Position = 1;
const int MSG_DownLimitTrip = 2;
const int MSG_DownLimitOK = 3;
const int MSG_UpLimitTrip = 4;
const int MSG_UpLimitOK = 5;
const int MSG_NONE = 99;
char charUpLimitOK[] = "UpLimitOK";
char charUpLimitTrip[] = "UpLimitTrip";
char charDownLimitTrip[] = "DownLimitTrip";
char charDownLimitOK[] = "DownLimitOK";
char charPosition[] = "Position";
char charMSG_NONE[] = "MSG_NONE";
int i;
const int maxSizeCommand = 35;
struct msg
{
char Cmd[maxSizeCommand]; // largest cmd from CYD size, probably going to be a Position command, <MoveTo1600>
uint8_t ID; // The internal message number
};
msg msgTable[] = {
// only messages that comes from stepper
{"Position", MSG_Position},
{"DownLimitTrip", MSG_DownLimitTrip},
{"DownLimitOK", MSG_DownLimitOK},
{"UpLimitTrip", MSG_UpLimitTrip},
{"UpLimitOK", MSG_UpLimitOK},
{"MSG_NONE", MSG_NONE} // Command not found in the list
};
const int MSG_COUNT = (sizeof(msgTable) / sizeof(msgTable[0])); // ELEMENTS(msgTable);
void setup() {
Serial.begin(115200);
delay(1500);
Serial.print("Number of Elements = "); Serial.println(MSG_COUNT);
for (i=0;i<MSG_COUNT;i++){
Serial.print(i); Serial.print(" ");Serial.println(msgTable[i].Cmd);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
[sterretje edit]
fixed code tags for first block
[/sterretje]
They are commands passed between a cheap yellow display ESP32 and another ESP32 that's connected to a stepper motor. The ESP32s communicate using ESP_NOW. There are a lot more commands. I'm trying to avoid errors by changing the "char command" and mistyping the equivalent entry in the msgTable.
msg msgTable[] = {
// only messages that comes from stepper
{"Position", MSG_Position},
{"DownLimitTrip", MSG_DownLimitTrip},
{"DownLimitOK", MSG_DownLimitOK},
{"UpLimitTrip", MSG_UpLimitTrip},
{"UpLimitOK", MSG_UpLimitOK},
{"MSG_NONE", MSG_NONE} // Command not found in the list
};
To
msg msgTable[] = {
// only messages that comes from stepper
{charPosition, MSG_Position},
{charDownLimitTrip, MSG_DownLimitTrip},
{charDownLimitOK, MSG_DownLimitOK},
{charUpLimitTrip, MSG_UpLimitTrip},
{charUpLimitOK, MSG_UpLimitOK},
{charMSG_NONE, MSG_NONE} // Command not found in the list
};
But they are not used in the sketch that you posted
I still don't understand what you use them for, but if you want to use the array names in the msgTable array then declare the array in the msg struct as const char
Personally I would not use the arrays and would just put the text in the msgTable array and use it when needed
#include <Arduino.h>
const int MSG_Position = 1;
const int MSG_DownLimitTrip = 2;
const int MSG_DownLimitOK = 3;
const int MSG_UpLimitTrip = 4;
const int MSG_UpLimitOK = 5;
const int MSG_NONE = 99;
char charUpLimitOK[] = "UpLimitOK";
char charUpLimitTrip[] = "UpLimitTrip";
char charDownLimitTrip[] = "DownLimitTrip";
char charDownLimitOK[] = "DownLimitOK";
char charPosition[] = "Position";
char charMSG_NONE[] = "MSG_NONE";
int i;
const int maxSizeCommand = 35;
struct msg
{
char Cmd[maxSizeCommand]; // largest cmd from CYD size, probably going to be a Position command, <MoveTo1600>
uint8_t ID; // The internal message number
};
msg msgTable[] = {
// only messages that comes from stepper
{charPosition, MSG_Position},
{charDownLimitTrip, MSG_DownLimitTrip},
{charDownLimitOK, MSG_DownLimitOK},
{charUpLimitTrip, MSG_UpLimitTrip},
{charUpLimitOK, MSG_UpLimitOK},
{MSG_NONE, MSG_NONE} // Command not found in the list --- This MUST BE the last entry in the table as it gets passed back if no message found -- return msgTable[MSG_COUNT - 1]; // Return MSG_NONE entry - nothing found
};
const int MSG_COUNT = (sizeof(msgTable) / sizeof(msgTable[0])); // ELEMENTS(msgTable);
void setup() {
Serial.begin(115200);
delay(1500);
Serial.print("Number of Elements = "); Serial.println(MSG_COUNT);
for (i=0;i<MSG_COUNT;i++){
Serial.print(i); Serial.print(" ");Serial.println(msgTable[i].Cmd);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
Sketch uses 2368 bytes (7%) of program storage space. Maximum is 30720 bytes.
Global variables use 490 bytes (23%) of dynamic memory, leaving 1558 bytes for local variables. Maximum is 2048 bytes.
My bad - I've changed it and still get the error for every entry in the msgTable
error: invalid conversion from 'int' to 'char*' [-fpermissive]
25 | {charPosition, MSG_Position},
#include <Arduino.h>
const int MSG_Position = 1;
const int MSG_DownLimitTrip = 2;
const int MSG_DownLimitOK = 3;
const int MSG_UpLimitTrip = 4;
const int MSG_UpLimitOK = 5;
const int MSG_NONE = 99;
int i;
const int maxSizeCommand = 35;
struct msg
{
const char Cmd[maxSizeCommand]; // largest cmd from CYD size, probably going to be a Position command, <MoveTo1600>
uint8_t ID; // The internal message number
};
msg msgTable[] = {
// only messages that comes from stepper
{ "Position", MSG_Position },
{ "DownLimitTrip", MSG_DownLimitTrip },
{ "DownLimitOK", MSG_DownLimitOK },
{ "UpLimitTrip", MSG_UpLimitTrip },
{ "UpLimitOK", MSG_UpLimitOK },
{ "MSG_NONE", MSG_NONE } // Command not found in the list --- This MUST BE the last entry in the table as it gets passed back if no message found -- return msgTable[MSG_COUNT - 1]; // Return MSG_NONE entry - nothing found
};
const int MSG_COUNT = (sizeof(msgTable) / sizeof(msgTable[0])); // ELEMENTS(msgTable);
void setup()
{
Serial.begin(115200);
delay(1500);
Serial.print("Number of Elements = ");
Serial.println(MSG_COUNT);
for (i = 0; i < MSG_COUNT; i++)
{
Serial.print(i);
Serial.print(" ");
Serial.println(msgTable[i].Cmd);
}
}
void loop()
{
// put your main code here, to run repeatedly:
}
I'm going to go with UKHeliBob's suggestion. I can use the msg.ID to find the msg.Cmd to send back and forth.
In my old age I tend to get fixated on a solution where I should really look at it from a different view point.
Thank you all for the help
Colin