Arduino Crash Kurs

Aus www.wiki.ardumower.de
Version vom 1. April 2016, 10:12 Uhr von Alexanderg (Diskussion | Beiträge) (2. Dein erstes Programm mit Variablen und serieller Konsole)

Wechseln zu: Navigation, Suche

1. Dein erstes Programm

Die setup-Funktion wird beim Programmstart einmal durchlaufen. Hier kann man Dinge durchführen, die nur einmal nach dem Arduino-Reset ausgeführt werden sollen. Die Loop-Funktion hingegen wird unendlich oft durchlaufen.

 // Die setup-Funktion wird beim Programmstart einmal durchlaufen.
 void setup() 
 {

 }

 // Die loop-Funktion wird unendlich oft wiederholt.
 void loop() 
 {

 }


Aufgabe:

1. Baue folgende Zeile in die setup-Funktion ein:

  pinMode(13, OUTPUT);  // Pin 13 soll als Ausgang konfiguriert werden

2. Baue folgende Zeilen in die loop-Funktion ein:

  digitalWrite(13, HIGH);    // Pin 13 soll auf HIGH ( 5 Volt ) gesetzt werden
  delay(1000);               // Warte 1000 Millisekunden (= 1 Sekunde)
  digitalWrite(13, LOW);     // Pin 13 soll auf LOW ( 0 Volt/Masse ) gesetzt werden
  delay(1000);               // Warte 1000 Millisekunden (= 1 Sekunde)

1. Lösung

 // Die setup-Funktion wird beim Programmstart einmal durchlaufen.
 void setup() 
 {
   pinMode(13, OUTPUT);  // Pin 13 soll als Ausgang konfiguriert werden
 }

 // Die loop-Funktion wird unendlich oft wiederholt.
 void loop() 
 {
   digitalWrite(13, HIGH);    // Pin 13 soll auf HIGH ( 5 Volt ) gesetzt werden
   delay(1000);               // Warte 1000 Millisekunden (= 1 Sekunde)
   digitalWrite(13, LOW);     // Pin 13 soll auf LOW ( 0 Volt/Masse ) gesetzt werden
   delay(1000);               // Warte 1000 Millisekunden (= 1 Sekunde)
 }

2. Dein erstes Programm mit Variablen und serieller Konsole

Aufgabe:

1. Ersetze die Pin-Nummer "13" durch eine Variable welche Du außerhalb von Funktionen definierst:

int pinLED = 13;  // die Variable namens "pinLED" soll den Wert 13 bekommen

2. Ersetze nun überall dort die "13" durch "pinLED" wo der LED-Pin benutzt wird.

3. Teste Dein Programm einmal.

4. Baue folgende Zeile in die setup-Funktion ein:

  Serial.begin(115200);     // serielle Konsole für 115200 Baud konfigurieren
  Serial.println("START");  // Text auf serielle Konsole ausgeben

5. Baue folgende Zeilen in die loop-Funktion ein:

  Serial.println("LED EIN");     // Text auf serielle Konsole ausgeben
  Serial.println("LED AUS");     // Text auf serielle Konsole ausgeben

2. Lösung

 int pinLED = 13;  // die Variable namens "pinLED" soll den Wert 13 bekommen

 // Die setup-Funktion wird beim Programmstart einmal durchlaufen.
 void setup() 
 {
   pinMode(pinLED, OUTPUT);  // PinLED soll als Ausgang konfiguriert werden
   Serial.begin(115200);     // serielle Konsole für 115200 Baud konfigurieren
   Serial.println("START");  // Text auf serielle Konsole ausgeben
 }

 // Die loop-Funktion wird unendlich oft wiederholt.
 void loop() 
 {
   digitalWrite(pinLED, HIGH);    // PinLED soll auf HIGH ( 5 Volt ) gesetzt werden
   Serial.println("LED EIN");     // Text auf serielle Konsole ausgeben
   delay(1000);                   // Warte 1000 Millisekunden (= 1 Sekunde)
   digitalWrite(pinLED, LOW);     // PinLED soll auf LOW ( 0 Volt/Masse ) gesetzt werden
   Serial.println("LED AUS");     // Text auf serielle Konsole ausgeben
   delay(1000);                   // Warte 1000 Millisekunden (= 1 Sekunde)
 }

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