Arduino crash course

Aus www.wiki.ardumower.de
Wechseln zu: Navigation, Suche

You never had to with programming yet? This page shows you the very basic concepts of programming the Arduino. After reading, you will be able to develop your own Arduino programs and to understand the Ardumower code.


Your first program

This will make the onboard Arduino LED to blink (by toggling Arduino pin 13 between 5V and 0V):

 // This is run only once after the Arduino starts. The command 'pinMode' configures the Arduino LED pin 13
 void setup() {
   pinMode(13, OUTPUT);
 }

 // This will be run over and over (so forever). It's the main loop of the program.
 void loop() {
   digitalWrite(13, HIGH);    // set pin 13 to 5V/HIGH
   delay(1000);               // wait 1000 milliseconds (=1 second)
   digitalWrite(13, LOW);     // set pin 13 to 0V/GND/LOW
   delay(1000);               // wait 1000 milliseconds (=1 second)
 }

Run this code in your Arduino IDE and see what happens with your onboard Arduino LED. Then try to adjust the example so that the LED blinks more quickly!

Expressions

Everything that can be calculated is an expression. Expressions are used to compute things. We will use expressions later for variables or functions calls.

Examples for expressions:

 13               => can be computed to 13 (it's simple right?)
 2 * 13           => can be computed to 26
 42 - 9 * 100     => can be computed as everything is known to compute it
 a + b - c + 20   => can be computed if a, b and c are known

Variables

A memory variable can contain a value (integer number, floating number, text character etc.). It is useful to use variables instead of repeating hard wired numbers. Here the same example, this time using a variable for the LED pin:

 // declare an integer variable named 'pinLED' and assign it the value 13
 int pinLED = 13;       // this is a comment

 void setup() {
   pinMode(pinLED, OUTPUT);   // here, we use the variable named 'pinLED' instead of the hard-wired number
 }

 void loop() {
   digitalWrite(pinLED, HIGH);    // here, we use again the variable 'pinLED'
   delay(1000);              
   digitalWrite(pinLED, LOW);     // here, we use again the variable 'pinLED'
   delay(1000);              
 }

Try it out! Variables can be integer number, floating numbers, logical (TRUE/FALSE), and text characters:

 int someIntegerNumber = 42;
 boolean someLogical = TRUE;
 float someFloatingNumber = 3.1419;
 char someCharacters[] = "Hello World";

A variable's contents can be re-assigned at any time in the program:

 int pinLED = 13;   // declare variable named pinLED and initialize it with value 13
 pinLED = 12;       // assign pinLED a new value (12)

You can even use calculations/expressions:

 int A = 4;
 int B = A * 42 + 15;


You may wonder what the values 'HIGH' and 'LOW' in the above code example are. These are integer constants that the Arduino library already did define for you (HIGH = 1, LOW = 0). Using variables and constants makes code more human-readable.

Functions

We did use three pre-defined functions already in above code which are already defined by the Arduino library:

 pinMode
 digitalWrite
 delay

Additionally, we already defined ourselves two functions in above code: 'setup' and 'loop'. Functions group commands into blocks. These blocks are then called by the name of the function. A function can also have input parameters, and one output parameter.

Example:

 // Declares a function named 'writeAndWait' to set LED to a given value (HIGH/LOW) and wait 
 void writeAndWait(int value){
   digitalWrite(pinLED, value);    // here, we use the parameter 'value' instead of a fixed HIGH/LOW value
   delay(1000);                
 }

Now we can use that new function to make the main loop more readable:

 void loop() {
   writeAndWait(HIGH);  // call function with 'HIGH' (for parameter 'value') 
   writeAndWait(LOW);   // call function with 'LOW' (for parameter 'value')
 }

After each statement (variable assignment, function call etc.) we make a semicolon. The semicolon divides the current statement and the next statement. Try it out!


Optionally, a function can return a value. In this example, the function named 'addSomething' will compute the sum of its input parameters 'a' and 'b' and return that computed sum.

 int addSomething(int a, int b){
   return a + b;
 }

Now you use your new function to compute the sum of any integer values:

 int c = addSomething( 4, 5 );

Serial console output and text

To get feedback from your Arduino, you may want to use the serial console. Let's see a simple example. Run this program and open your Arduino IDE serial console (CTRL+SHIFT+M) at 19200 baud and look if it returns the text.

 void setup(){
   Serial.begin(19200);  // set the baud rate to use
 }

 void loop(){
   Serial.println("Hello world"); // output the text to Arduino serial console
   delay(1000);
 }

For-loops and blocks

To repeat things, use for-loops. This example will repeat outputting the text by using a variable ('counter') that counts from 0 to 3. Each time the loop is executed, the variable 'counter' will increase by one:

Hello 0
Hello 1
Hello 2
Hello 3
... then the main loop restarts...

The example uses a block indicated by '{ ... }'. Everything that is inside this block is run each time the for-loop increases.

 void loop(){  
   for (int counter=0; counter < 4; counter++)
   {
     Serial.print("Hello ");
     Serial.println(counter);
   }
   delay(1000);
 }

Try it out!

Digital input

Let's read the ouside world - we'll read the state of digital pin 5 in this example. We use the Arduino internal pull-up resistor (so pin 5 is HIGH when the switch is open). Simply connect pin 5 to GND to make the switch going LOW.

 int pinSwitch = 5; // connect this pin to GND to activate the switch
 int pinLED = 13;  // we use the Arduino onboard LED

 void setup(){
   // configure the switch pin to use the Arduino internal pull-up resistor
   pinMode(pinSwitch, INPUT_PULLUP);
   pinMode(pinLED, OUTPUT);
   Serial.begin(19200);  
 }

 void loop(){  
   // read the value of the switch pin - it is '1' when HIGH, and '0' when LOW.
   int value = digitalRead( pinSwitch );
   Serial.print("The switch is "); 
   Serial.println( value ); 
   // write the value to the LED pin - it will be HIGH when writing a '1', and LOW when writing a '0'.
   digitalWrite( pinLED, value );
   delay(1000);
 }

Try it out!

If-clauses

For running code based on conditions, use if-clauses. If the condition for the if-clause evaluates to logical-TRUE, the first block is called, otherwise the else block.

Example:

 void loop(){
   int value = digitalRead ( pinSwitch );
   if (value == HIGH){
     Serial.println( "The switch is HIGH" );
   } 
   else  {
     Serial.println( "The switch is LOW" );    
   }
   delay(1000);
 }  

Conditions can be more complex of course:

if (numberX > 5)... // variable 'numberX' must be larger than 5
if (numberX > numberY) ... // variable 'numberX' must be larger than variable 'numberY'
if (numberX == numberY) ... // 'numberX ' must be equal 'numberY'
if (numberX < numberY) ... // 'numberX' must be smaller than 'numberY'

Also you can combine multiple conditions:

if ( (number > 5) && (number < 10) )... // logical AND: both conditions must be TRUE
if ( (number < 5) || (number > 10) )... // logical OR: only one condition must be TRUE

What you have learned

  • Variables can contain numbers (integer, float) or text (characters) and are placeholders for them.
  • Functions can containe blocks of code and are placeholders for the blocks.


Further links