Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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
languagejava
titleBeispiel GrunstrukturExample Code
collapsetrue
#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
languagejava
titleBeispiel Arithmetische OperatorenExample Arithmetic Operators
collapsetrue
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
languagejava
titleBeispiel Vergleichende OperatorenExample comparative operators
collapsetrue
if(100 != 120) // Condition
{
  // Performed action
}

...

Code Block
languagejava
titleBeispiel Boolsche OperatorenExample Boolean Operators
collapsetrue
if(digitalRead(2) == HIGH) // Condition
{
   // Performed action
}

...

Code Block
languagejava
titleBeispiel Example if...
collapsetrue
if(x>120)
{
  // Action when x is greater than 120
}

...

Code Block
languagejava
titleBeispiel Example if...else
collapsetrue
if(x>120)
{
 // Action when x is greater than 120
}
else
{
  // Action when x is smaller than or exactly 120
}

...

Code Block
languagejava
titleBeispiel Example for...
collapsetrue
for(int i=0; i<=255; i++) 
{
  // action repeated 255 times, with i++ every iteration 
}

...

Code Block
languagejava
titleBeispiel Example switch...
collapsetrue
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
languagejava
titleBeispiel Example while...
collapsetrue
var = 0;
while(var<120)
{
  // Action repeated 120 times
  var++;
}

...

Code Block
languagejava
titleBeispiel Example break...
collapsetrue
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
languagejava
titleBeispiel Example return...
collapsetrue
int checkSensor()
{
  if(analogRead>200)
  {
    return 1; // checkSensor function returns a 1
  }
  else
  {
    return 0; // checkSensor function returns a 1
  }
}

...