Offline
Newbie
Karma: 0
Posts: 34
|
 |
« on: December 21, 2012, 04:34:22 am » |
This is probably a silly question but I can't seem to find the answer....... typedef struct { byte a; byte b; } button_t;
button_t values[10]; Is it possible to populate the values in one of the array elements like this: values[].a = (1,2,3,4,5......) I know this doesn't work but hopefully you will get what I am trying to do. Or do I need to do this: values[0].a = 1; values[1].a = 2; values[2].a = 3; . . . . Thanks in advance.
|
|
|
|
|
Logged
|
|
|
|
|
Sydney
Offline
God Member
Karma: 14
Posts: 717
Big things come in large packages
|
 |
« Reply #1 on: December 21, 2012, 04:55:53 am » |
You can. You need to enclose all the elements for a structure within curly braces and separate these groups by comms.. Essentially each array element is a group of variables instead of the individual items if a simple array. struct_type data[3] = {{1,2,3},{3,4,5},{6,7,8}};
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 34
|
 |
« Reply #2 on: December 21, 2012, 06:11:29 am » |
You can. You need to enclose all the elements for a structure within curly braces and separate these groups by comms.. Essentially each array element is a group of variables instead of the individual items if a simple array. struct_type data[3] = {{1,2,3},{3,4,5},{6,7,8}};
So I can't just populate one of the elements? Would this work struct_type data[3] = {{1,,},{3,,},{6,,}};
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19018
I don't think you connected the grounds, Dave.
|
 |
« Reply #3 on: December 21, 2012, 06:41:40 am » |
So I can't just populate one of the elements?
Yes you can, if the element you want to populate is the first, just omit the second value.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 30
Posts: 2903
I only know some basic electricity....
|
 |
« Reply #4 on: December 21, 2012, 06:45:48 am » |
You could put the data in flash then read it in at setup(), any way you want.
Otherwise, why not initialize all the values in one place to make checking and debug easier? If you don't know some, make them 0 or -1 to suit your program since they have to start with some value anyway?
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19018
I don't think you connected the grounds, Dave.
|
 |
« Reply #5 on: December 21, 2012, 06:47:32 am » |
If they're global or static, the compiler will initialise them the zero.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 30
Posts: 2903
I only know some basic electricity....
|
 |
« Reply #6 on: December 21, 2012, 02:11:46 pm » |
Yes. And if you want some initialized otherwise, you might as well do the lot in 1 place. It is easier to find when you need to check or to make changes.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 34
|
 |
« Reply #7 on: December 27, 2012, 07:28:42 am » |
Thanks to everyone for their assistance - I would have replied earlier but tonight has been my first chance to get back to my sketch. This is what I have ended up with, and it works typedef struct { byte xEEPROM; // EEPROM adresss for stored value int xPos; // Stored value for X position byte yPos; // Variable value for Y position int yValue; // Calculated % value of position x int valuePos; // X position for displaying the % value } sliderButton_t;
sliderButton_t button[10] = { {0,50,0,0,0}, {0,84,0,0,0}, {0,118,0,0,0}, {0,152,0,0,0}, {0,186,0,0,0}, {0,220,0,0,0}, {0,254,0,0,0}, {0,288,0,0,0}, {0,322,0,0,0}, {0,356,0,0,0} };
If I did not insert the zero, i.e. used {,50,,,} I got an error message "LED_Controller:33: error: expected primary-expression before ',' token" so it would appear that you need to insert a value for each of the array elements, even if it is zero. Cheers
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19018
I don't think you connected the grounds, Dave.
|
 |
« Reply #8 on: December 27, 2012, 07:34:24 am » |
If I did not insert the zero, i.e. used {,50,,,} You should be able to write {0,50}
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 34
|
 |
« Reply #9 on: December 27, 2012, 04:53:22 pm » |
If I did not insert the zero, i.e. used {,50,,,} You should be able to write {0,50} You are correct, that does work  Thanks again...
|
|
|
|
|
Logged
|
|
|
|
|
Leeds, UK
Offline
God Member
Karma: 35
Posts: 983
Once the magic blue smoke is released, it won't go back in!
|
 |
« Reply #10 on: December 27, 2012, 05:48:16 pm » |
If you swap two lines around in the struct you could just do: sliderButton_t button[10] = { {50}, {84}, ... and so on. byte xEEPROM; // EEPROM adresss for stored value int xPos; // Stored value for X position Becomes: int xPos; // Stored value for X position byte xEEPROM; // EEPROM adresss for stored value Unless you are relying on the position of each item in the struct then the above would work fine.
|
|
|
|
|
Logged
|
~Tom~
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 30
Posts: 2903
I only know some basic electricity....
|
 |
« Reply #11 on: December 27, 2012, 10:14:10 pm » |
UNO has 1k EEPROM....
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 34
|
 |
« Reply #12 on: December 28, 2012, 05:23:44 am » |
UNO has 1k EEPROM....
I am using a Mega but I am a bit confused about your comment - what does the amount of EEPROM have to do with anything??
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19018
I don't think you connected the grounds, Dave.
|
 |
« Reply #13 on: December 28, 2012, 05:30:55 am » |
Maybe it's a reference to the fact that you can't put a 10 bit address in an eight bit variable. This may not be a problem if you never address single EEPROM bytes.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 30
Posts: 2903
I only know some basic electricity....
|
 |
« Reply #14 on: December 28, 2012, 07:14:57 am » |
There are 256 4-byte words worth of space so you could use 8 bits to point to those. Or use 8 bits if you wanted to make sure you never overfilled the EEPROM. Not judging, just pointing out the address length, your app may take that into consideration....
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
|