I see in a lot of examples "*" which I have been told are pointers (what ever that is), "uint8_t", "0x4e" and so on... I understand all the Arduino basics and now would like to understand all this "stuff" I see but have no idea what it is, how it works or why. What should I now focus on? "C++"? or some other language? In essence: how do i start to learn more of the underlying science of what i have been using.
I suggest a basic C text, maybe even Kernigham and Ritchie, but their examples don't work on an Arduino very well.
Thank you.. will learning "C" ultimatly tell me what all these things mean in the end? Also: is there any "C" books that will be best for working with Arduino?
"*" is used for multiplication, but is also used for pointer dereferencing. You will need to learn a bit about pointers before tackling that one.
"uint8_t" is a type definition. It represents a type of "unsigned integer length 8 bits". On the Arduino it means the same thing as either "byte" or "unsigned char". For example, the following lines of code are all equivalent.
uint8_t someVariable = 12;
byte someVariable = 12;
unsigned char someVariable =12;
"0x4E" is just a number in hexadecimal format. The "0x" part is just a prefix that tell the compiler to treat the number that follows as base 16. For example 0x0F is the same as 15 and 0x10 is the same as 16.
stuart0: Thank you. I mentioned those 3 things to show my ignorance. I am looking for guidance on a path to take to learn the basics (which will encompass much more than the 3 things I mentioned above). My impression right now is that learning "C" will be the right direction to go. I am hoping to find C books that a re geared for the Arduino beginner...
I'd suggest that you find a C or C++ tutorial and work through that. They're big enough topics that I wouldn't expect an Arduino text to cover them in any depth. Just look at the number of pages in any book on the topic.
Once you have some mastery of the language, it'll be simple to apply what you've learned to Arduino.
A variable is an empty box with a name into which you can insert a value.
So the code
int8_t x;
declares and empty box called 'x' that can contain 8 bit/1 byte integer values.
You insert a value into this empty box via the assignment operator like so
x = 0;
The data type of the value you put in the box much match the data type that you declared the empty box with.
At the most fundamental level a microcontroller's memory is simply a long line of bits that can contain a 1 or a 0, and each bit has a number. I believe in 8 bit controllers these are then grouped into blocks of 8 bits.
1 2
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| | | | | | | | | | | | | | | |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
So in the above simple example the micontroller would have 2 bytes of available memory and each byte contains 8 bits.
The address of those two bytes are '1' and '2'.
By declaring the variable x you are simply reserving one block of 8 bits as empty box 'x'.
So the compiler might choose to reserve the first byte of the available memory as your variable x.
The address of x is then '1' and in your sketch x is simply a proxy for address 1.
At the most fundamental level, the type of the variable x simply tells the compiler how many bits to reserve for x, in this case 1 byte / 8 bits starting from address or byte number 1.
A pointer is simply another 'empty box' that that can contain a value that corresponds to an address from the microcontroller's total available memory.
uint8_t x = 0;
uint8_t *p = &x
So now you would have two empty boxes or variables.
x can contain any old integer value from 0 to 255.
If you try and do this x=256 you will get over flow and you will find that x instead contains 0. But we will leave overflow for another time - you need to teach yourself to count in binary rather than more familiar decimal in order to understand what overflow is.
With 16 bit arduino microcontrollers, all pointers are 2 bytes or 16 bits in size - the compiler sees '*' in the variable declaration and automatically allocates 2 bytes from the total memory.
Essentially p can contain any integer value between 0 and 65535 but it should contain a valid address for one of your other variables.
You use p like this: p = &x;
& tells the compiler to get the memory address of x (not the value it contains) and place the address of x in your variable p.
p is now a pointer to x.
If you do this x = 0; Serial.println(*p); Serial.println(x) you will find you will get the same value output - 0
If you do this x=0;Serial.println(p);Serial.println(&x) you will likewise get the same value output - the memory address of x but not the value 0.
If you do this x = 0;Serial.println(p);Serial.println(x) you will get different values output - a number between 0 and 65535 corresponding to the memory address of x and 0
*p is how you de-refference a pointer. You are telling the compiler to give you the value contained in the empty box that p is pointing to rather than the memory address of the empty box that p itself contains.
Anyway try and digest that and try out the bits of code in a sketch.
Hope it helps.
SamBrownADK:
Thank you.. will learning "C" ultimatly tell me what all these things mean in the end? Also: is there any "C" books that will be best for working with Arduino?
There must be hundreds of hundreds of online C tutorials. Do a bit of googling and find one that suits you.
You could start here: C Tutorial - Learn C Programming.
However Arduino hides the function main().
So everything that this tutorial tells you to type in function main() you could simply type in Arduino's setup() function instead.
Except printf won't work, of course.
KeithRB:
Except printf won't work, of course.
Unless you do Serial.begin(9600) at the top of setup() before any Serial.print's
boylesg:
A variable is an empty box with a name into which you can insert a value.
except that it is not empty...
int8_t x;
because we are close to bare metal, x may have a value and that value may or may not be zero, depending on when you initialize it (like you are here).
Once you get you head around pointers and you programming knowledge builds a little you will soon understand what situations that pointers are really useful in.
BulldogLowell:
except that it is not empty...int8_t x;
because we are close to bare metal, x may have a value and that value may or may not be zero, depending on when you initialize it (like you are here).
Conceptually empty - until you put a value in it that you want to re-use.
boylesg:
Conceptually empty - until you put a value in it that you want to re-use.
No, not even conceptually. In your example, x will always have a value. Compared to JavaScript, for example, where a variable which is initialized without an assignment is always undefined.
In my opinion, it may better to teach folks to initialize a variable to a value, especially local variables.