Arduino Crash Kurs
Inhaltsverzeichnis
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. Übertrage obige Zeilen in die Arduino IDE.
2. Baue folgende Zeile in die setup-Funktion ein:
pinMode(13, OUTPUT); // Pin 13 soll als Ausgang konfiguriert werden (für LED ansteuern)
3. 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 Zeilen 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 jeweils an geeigneter Stelle 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)
}
3. Ein Taster
Wir schließen einen Taster an den Arduino. Die LED soll nur dann auslösen wenn der Taster gedrückt wird.
Aufgabe: 1. Schliesse den Taster an Pin 5 an und definiere den Pin:
int pinSwitch = 5; // Pin für Taster definieren
2. Baue folgende Zeile in die setup-Funktion ein:
pinMode(pinSwitch, INPUT_PULLUP); // Taster soll als Eingang (mit Pull-Up) konfiguriert werden
3. Baue folgende Zeilen in die loop-Funktion ein:
int zustand = digitalRead(pinSwitch); // Taster in Variable "zustand" einlesen
Serial.print("Taster Zustand: ");
Serial.println( zustand );
digitalWrite( pinLED, zustand ); // LED passend ansteuern delay(1000);
3. Lösung
int pinSwitch = 5; // Pin für Taster
int pinLED = 13; // Pin für LED
void setup()
{
pinMode(pinSwitch, INPUT_PULLUP); // Taster soll als Eingang (mit Pull-Up) konfiguriert werden
pinMode(pinLED, OUTPUT); // LED soll als Ausgang konfiguriert werden
Serial.begin(115200); // serielle Konsole mit 115200 Baud konfigurieren
}
void loop()
{
int zustand = digitalRead(pinSwitch); // Taster in Variable einlesen
Serial.print("Taster ist ");
Serial.println( zustand );
digitalWrite( pinLED, zustand ); // LED passend ansteuern
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