11.07.2015 Views

Arduino Inventor's Guide - Oomlout

Arduino Inventor's Guide - Oomlout

Arduino Inventor's Guide - Oomlout

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

03 PROGprogrammingprimer.: A Small Programming Primer:.ARDUINO PROGRAMMING IN BRIEFThe <strong>Arduino</strong> is programmed in the C language. This is a quick little primer targeted at peoplewho have a little bit of programing experience and just need a briefing on the idiosyncracies of Cand the <strong>Arduino</strong> IDE. If you find the concepts a bit daunting, don't worry, you can start goingthrough the circuits and pick up most of it along the way. For a more in-depth intro the<strong>Arduino</strong>.cc website is a great resource.STRUCTUREEach <strong>Arduino</strong> program(often called a sketch) hastwo required functions(also called routines).void setup(){ }All the code between the twocurly brackets will be run oncewhen your <strong>Arduino</strong> programfirst runs.void loop(){ }This function is run after setuphas finished. After it has runonce it will be run again, andagain, until power is removed.SYNTAXOne of the slightlyfrustrating elements of C isits formatting requirements(this also makes it verypowerful). If you rememberthe following you should bealright.// (single line comment)It is often useful to write notesto yourself as you go alongabout what each line of codedoes. To do this type two backslashes and everything until theend of the line will be ignored byyour program.{ } (curly brackets)Used to define when a blockof code starts and ends (usedin functions as well as loops)./* */(multi line comment)If you have a lot to say you canspan several lines as acomment. Everything betweenthese two symbols will beignored in your program.; (semicolon)Each line of code must beended with a semicolon (amissing semicolon is oftenthe reason for a programrefusing to compile).VARIABLESA program is nothing morethan instructions to movenumbers around in anintelligent way. Variables areused to do the moving.int (integer)The main workhorse, stores anumber in 2 bytes (16 bits).Has no decimal places and willstore a value between -32,768and 32,767.long (long)Used when an integer is notlarge enough. Takes 4 bytes (32bits) of RAM and has a rangebetween -2,147,483,648 and2,147,483,647.04boolean (boolean)A simple True or Falsevariable. Usefulbecause it onlyuses one bit ofRAM.float (float)Used for floating point math(decimals). Takes 4 bytes (32bits) of RAM and has a rangebetween -3.4028235E+38and 3.4028235E+38.char (character)Stores one character using theASCII code (ie 'A' = 65). Usesone byte (8 bits) of RAM. The<strong>Arduino</strong> handles strings as anarray of char’s.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!