I keep running into this type of int command in various sketches but have been unable to find out what it does exactly. I have googled it and even put it in the Arduino Search above which only kicks me back out to google. Sorry for such a silly request but I enjoy the coding a bit more then the Hard :o :o ware and like to understand it. Thanks
It's a type of variable like char or int or long.
In that case it represents a type (hence the _t at the end) of an unsigned (hence the u at the begining) integer (hence the int) number stored in memory on one byte in arduino's memory (really 8 bits - hence the 8 - and most computers have 8 bits for a byte)
So const uint8_t motorDirectionPin = 3;
declares a constant, requiring 8 bits of memory, representing an unsigned number and initialized with the value 3 that you can refer too later in the code with the name motorDirectionPin
.
You'll find as well
int8_t uint8_t
int16_t uint16_t
int32_t uint32_t
int64_t uint64_t
Integer type with a width of exactly 8, 16, 32, or 64 bits.
For signed types (no u in front of the type), negative values are represented using 2's complement.
No padding bits.
You need to brush up your googleFoo because the first 3 search answers for "uint8_t C++" give me the correct info
Hope this helps!
The uint8_t is typedef of classic C 'unsigned char' aka byte. Similary the other types. They are defined in stdint.h (AVR 8 ). I think, just read this header file and everything will be clear.
So basically anything C++ goes with the Ide. Cool! Makes sense as its based on the G++ compiler. It can not be a wasted day when I have learned something new. Thank you for that fantastic breakdown!
Yes there are a few functions not implemented by default because they would bloat the binary code to unreasonable size (like handling float in some string functions) but you are pretty safe with the c++ standard as your guide.
So const uint8_t motorDirectionPin = 3; declares a constant, requiring 8 bits of memory, representing an unsigned number and initialized with the value 3 that you can refer too later in the code with the name motorDirectionPin
.
So what is the advantage over say
int motorDirectionPin=3;
? Sorry Im think headed
It helps the compiler. If that name is declared as a const, so it won't change, the compiler is likely not going to allocate memory for it at all and will just substitue in assembly language the reference to the memory fetch you would need to do with the number 3. So less memory used and faster code.
It will also bark at you if you have a bug and later in your code you try to change the value to something else by mistake because const can't change value. So that is a benefit as well in terms of code quality.
Your version will (compilers are smart though so might notice you use it as a const) allocate two bytes instead of one that is enough for storing the value 3, will trigger then two bytes fetch in memory every time you use it, and most likely then use only the low byte because functions you use it with will require a byte, not two...
Basically the more info about your intent you can give to the compiler, the better it will optimize your code and help you with not creating bugs.
Thank you, that was crystal clear, and thanks for the c++ reference as well, good stuff !
With pleasure.
Have fun with your code!
Good stuff, trials show that Im saving about 1 byte per variable using uint8_t over the int. Again its never a wasted day if i have learned something new ...