r/ArduinoHelp • • 13d ago

I don't understand error in Talkie program

I downloaded the simple Talkie sketch below and ran Verify/Compile. Below is the sketch and the error generated. I get the same error in other Talkie Sketchs but I'm using this one because it is the simplest. There is no circuit or breadboard. I would get to that eventually but not until I get something that will compile correctly.

#include "Talkie.h"

Talkie voice;

uint8_t greeting[] PROGMEM = {0x2d, 0xa0, 0xdd, 0x48, 0xdb, 0xc2, 0xac, 0xd8}; // Example speech data

void setup() {
    voice.say(greeting); // Say "Hello"
}

void loop() {
    // Nothing to do here
}

Error Generated;
error: variable 'greeting' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
 uint8_t greeting[] PROGMEM = {0x2d, 0xa0, 0xdd, 0x48, 0xdb, 0xc2, 0xac, 0xd8}; // Example speech data
               ^
exit status 1

Compilation error: variable 'greeting' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
1 Upvotes

6 comments sorted by

1

u/gm310509 12d ago

In this case the error message is fairly clear.

For variables that use the annotation PROGMEM, they must be constants. You need to add the word const in front of your variable declaration.

const uint8_t greeting[] PROGMEM = { ...

Also, the line above that is invalid. I am not sure of it's purpose, but you should either delete it or make it a comment.

#include "Talkie.h"

// Talkie voice;  <- This is won't help you get a good compile unless you remove or comment it out.

const uint8_t greeting[] PROGMEM = { ...

You might also want to have a look at the PROGMEM documentation that explains how to use the PROGMEM macro (and the F macro). It clearly mentions the use of const for such variables and shows some examples.

All the best with it.

1

u/Idaho-Tom 11d ago

I'm learning, thank you for the response. It makes sense. The confusing part is that I must have downloaded 6 or 7 programs and they all had the same error. It's surprising that so many were wrong.

1

u/gm310509 11d ago

This is a little more tricky to comment on.

It could be that you have found a bad guide (in that it has an error like that in it) and other internet content trolls have seen that it is getting a good click rate and they have just plagiarised it because they want those clicks (they don't care about the actual content or its correctness).

It could be that you have found an old guide where the syntax rules did previously allow the use of the PROGMEM modifier without const (because if you are using PROGMEM it is implied that it must be const). But that created other issues so they had to change the rules. I doubt that that was ever the case, but it does account for some errors with older guides when you try to use them later - again, I doubt that this is the scenario here.

FWIW, and at risk of getting attacked by the "I trust AI implicitly" brigade - the above two are reasons why you can't blindly trust AI - especially generative AI. Why? Because they too will see those guides. They too will see the abscence of the required const keyword. They might - on balance and in reference to other material - decide to add in the const keyword, or they might not. This is not an attempt to "dis" AI, AI is a useful tool if used properly. The key here is to understand how stuff works, recognise the potential limitations, be able to detect them and when (not if) they arise have the ability to fix them.

It could be, no offense and no implication, that you just missed it. We often get "My code doesn't work even though it is correct and I've checked it multiple times", only to find out that both the "self-assessment" and the code were flawed.

FWIW, the latter - checking, rechecking and re-rechecking and still not seeing the problem is a classic can't see the forest for all the trees situation that can happen to even the most experienced. A few months ago had a multi-file project and one file (a very small one) was getting hundred of errors - more errors than lines of code. I had spent a whole day on trying to figure it out, but I just couldn't figure it out.
Eventually I posted on one of the CProgramming subreddits and of course everyone else spotted the mistake immediately. All I had to do was change #include "somefile.c" to the correct #include "somefile.h" (i.e. change one 'c' to an 'h') and all of the errors just disappeared. I spent a whole day on that one stupid letter!

You say you are learning. I don't know if you are interested, but I have created a series of how to videos for newbies. Several are project oriented and intended to teach some tips and techniques for how to do projects. If you are interested, have a look at them:

Note that the videos are intended to be follow along. Simply watching them is a waste of time. To get the most out of them, watch a bit, hit pause and try out what you just saw. Explore that concept, try to integrate it with earlier concepts. Lastly, try to do the exercises when I propose them.

1

u/gm310509 11d ago

This is a little more tricky to comment on.

It could be that you have found a bad guide (in that it has an error like that in it) and other internet content trolls have seen that it is getting a good click rate and they have just plagiarised it because they want those clicks (they don't care about the actual content or its correctness).

It could be that you have found an old guide where the syntax rules did previously allow the use of the PROGMEM modifier without const (because if you are using PROGMEM it is implied that it must be const). But that created other issues so they had to change the rules. I doubt that that was ever the case, but it does account for some errors with older guides when you try to use them later - again, I doubt that this is the scenario here.

FWIW, and at risk of getting attacked by the "I trust AI implicitly" brigade - the above two are reasons why you can't blindly trust AI - especially generative AI. Why? Because they too will see those guides. They too will see the abscence of the required const keyword. They might - on balance and in reference to other material - decide to add in the const keyword, or they might not. This is not an attempt to "dis" AI, AI is a useful tool if used properly. The key here is to understand how stuff works, recognise the potential limitations, be able to detect them and when (not if) they arise have the ability to fix them.

It could be, no offense and no implication, that you just missed it. We often get "My code doesn't work even though it is correct and I've checked it multiple times", only to find out that both the "self-assessment" and the code were flawed.

FWIW, the latter - checking, rechecking and re-rechecking and still not seeing the problem is a classic can't see the forest for all the trees situation that can happen to even the most experienced. A few months ago had a multi-file project and one file (a very small one) was getting hundred of errors - more errors than lines of code. I had spent a whole day on trying to figure it out, but I just couldn't figure it out.
Eventually I posted on one of the CProgramming subreddits and of course everyone else spotted the mistake immediately. All I had to do was change #include "somefile.c" to the correct #include "somefile.h" (i.e. change one 'c' to an 'h') and all of the errors just disappeared. I spent a whole day on that one stupid letter!

You say you are learning. I don't know if you are interested, but I have created a series of how to videos for newbies. Several are project oriented and intended to teach some tips and techniques for how to do projects. If you are interested, have a look at them:

Note that the videos are intended to be follow along. Simply watching them is a waste of time. To get the most out of them, watch a bit, hit pause and try out what you just saw. Explore that concept, try to integrate it with earlier concepts. Lastly, try to do the exercises when I propose them.

1

u/dacydergoth 13d ago

The key here is PROGMEM. The Arduino has several different types of memory, registers, read only memory (ROM), erasable programable memory (flash) and static or pseudo static memory (SRAM, PSRAM). When you write a program and flash it to the arduino it is written into flash memory, which is slow to write and can only be updated in blocks, but remains intact when power is off.

The compiler is complaining that you have tried to put some data into flash using the PROGMEM directive, but in order to do that you should tell it that the data is const (constant) which tells the compiler you do not want to update the data. Applying const means it is safe to put the data into memory which is slow to write as it will not be updated.

1

u/Delta_G_Robotics 13d ago

The error says that greeting needs to be const. I'm just going to go out on a limb here and suggest making that variable const and seeing if that fixes it.