How to Serial.print a serie of different data in pointer locations

Hello,

I'm still a beginner but there's is progress. I bumped a few time's against the Serial.print 'barrier'.
Now trying to Serial.println mutiple variables, storred in two arrays of pointers.
The following code is made of examples i found but i have the feeling i'm doing something wrong when i create the arrays (but the first one with the const char is exactly how Bing showed me).
The second array is a array of pointers to int's but that doensn't seem to work. So I want to print 'for' every 'i' a line with the pointer content of the first array (String) and the pointer content of the second array.
Don't mind the extra stuff after the //. That is info for my project where i want to use this.

//first fill the arrays of pointers
const char* menuText[] = {"MinLevel\t", "MaxLevel\t", "FillStart\t", "FillStop\t", "Alarmtimer\t"}; //this is also a arry of pointers but these 'values' are alway the same
//int* menuVariables[4]; //this is a array of pointers and can be used like this without pre-defining the values
int* menuVariables[] = {180, 30, 170, 150, 120};//last int is 120 minutes and needs to be converted to ms


void setup() {
  Serial.begin(9600);
  for (int i=0; i<=4; i++){
    //Serial.println(*menuText[i], *menuVariables[i]);//doesnt work
    Serial.print(menuText[i]);
    Serial.println(*menuVariables[i]); //doesnt work
  };
}

void loop() {
}

thx!!

Change the warning level from NONE to ALL and the compiler will happily tell you what you're doing wrong.

.../test.ino:4:47: warning: invalid conversion from 'int' to 'int*' [-fpermissive]
 int* menuVariables[] = {180, 30, 170, 150, 120};//last int is 120 minutes and needs to be converted to ms
                                               ^
.../test.ino:4:47: warning: invalid conversion from 'int' to 'int*' [-fpermissive]
.../test.ino:4:47: warning: invalid conversion from 'int' to 'int*' [-fpermissive]
.../test.ino:4:47: warning: invalid conversion from 'int' to 'int*' [-fpermissive]
.../test.ino:4:47: warning: invalid conversion from 'int' to 'int*' [-fpermissive]

Your menuVariables array is an array of int, not int *.

I'm simply shocked that Bing didn't warn you about that... :roll_eyes:

1 Like

I'm sure what you meant to say is that the menuVariables array should just be int, not int *.

As it is, it is a valid if somewhat dangerous array of pointers to who knows what.

So

int menuVariables[] = {180, 30, 170, 150, 120};


// and later

  
    Serial.println(menuVariables[i]);

a7

I could have worded it more clearly. My point still stands. Turn on all compiler warnings and pay attention to what it complains about.

thx for the info about the compiler warning level!

I'm backing down from pointers for now. its going to be a struggle if i want to get different types in one 2-coƶrdinate of 3-coƶrdinate array (what normaly isn't possible but propably can be solved with using pointers).
I've kept it more simple and changed it to:

//first fill the arrays of pointers
String menuText[] = {"MinLevel\t", "MaxLevel\t", "FillStart\t", "FillStop\t", "Alarmtimer\t"}; //this is also a arry of pointers but these 'values' are alway the same
int menuVariables[5] = {180, 30, 170, 150, 120};//last int is 120 minutes and needs to be converted to ms
String menuUnits[] = {"cm", "cm", "cm", "cm", "min"};


void setup() {
  Serial.begin(9600);
  for (int i=0; i<=4; i++){
    //Serial.print(menuText[i]);
    //Serial.print(menuVariables[i]);
    //Serial.println(menuUnits[i]);
    Serial.println(menuText[i] + menuVariables[i] + menuUnits[i]);
  };
}

void loop() {
}

In a other stage of my project/sketch the Serial.begin need to be replaced by A lcd object to generate the same text on a 16x2 lcd screen. Hopefully i'ts copy/paste (menuText[i] + menuVariables[i] + menuUnits[i]) but of course with setting the cursor first (i'v done some simple testing).

Pointers maybe but if all you are worrying about is

You could benefit from using a struct (structured variabke). You can use an array of them and each element can hold all different types as defined by your struct.

Look here or consult your favorite learning source:

HTH

a7

2 Likes

I'm going to ask Bing for a example. This data also needs to be stored in the free to use EEPROM in case the power goes down. But this is the last challenge my (for me big) little project.
It's going to be a one button waterlevel meter for my (rain)watertank with refill valve command, lcd level readout (in %) and double external refill (adjustable refill with tap water) error alarm.

And at this point I'll take my leave. Good luck and good-bye.

1 Like

Who is Bing?

a7

MSN search. I'm getting AI-like answers there.

If you are going to ask AI for advice, that is your choice, but in that case, please do not waste the time of real people who help on the forum. Let AI write your code and then help you find errors in it. The forum has pretty strict rules on this matter, no one will correct the code written by robots.

I dindn't knew that but it makes sense. The code i have posted is all mine.
..i need to skill up my coding & the code i"m using in my project is all mine. I want to take credit for myself.
I'm not even shure if AI would be able to make it function exactly as i want. It also needs to be 'readable' for me. So i'm not gonna spend time on it. Last i threw away a few days of my time by trying to use a pushbutton library that was, after harshly trying to understand the code (what you normaly don't have to do!), rubbisch for my project (and other projects). So i'm skipping the risks now and build it myself.

1 Like

Libraries can be handy. Most are at least functional. Button libraries run the gamut from worse than useless to "doesn't suck".

It is worth learning how and doing button handling once in your lifetime. Many ppl do it by hand, so to speak, alla time.

This exact Button.h library is the barest of bones style. So if you just want a stable readable input and recognition of when the input changes (when a button gets pressed or when it gets released) it is easy to use and def does not suck. I have found no caveats.

It's also not terrible in terms of something to read and see one way of doing it.

The default debounce constant 100 milliseconds is waaaay toooooo loooong and makes it feel sluggish. Fortunately the constructor can take an argument to set that for your circumstances.

A line from the examlke sketch with the debounce argument added:

Button button3(4, 15) ; // button between pin 4 and GND, 15 ms debounce.

HTH

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.