Pages: [1]   Go Down
Author Topic: Const better than not Const?  (Read 412 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a pretty large multi dimensional array for a sketch (I haven't finished filling in all of the values):

Code:
const short letterStrokes[11][5][2] = {
{ //I ###############################################################
  {sketchW, 0},
  {-sketchW_2, 0},
  {0, sketchH},
  {-sketchW_2, 0},
  {sketchW, 0}
},
{ //W ###############################################################
  {sketchW_3, -sketchH},
  {sketchW_6, sketchH_3},
  {-sketchW_6, -sketchH_3},
  {sketchW_3, sketchH},
  {-1, -1}
},
{ //A ###############################################################
  {sketchW_3, sketchH_2},
  {sketchW_3, 0},
  {-sketchW_3, 0},
  {sketchW_6, sketchH_2},
  {(sketchW_6 + sketchW_3), -sketchH}
},
{ //N ###############################################################
  {0, 0},
  {0, 0},
  {0, 0},
  {0, 0},
  {0, 0}
},
{ //T ###############################################################
  {0, 0},
  {0, 0},
  {0, 0},
  {0, 0},
  {0, 0}
},
{ //L ###############################################################
  {0, 0},
  {0, 0},
  {0, 0},
  {0, 0},
  {0, 0}
},
{ //O ###############################################################
  {0, 0},
  {0, 0},
  {0, 0},
  {0, 0},
  {0, 0}
},
{ //F ###############################################################
  {0, 0},
  {0, 0},
  {0, 0},
  {0, 0},
  {0, 0}
},
{ //H ###############################################################
  {0, 0},
  {0, 0},
  {0, 0},
  {0, 0},
  {0, 0}
},
{ //I ###############################################################
  {0, 0},
  {0, 0},
  {0, 0},
  {0, 0},
  {0, 0}
},
{ //S ###############################################################
  {0, 0},
  {0, 0},
  {0, 0},
  {0, 0},
  {0, 0}
}
};

It basically is a set of stroke lengths for a set of letters that are drawn with some motors. My question is: I'd like to have a config function that runs at setup that finds the maximum stroke width and height (sketchW and sketchH), then have all the stroke lengths set in the array. I'm wondering if making this array NOT a const (but obviously still initializing its size) so that I can set the values in a config function has any draw backs on memory or performance of the arduino? In general are const variables more stable or anything?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26497
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not make sure it stays "const", and put it in PROGMEM?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 640
Posts: 50335
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
In general are const variables more stable or anything?
No. The compiler assures that the rest of your code doesn't crap on them is all that const does.

Quote
Why not make sure it stays "const", and put it in PROGMEM?
Because OP doesn't really want them const. As I read it, anyway. It appears that all those sketch names are variables that are valued at run time.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd like to have a config function that runs at setup that finds the maximum stroke width and height (sketchW and sketchH), then have all the stroke lengths set in the array.

I'd suggest you aim to make that array constant. If some of the values in it need to be scaled by common factors, hold the scale factors in memory and scale the values read from the array before using them.

This saves all that work to process and modify the array contents, and also means the whole array can be a constant which reduces the chance of messing it up, and it can be moved to PROGMEM so freeing up a significant amount of RAM.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Dallas, TX USA
Offline Offline
Faraday Member
**
Karma: 70
Posts: 2740
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
In general are const variables more stable or anything?
No. The compiler assures that the rest of your code doesn't crap on them is all that const does.
const does much more than that.
const also allows the compiler to perform certain optimizations because it knows the value is a constant
and will not change including by code in other modules.
It could even eliminate the storage for the variable and use the variables value as an immediate.
In a conditional, it might even eliminate the conditional test and half of the conditional (if/else)
depending on the code since the result of the conditional check is known at compile time
if the tested values are const

Using const in a table can allow some  pretty intense & amazing optimizations.
For example, if the table is const and all the code that accesses the table uses indexes known at compile time
(by using const, #define, or immediate constants), to index into the table, and no pointers to the table are passed around,
then the compiler can literally optimize away the entire table and its data storage.
It will replace all the lookups into the table with their immediate values.
When possible,
this a much better approach than using AVR progmem since progmem is a ugly/kludgey hack for making
the AVR harvard architecture work in a von nuemann C language world.

--- bill
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good feedback everyone, thanks (especially PaulS, as you are currently saving my ass on numerous fronts... I owe you a lb of coffee. Vita, the good stuff. Or are you a Stumptown man?). I decided to leave it as a CONST and write a separate sketch that performs config functions. I wanted it to be a "plug and run" for all situations, but given that I only need to run the config the very first time the things is ever run, it seems easiest to just separate it out into its own sketch, then plug config values into the main sketch and upload.

Wow... that just got really long. Did anyone read this far? If you did you get this second thank you: thank you.
Logged

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 808
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Good feedback everyone, thanks (especially PaulS, as you are currently saving my ass on numerous fronts... I owe you a lb of coffee. Vita, the good stuff. Or are you a Stumptown man?). I decided to leave it as a CONST and write a separate sketch that performs config functions. I wanted it to be a "plug and run" for all situations, but given that I only need to run the config the very first time the things is ever run, it seems easiest to just separate it out into its own sketch, then plug config values into the main sketch and upload.

Wow... that just got really long. Did anyone read this far? If you did you get this second thank you: thank you.

I stopped reading after Post #2......
Logged

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 502
Posts: 19086
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow... that just got really long. Did anyone read this far? If you did you get this second thank you: thank you.

Yep, although being C, it's "const" and not "CONST" (nor "Const").
Logged


Pages: [1]   Go Up
Jump to: