0
Offline
God Member
Karma: 6
Posts: 918
Arduino rocks
|
 |
« on: July 31, 2012, 08:48:55 pm » |
Hi gang-
I have a question on using arrays.
normal usage:
byte buttons[] = {5, 6, 3, 9};
can it be done like this instead:
const int safetyPin = 5; const int normalPin = 6; const int autoPin = 3; const int reloadPin = 9;
byte buttons[] = {safetyPin, normalPIn, autoPin, reloadPin};
I guess an arrays of variables? or references to pin(s)? this way I can also refer to them by pin name else where..etc..
Thanks
|
|
|
|
|
Logged
|
|
|
|
|
North Queensland, Australia
Online
Edison Member
Karma: 31
Posts: 1181
|
 |
« Reply #1 on: July 31, 2012, 09:15:39 pm » |
Yes int *i_Array[] = { &safetyPin, &normalPIn, &autoPin, &reloadPin}; notice the int pointer type, not a byte, declare the constants as bytes to use byte* EDIT: this will use more ram than your first method.
|
|
|
|
« Last Edit: July 31, 2012, 09:30:20 pm by pYro_65 »
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #2 on: July 31, 2012, 11:23:40 pm » |
can it be done like this instead: const int safetyPin = 5; const int normalPin = 6; const int autoPin = 3; const int reloadPin = 9;
byte buttons[] = {safetyPin, normalPIn, autoPin, reloadPin}; Try it and see? Apart from the typo on normalPIn, that compiles and should work.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #3 on: July 31, 2012, 11:24:16 pm » |
Even this, since you are using constants: const byte buttons[] = {safetyPin, normalPin, autoPin, reloadPin};
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 6
Posts: 399
|
 |
« Reply #4 on: July 31, 2012, 11:57:50 pm » |
But the way you and Nick are doing it isn't a pointer. You're just setting one variable (in the array) equal to another (not in the array). There aren't really any pointers involved here.
The code Pyro posted uses pointers and would involve some other changes in your code to dereference those.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #5 on: August 01, 2012, 12:05:05 am » |
The OP did not say anything about pointers.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #6 on: August 01, 2012, 12:06:03 am » |
Ah, except the thread title of course.
My response was to the actual post, I ignored the thread title.
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 30
Posts: 2918
I only know some basic electricity....
|
 |
« Reply #7 on: August 01, 2012, 02:34:19 am » |
Hi gang-
I have a question on using arrays.
normal usage:
byte buttons[] = {5, 6, 3, 9};
can it be done like this instead:
const int safetyPin = 5; const int normalPin = 6; const int autoPin = 3; const int reloadPin = 9;
byte buttons[] = {safetyPin, normalPIn, autoPin, reloadPin};
I guess an arrays of variables? or references to pin(s)? this way I can also refer to them by pin name else where..etc..
Thanks
Learn about enumerators (enum), they can also help you keep your code straighter.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
|
|
Offline
Sr. Member
Karma: 6
Posts: 399
|
 |
« Reply #9 on: August 01, 2012, 04:59:37 pm » |
Google up.any good C++ tutorial. Enum is a pretty basic piece. Shouldn't be hard to find a good explanation.
|
|
|
|
|
Logged
|
|
|
|
|
North Queensland, Australia
Online
Edison Member
Karma: 31
Posts: 1181
|
 |
« Reply #10 on: August 01, 2012, 06:06:55 pm » |
Your intention could be expressed using enums something like this: enum MY_PIN_SET{ safetyPin = 5, normalPin = 6, autoPin = 3, reloadPin = 9, };
const byte buttons[] = {safetyPin, normalPin, autoPin, reloadPin};
|
|
|
|
|
Logged
|
|
|
|
|
|