German Version
To write programs for the Arduino, we can use an integrated programming environment (IDE),that was developed by David Mellis and heavily influenced by Processing. This makes it particularly easy for us to get started - but there are a few differences when we are programming for a microcontroller. For Arduino we use a simplified variant of C / C ++. Since Arduino is based on Processing, the interface and syntax of both programs look very similar. The programming environment also looks very similar.
...
Integrated Programming Environment (IDE)
Basic structure
The basic structure of an arduino program always includes the setup () and loop () functions. Setup () is only called once when the program is started (either after transferring it to the board or after pressing the reset button). In this function, the pins are set as inputs or outputs, the serial interface is activated or external libraries are initialized. The loop () function runs continuously as long as the Arduino is switched on.
Code Block |
---|
language | java |
---|
title | Beispiel GrunstrukturExample Code |
---|
collapse | true |
---|
|
#define LED_PIN 13
int counter = 0;
void setup()
{
pinMode(LED_PIN, OUTPUT);
}
void loop()
{
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(1000);
counter++;
} |
...
int (byte, int, uint, long, ulong)
Ganzzahlige VariablenA whole number
I.e. int x = 22;
float (double)
Floating Point number
I.e. float y = 1.234;
...
Code Block |
---|
language | java |
---|
title | Beispiel Arithmetische OperatorenExample Arithmetic Operators |
---|
collapse | true |
---|
|
x = 3+2; // x = 5
y = 2-1; // y = 1
z = 5*2; // z = 10
i = 10/2; // i = 5
r = 9%5; // r = 4 |
Comparison operators (,=,==,!=)
Code Block |
---|
language | java |
---|
title | Beispiel Vergleichende OperatorenExample comparative operators |
---|
collapse | true |
---|
|
if(100 != 120) // Condition
{
// Performed action
} |
...
Code Block |
---|
language | java |
---|
title | Beispiel Boolsche OperatorenExample Boolean Operators |
---|
collapse | true |
---|
|
if(digitalRead(2) == HIGH) // Condition
{
// Performed action
} |
...
Code Block |
---|
language | java |
---|
title | Beispiel Example if... |
---|
collapse | true |
---|
|
if(x>120)
{
// Action when x is greater than 120
} |
...
Code Block |
---|
language | java |
---|
title | Beispiel Example if...else |
---|
collapse | true |
---|
|
if(x>120)
{
// Action when x is greater than 120
}
else
{
// Action when x is smaller than or exactly 120
} |
...
Code Block |
---|
language | java |
---|
title | Beispiel Example for... |
---|
collapse | true |
---|
|
for(int i=0; i<=255; i++)
{
// action repeated 255 times, with i++ every iteration
} |
...
Code Block |
---|
language | java |
---|
title | Beispiel Example switch... |
---|
collapse | true |
---|
|
switch(var)
{
case 1:
// Action when var equals 1
break;
case 2:
// Action when var equals 2
break;
default:
// Action when var is niether 1 or 2
} |
...
Code Block |
---|
language | java |
---|
title | Beispiel Example while... |
---|
collapse | true |
---|
|
var = 0;
while(var<120)
{
// Action repeated 120 times
var++;
} |
...
Code Block |
---|
language | java |
---|
title | Beispiel Example break... |
---|
collapse | true |
---|
|
for(int i=0; i<200; i++)
{
digitalWrite(5,i);
sensorValue = analogRead(0);
if(sensorValue>200)
{
i = 0; // Exit loop early
break;
}
delay(100);
} |
...
Code Block |
---|
language | java |
---|
title | Beispiel Example return... |
---|
collapse | true |
---|
|
int checkSensor()
{
if(analogRead>200)
{
return 1; // checkSensor function returns a 1
}
else
{
return 0; // checkSensor function returns a 1
}
} |
...