Arduino crash course

Aus www.wiki.ardumower.de
Version vom 13. September 2014, 10:33 Uhr von Alexanderg (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „= 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 Ar…“)

(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Wechseln zu: Navigation, Suche

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 );


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.