Go Down

Topic: Is there a way to reference one variable with another? Preferably by name. (Read 1 time) previous topic - next topic

Dyslexicbloke

Its all interesting and useful stuff ...

The idea was to make the display definition file easy to read, human read, whilst including information that the program can use to fetch and format the appropriate value ...

I discovered that dtostrf works for floats, I think because the implementation of floats and doubles is the same for the Arduino IDE.
Anyway I decided that numbers, ones I may want to display anyway, would always be held as floats, just to keep the code streamlined.

The text file will need to be used by two routines, one to populate the LCD and a second to mimic that on a web page.
Ignoring the web page for the time being, I will need a routine that accepts a file mane as an argument and then displays that file, inserting dynamic data as it go's.

I think based on the suggestions, that I have all I need to be building and manipulating the stings.
It also seems clear that fetching a variable based on a human readable name at run-time may not be worth the effort, given the requirement to map the names anyway.
I think where I am now is using a case statement, in a single function, to effectively map variables to names at design time making them usable at run time.

I am well up for any additional suggestions though.
Would getting pointers help? I hear C++ is all about pointers which I appreciate conceptually but have never used .. That's what you get for learning VB
could I use a struct or class to work similarly to the case and if I could is there any point?

Thanks
Al






If I knew where the box was I would probably still want to think outside it!

Feel free to be blunt ... Its how I learn.

PaulS

Quote
I discovered that dtostrf works for floats, I think because the implementation of floats and doubles is the same for the Arduino IDE.

No. It's because when you pass a float to a function that expects a double, the float gets a temporary promotion (to double).

dhenry

Quote

Is there a way to reference one variable with another?


Isn't that called a "pointer"?

jraskell

PaulS's suggestion to use a map seems ideally suited to this task.  No need to use tediously large switch statements or if/else if chains.

MichaelMeissner


Quote
I discovered that dtostrf works for floats, I think because the implementation of floats and doubles is the same for the Arduino IDE.

No. It's because when you pass a float to a function that expects a double, the float gets a temporary promotion (to double).

It depends.  If you are programming in C and call a function without a prototype in scope, all float's are converted to double, and char/short values are promoted to int.  This isn't true for C++, since C++ requires prototypes (the arduino IDE creates the prototypes behind your back).  Likewise if you are programming in C/C++ and call a varardic function (one that accepts variable arguments like printf), the default promotion rules kick in.

On the other hand, if you are call a non-varardic function with a proper prototype, the compiler does not have to convert floats to doubles.  Given the compiler knows the types of the called function, it will do an implicit conversion from the type of the argument that is passed to the type of the argument in the function.

The usual naming convention says that math type rounds that ends in 'f' take float arguments, I would imagine dtostrf takes a float as an argument, and dtostr takes a double.  On the AVR based systems, both have the same representation, but on other systems (like the DUE), float and double likely have different representations.

Go Up