Sunday, August 26, 2012

0 Arduino Programming - Lesson 1

Hi folks! Been a long time since my last post but this time imma teach you programming! This is a post about the basics of arduino programming.
Now, the arduino is not a language of its own, but was developed from C. Since C looks like it has been designed by dyslexic aliens, and is very difficult for beginners to pick up, the designers of arduino decided to do some modifications in thier IDE to make the language look like plain english. Believe you me, if you know nothing about arduino, and start to read a program, you will be able to understand what exactly its author intended the hardware to do.
For example, let's review the following lines of code:


/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
*/
void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode (13, OUTPUT );
}
void loop() {
digitalWrite(13, HIGH);  // set the LED on
delay(1000);     // wait for a second
digitalWrite(13, LOW);  // set the LED off
delay(1000);     // wait for a second
}



  • A slash followed by an asterisk (/*) opens a block where you can input your comments (they are ended by a */). Two forward slashes (//) indicate that the rest of the line is a comment. Note that comments are not a part of the compiling process, they are there only for the ease of humans to read the piece of code.
  • The setup() function executes in the beginning of a program, and it's a one-time declaration. The setup() function will be called automatically immediately after the Arduino is powered or has been programmed. 
  • Calling a function consists of a return value of a function (void), the name of the function (setup), and a list of parameters in parentheses. This function does not take parameters, so there is nothing inside the parentheses. The setup function does not return values, so its type is void (empty). When calling a function, commands are listed within a block of code, which is enclosed in curly braces ({}).
  • This setup() function includes only one command, which sets the pin connected to the LED into output mode. It calls the pinMode() function, which is defined in the Arduino libraries. OUTPUT is a constant defined within Arduino. When using digital pins on an Arduino, you must always declare them to be in either OUTPUT or INPUT mode.
  • As is required by the C language (which Arduino is based on), a line ends with a semicolon.
  • The majority of the execution time of the program will repeat the loop() function. The loop() function is called automatically (and repeatedly) after the setup() function finishes.
  • To address a digital pin that has been set to be an OUTPUT, we use the digitalWrite() function. Set digital pin 13 on HIGH (which means +5V). Digital pin 13 is unique in that Arduino has a built-in LED and resistor attached to it, specifically for debugging purposes. The LED will light up. The pin remains on HIGH until we address the pin with another call from digitalWrite().
  • Wait 1,000 milliseconds (1,000 one-thousandths of a second, totaling one full second). The LED is lit all the time.
  • Switch the pin off (LOW) and wait again for a second. This is the last command in the loop() function, which means that the execution of the loop() function ends.
See how simple that was! As you practice with arduino, you will begin to develop your own interpretation of  the language, which will allow you make the codes shorter and shorter.
In the next lesson, I will show you how to attach various peripherals to your arduino, and control them via software. 
That's me signing off for today! See ya in the next post!


0 comments:

Post a Comment

Do Comment on Anything you like or dont like!

 

The Engineer's Spot! Copyright © 2011 - |- Template created by O Pregador - |- Powered by Blogger Templates