Assistance with code for MD_MAX72XX matrix display

Hi,
I could use some programming assistance if someone would be so kind. I will post the code and then explain the issue.

#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>

// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may
// need to be adapted
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 8

#define PAUSE_TIME    5000
#define SCROLL_SPEED  50

#define CLK_PIN   13
#define DATA_PIN  11
#define CS_PIN    10

char Day = "Monday";

// HARDWARE SPI
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
// SOFTWARE SPI
//MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);


void setup(void)
{
  // initialise the LED display
  P.begin();
  P.setZoneEffect(0, true, PA_FLIP_LR);
}

void loop(void)
{

//if (Day == "Monday") {
char *msg[] = {"Test text A","Test text B",};
//}

  
  static uint8_t cycle = 0;

  if (P.displayAnimate())
  {
    // set up the string
    P.displayText(msg[cycle], PA_CENTER, SCROLL_SPEED, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);

    // prepare for next pass
    cycle = (cycle + 1) % ARRAY_SIZE(msg);
  }
}

I would like to include the code: char *msg[] = {"Test text A","Test text B",}; within an if statement. But when I do, i get the following error: 'msg' was not declared in this scope

Could someone please explain how to correct this error?

Thank you in advance for your assistance.

Scott

char *msg[] = {"Test text A","Test text B",};

Start by getting rid of the comma. Then, copy / paste / post the complete error message.

Also, you can't compare c-strings like this:

if (Day == "Monday") {

See strcmp and strncmp.

Hi gfvalvo,

I took the second comma out so the line of code now reads.

char *msg[] = {"Test text A","Test text B"};

I get the error message:

C:\Users\ScottJ2505\Documents\Arduino\Parola_Ambulance2\Parola_Ambulance2.ino: In function 'void loop()':

Parola_Ambulance2:57:19: error: 'msg' was not declared in this scope

P.displayText(msg[cycle], PA_CENTER, SCROLL_SPEED, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);

^

Using library MD_Parola at version 3.0.1 in folder: C:\Users\ScottJ2505\Documents\Arduino\libraries\MD_Parola
Using library MD_MAX72XX at version 3.0.1 in folder: C:\Users\ScottJ2505\Documents\Arduino\libraries\MD_MAX72XX
Using library SPI at version 1.0 in folder: C:\Users\ScottJ2505\Documents\ArduinoData\packages\arduino\hardware\avr\1.6.21\libraries\SPI
exit status 1
'msg' was not declared in this scope

I don't understand the problem with the c-strings. I did consult your references.

Please advise.

Thank you,
Scott

That if statement puts msg out of scope later in the function. I think you will need to declare those strings in advance then point to them based on the day.

char *mon_msg[] = {"Monday text A", "Monday text B"};
char *tue_msg[] = {"Tuesday text A", "Tuesday text B"};
char **msgs;

char *Day = "Monday";

void setup() {

  Serial.begin(9600);

  if ( !strcmp(Day, "Monday" ) )
  {
    msgs = mon_msg;

  } else if ( !strcmp(Day, "Tuesday" ) ) {

    msgs = tue_msg;

  }

  Serial.println(msgs[0]);
  Serial.println(msgs[1]);

}

void loop() { }

Right.

First fix the scope problem. Then I would change all of your constant char pointer array definitions to be like:

const char * const mon_msg[] = {"Monday text A", "Monday text B"};

and

const char * const Day = "Monday";

This will ensure that you don't accidentally change a string literal or strand it in memory by changing the pointer.

ScottJ2505:
I don't understand the problem with the c-strings. I did consult your references.

As I stated, you can't compare c-strings for equality in the way you're trying to do. That's why I gave you the references to the correct functions for doing so.

Thank you, gfvalvo, for your assistance.

Could you please tell me what the asterisks mean (i,e., *mon_msg[] and *Day)?

Also, for the code:

if ( !strcmp(Day, "Monday" ) )

It appears that !strcmp a function that compares the value of Day to "Monday" where Day has previously been defined to equal "Monday" by the code: char *Day = "Monday" . Is this correct?

Thank you,
Scott

Also **msgs;

gfvalvo,
I also noticed that the if statement you propose I use is shown under setup() and not loop(). I believe everything under setup() is executed only once when the program starts up. At a later point in the development of my code I will add use of a DS3231 real time clock which will be used to determine which day it is. The program will need to monitor the day of the week and display the correct message based upon the day of the week.

Do you think there will be any problem with moving your proposed code to be under loop() ?

Thank you,
Scott

ScottJ2505:
Could you please tell me what the asterisks mean (i,e., *mon_msg[] and *Day)?

Google search for "C Language Pointer Variables"

ScottJ2505:
I also noticed that the if statement you propose I use is shown under setup() and not loop().

I didn't propose anything. That 'if' statement was in the code you posted. I simply pointed out that it won't work the way you have it.

Blue Eyes,

Thank you for your assistance.

Could you please tell me what the asterisks mean (i,e., *mon_msg[], **msgs and *Day)?

Also, for the code:

if ( !strcmp(Day, "Monday" ) )

It appears that !strcmp a function that compares the value of Day to "Monday" where Day has previously been defined to equal "Monday" by the code: char *Day = "Monday" . Is this correct?

I also noticed that the if statement you propose I use is shown under setup() and not loop(). I believe everything under setup() is executed only once when the program starts up. At a later point in the development of my code I will add use of a DS3231 real time clock which will be used to determine which day it is. The program will need to monitor the day of the week and display the correct message based upon the day of the week.

Do you think there will be any problem with moving your proposed code to be under loop() ?

In your original code you had:

char Day = "Monday";

That's incorrect because when you say "char" it means one character while "Monday" is an array of characters. You could declare it as either of

char *Day = "Monday";
char Day[] = "Monday";

An asterisk makes the variable a pinter.

For the Monday messages, if you only had one you could do the same thing, declare a pointer to char:

char *mon_msg = "Monday text A";

But since you have more then one you have an array of pointers to char:

 char *mon_msg[] = {"Monday text A", "Monday text B"};

The other variable, msgs, lets us dynamically point to messages for a single day so it is declared as a pointer to pointer to char, don't get too caught up on that, just know that it points to the messages for one day.

Upload the code and look at the serial monitor, you should see the messages for Monday. Change the declaration of Day to

char Day[] = "Tuesday";

and you should see the Tuesday messages.

Yes strcmp() compares two strings and returns zero if they are equal, that's why the if statement has the negation symbol "!", saying zero is true.

I put the code in setup() because it is just a demo of how you might declare and reference your variables. If it was in loop() it would keep printing and printing. It's up to you to incorporate it into your code or ignore it.

I encourage you to run it as is then make some changes to see if it is useful to you. I assume you want different messages for each day, try adding messages and days.

Blue Eyes,
You have done a great job explaining everything to me very clearly. I really appreciate it. I incorporated all the things you have said into the code and it runs very well.
Thanks so much.
Scott

Blue Eyes,

One point of clarification. The code you assisted me with runs great stand-alone. I still need to incorporate it code used to run the MD_MAX72XX display. So, I may have a question or two for you in the future, if you don't mind.

Best regards,
Scott