Arduino for NOOB's
After troweling the internet and forums i decided to write a small note( haha ) for those who want to know the very basics of the arduino.
I am by no means an expert ( whatever that means!) but i'm keen to share what i have learnt by trial and error.
This will cover the very basics which can trip you up, and will hopefully help you get past the steep learning curve or brick wall that you may hit!
Im not going into the what an Arduino is, just that 20 years ago the same device would have been loads of component stuck on a pcb about the size of most tablets now.
Memory:
Volatile memory is forgotten once the Arduino is powered down.
Nonvolatile stays in the Arduino when its powered down.
There are 3 types of memory in an Arduino.
- ROM (Read Only Memory) this is Nonvolatile and is where the program is stored.
- RAM (Random Access Memory) this is Volatile and is where Variables are stored.
- EEPROM ( Electrically Erasable Programmable Read Only Memory) This is Nonvolatile and is used to store Variables that need to be kept.
A Bit is a single piece of information in Binary, either 1 for TRUE, or 0 for FALSE.
A Byte is made from 8 Bits, and is usually written right to left, each of the positions represents a number.
eg.
00000001 = 1
00000010 = 2
The numbers are 128,64,32,16,8,4,2,1
so by using this combination you can get any number from 0 - 255, that is 256 numbers, 0 is actually a number in this case.
As the Arduino is 8 bit 22 would read as follows:
00010110
So why do we need this, well computers are dumb they just work with ON & OFF . But humans don't, so we need to see number or letters, this blog for example is seen by a computer as a huge long stream of numbers which it converts in to a user readable character using a code called ASCII .
thats fine for text but what if you want to just store a number, your limited to 256.
Well you can use multiple Bytes for this.
Before we go there we just need to explain the memory and how its used.
Imagine memory is like mail sorting room, one wall covered in a set of pigeon holes.
Each one of those is a memory location, so how do we tell the Arduino what each location is called.
Well we use Variables. A Variable is a pointer to a memory location.
like your home address any mail to that address will go into a special pigeon hole in the sorting office.
Variable names can be short e.g a or more descriptive like; ButtonPressedState
for example:
a= 100; this will put the value 100 into the location pointed to by a this could be different location each time but the arduino knows where it is.
One problem Memory is immutable ( what the heck? immutable means cannot be changed!).
so what happens when we add, subtract, mulitply or divide, the answer is put into another location and the pointer is then pointed at that new location
example
a= a + b, so 'a' is at memory location 1, 'b' is at memory location 2, but location 1 has the original value and we want to change it, so we put the answer into location 3( or 700000) it doesn't matter as long as that location is not being pointed to and we then point the variable 'a' to that new location. and the original location as it now has no pointer is deleted!
So next time we do the sum a=a+b;
we will use location 3 for 'a', location 2 for 'b' and the result can now go back into location 1 (or any other location)as its free, the pointer will change to location 1 for 'a' and location 3 will be deleted.
This is what the Dynamic Memory (RAM) in the Arduino is for , that actual program a=a+b is stored in program storage (ROM).
Phew thats all done, i hope that makes sense.
So now we have an idea on Memory ( or yours has just failed! like mine) so how do we get larger numbers.
Use multiple Bytes, 1 Byte gives use 256 , 2 bytes gives 65,535. This is achieved by using one byte as a UPPER BYTE and the other as an LOWER BYTE so we now have:
32768,16384,8192,4096,2048,1024,512,256,128,64,32,16,8,4,2,1
This is called a WORD, 16 bits of information.
Now we can go bigger as well by using 32 bit or 4 Bytes we can have 0- 4,294,967,295.
In Arduino Speak the above are as follows:
byte
unsigned int or word
unsigned long
No comments:
Post a Comment