Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« on: November 18, 2008, 07:14:25 pm » |
I have always wondered why all examples in the arduino enviroment use int where not needed. Why not use byte?
byte bLedPin = 13;
This uses half the memory and is, in my honest opinion a much better practice especially in a microcontroller enviroment.
[the prefix b might be omitted, ofcource, but it is quite standard to prefix variables so why not encourage it?]
This is more a question than anything else, I just have to know if there is any good arguments for using ints where not needed.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Faraday Member
Karma: 6
Posts: 2504
|
 |
« Reply #1 on: November 18, 2008, 07:25:52 pm » |
I suspect it's a matter of habit, left over from C programming on systems that aren't memory constrained.
I'll take it a step further - I don't see why these are variables at all. To use your LED example, are you going to change which pin the LED is connected to while the program runs? I think it should be #defined.
-j
|
|
|
|
|
Logged
|
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #2 on: November 18, 2008, 09:30:50 pm » |
Indeed it was a stupid example. But I though about all loops and such. Also, who not use the keyword unsigned a bit often? It can be helpful that the compiler let you know that it may be a stupid thing to do to try and set a led dim to -2, for instance. I have always wondered how the #define stuff works, does the number get 'hard coded' into hex in the resulting hex file? [i thought it was just a simple find/replace operation] or how does it work? You see, I though that it would take 2bytes if defined, that arduino defaults to ints but that is wrong?  if so, that is great. I vote for 'better' practices concidering datatype and use of unsigned datatypes.
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #3 on: November 19, 2008, 01:16:05 am » |
#defines are more efficient because the compiler can hard code the values in the hex file. But sometimes a variable is necessary, for example when using an array of pins, and using byte is more efficient than int where a variable is necessary.
I think the arduino team is rightly more concerned about how easy an example is for the user to understand than how efficient the code will be. But the examples do influence user style and I also vote for introducing the use of the right sized data types (and #defines) in example code.
|
|
|
|
« Last Edit: November 19, 2008, 01:16:50 am by mem »
|
Logged
|
|
|
|
|
0
Offline
Faraday Member
Karma: 6
Posts: 2504
|
 |
« Reply #4 on: November 19, 2008, 07:35:09 am » |
A #define (and all other preprocessor directives, e.g. #include , #ifdef, etc) are handled by the C preprocessor (cpp) and not the compiler. #define is a basic search and replace text operation that takes place before the compiler runs.
-j
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #5 on: November 19, 2008, 08:16:40 am » |
A #define (and all other preprocessor directives, e.g. #include , #ifdef, etc) are handled by the C preprocessor (cpp) and not the compiler. #define is a basic search and replace text operation that takes place before the compiler runs. That is correct but misses the point being made above. The compiler is more likely to optimize values that are #defined than values that are declared as normal variables.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Faraday Member
Karma: 6
Posts: 2504
|
 |
« Reply #6 on: November 19, 2008, 09:30:26 am » |
Sorry, I should have quoted for clarification. I wasn't commenting on (or correcting or arguing with) anything you said, mem, I was trying to answer the question "I have always wondered how the #define stuff works".
-j
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #7 on: November 19, 2008, 09:51:21 am » |
Now I feel bad for sounding harsh  Jason, thanks for clearing that up 
|
|
|
|
« Last Edit: November 19, 2008, 09:52:06 am by mem »
|
Logged
|
|
|
|
|
0
Offline
Faraday Member
Karma: 6
Posts: 2504
|
 |
« Reply #8 on: November 19, 2008, 09:59:44 am » |
no problem, didn't sound harsh to me. tone is difficult to pick up, that's why on the internet we a) don't take it personally, and b) use those smiley things. on all the points you brought up, we are in violent agreement.  -j
|
|
|
|
|
Logged
|
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #9 on: November 19, 2008, 12:01:16 pm » |
I think the arduino team is rightly more concerned about how easy an example is for the user to understand than how efficient the code will be. Ofcourse, but I would argue that it equally easy to comprehend the byte datatype, as opposed to ints. In microcontroller world you are bound to either come across bytes and bits, or if you are an artist using arduino for leds and such, you already know that you need some mega bytes to store tha program. I do think that it is rather intuitive that the byte is a variable that can hold data. Two last questions about #define: 1) are the variables then treated as constants by compiler? 2) does not arduino store the defines/constants in a integer datatype? [or does compiler check for how many bytes needed, tried google but no luck]
|
|
|
|
« Last Edit: November 19, 2008, 12:09:20 pm by AlphaBeta »
|
Logged
|
|
|
|
|
0
Offline
God Member
Karma: 0
Posts: 507
|
 |
« Reply #10 on: November 19, 2008, 12:39:14 pm » |
#define is just a macro #define pin 13 ... digitalWrite(pin,HIGH);
is identical to: digitalWrite(13,HIGH);
You can put just about anything in a #define, i.e. #define pinOn digitalWrite(13,HIGH) ... pinOn;
You can also paramaterize the macro, Don K posted a handy example here for determining the length of an arbitrarily typed array: #define ITEM_CNT(x) (sizeof(x)/sizeof(x[0]))
While conserving every last bit and byte is important (when you start running out of bits and bytes), being able to do things generically and reducing maintenance are good things too.
|
|
|
|
« Last Edit: November 19, 2008, 12:43:25 pm by dcb »
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #11 on: November 19, 2008, 12:44:19 pm » |
Two last questions about #define: >1) are the variables then treated as constants by compiler?
#defines of integral values are not variables, they are treated as constants
> 2) does not arduino store the defines/constants in a integer datatype? [or does compiler check for how many bytes needed, tried google but no luck]
The compiler uses the smallest data type that will hold the constant.
|
|
|
|
« Last Edit: November 19, 2008, 12:45:17 pm by mem »
|
Logged
|
|
|
|
|
Portland, OR, USA
Offline
Jr. Member
Karma: 0
Posts: 78
|
 |
« Reply #12 on: November 19, 2008, 12:51:34 pm » |
#defines are more efficient because the compiler can hard code the values in the hex file. You can also define a variable with the const attribute to serve the same purpose. The compiler will substitute the value as an optimization instead of allocating space in RAM for it (assuming there are no references taken of it). The added benefit is that the compile can type check uses of the "constant". static const uint8_t pinLED = 13;
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #13 on: November 19, 2008, 01:26:20 pm » |
You can also define a variable with the const attribute to serve the same purpose. Hi don, indeed you can , but #define pinLed 13 seems easier for beginners than static const uint8_t pinLED = 13;
|
|
|
|
« Last Edit: November 19, 2008, 01:27:47 pm by mem »
|
Logged
|
|
|
|
|
Norway@Oslo
Offline
Edison Member
Karma: 11
Posts: 2033
loveArduino(true);
|
 |
« Reply #14 on: November 19, 2008, 02:01:33 pm » |
My reason for discussing the #define is the fact that the compiler can help with constants, just as don said. [I forgot to explicitly say this, but I somewhat tapped into it while argumenting for unsigned usage]
I do agree that #define seems easyer for beginners, and because of this a better arduino practice on pin declaration.
|
|
|
|
« Last Edit: November 19, 2008, 02:02:04 pm by AlphaBeta »
|
Logged
|
|
|
|
|
|