1o1o1o1o1

1o1o1o1o1 > Physical Computing and Interaction > Arduino > Arduino Programming

Arduino Programming

Arduino's microcontorller, the ATMEGA8, is programmed by writing code in its own programming environment then transferring that code to the chip via usb or serial cable.



This is a screen grab of the Arduino Programming software.
If you can program in C or Java then you should be ok with this language which resembles Processing, they all look the same to me but unfortunately the syntax is different. Information about the syntax can be found HERE but I prefer to start with simple examples like the one featured in the screen grab above, then start making changes to its values and see what happens. Any greyed out code, like that on the left of the shot, is just notes by the programmer, explaining whats going on in that certain line. Lets analyze this code and look at the notes-

1>    int ledPin = 13;                           // LED connected to digital pin 13
2>    void setup()
3>       {
4>          pinMode(ledPin, OUTPUT);    // sets the digital pin as output
5>       }
6>    void loop()
7>       {
8>          digitalWrite(ledPin, HIGH);    // sets the LED on
9>          delay(1000);                         // waits for a second
10>        digitalWrite(ledPin, LOW);    // sets the LED off
11>        delay(1000);                         // waits for a second
12>     }

In the first line we are declaring that there is an integer [whole number],int, and it has the value of 13 and the name 'ledPin'. we do this because we intend to use the name ledPin later and not write the number 13. The next time ledPin is mentioned it is in  line 4 where is being configured as an out put port on the Arduino board. We could just right the number 13 here instead and get rid of the whole first line but it is more convenient this way because we can declare the number address of all the pin ports we are going to be using and give them a name that is easy to remember and use it later. for instance if we have  four pin ports on Arduino and they relate to  a left senor and a right sensor and a left buzzer and a right buzzer then we can declare then as below:

int leftsensor = 13;
int lrightsensor = 12;
int leftbuzzer = 11;
int rightbuzzer = 10;

and when we are writing our code we just write the name without having to remember what the number address of the pin is each time.
This declaring of values and names is done at the begin of the page which is also where you include any other header files you may need to compile your code.
In the second line we reach the words 'void setup'. A 'void' is like a subroutine or a procedure or a function [ except void does not return a value it just ends then goes on to the next or loops again ], and the 'setup' part is just the name, this is arbitrary except for 'void setup' and 'void loop' . setup() is preparation, and loop() is execution. In the setup section, always at the top of your program, you would set pinMode, initialize serial communication, etc. The loop section is the code to be executed -- reading inputs, triggering outputs, etc.
In line three the term 'pinMode' is an intrinsic function of the language, thats why its colored different in the software screen shot. Its the quite straight forward the pinMode of ledPin [address pin port 13 on the board] is set to OUTPUT. This is only to do with the digital in/out pins of the board and the opposite is obviously INPUT for switches and digital sensors. Then this 'void' ends and another begins. Ardiuno programs you write will endlessly loop unless you tell them otherwise.
In the 'void loop()' we encounter the command 'digitalWrite', this then needs to be followed by a number or a variable and then a value which is either HIGH or LOW which relates directly to ON or OFF of a port on the Arduino board. The next line 'delay(1000)' is quite simple, this is a pause of 1000 miliseconds or 1 second. I think you can figure out the rest, the program turns the output pin OFF or to LOW then pauses again and loops within 'void loop()' forever.


 

TOP
ui
 
hj