Interaction Design WikiElectrical Engineering

Arduino Programming (EN)

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.

Example Code Expand source
#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++;
}

Variables

int (byte, int, uint, long, ulong)
A whole number
I.e. int x = 22;

float (double)
Floating Point number 
I.e. float y = 1.234;

char
Character 
I.e. char z = “a”;

String
A collection of characters 
I.e. String testString = “Arduino”;

boolean
A binary variable
I.e. boolean state = false;

Operators 

Arithmetic operators (+,-,*,/,=,%)

Example Arithmetic Operators Expand source
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  (,=,==,!=)

Example comparative operators Expand source
if(100 != 120) // Condition
{
  // Performed action
}

Boolean Operators (&&,||,!)

Example Boolean Operators Expand source
if(digitalRead(2) == HIGH) // Condition
{
   // Performed action
}

Compound operators (++,–,+=,-=,*=,/=)

Beispiel Zusammengesetzte Operatoren Expand source
x++ // x plus 1
y– // y minus 1
z+=2 // z plus 2
i-=5 // i minus 5

Conditionals Examples:

Example if... Expand source
if(x>120)
{
  // Action when x is greater than 120
}
Example if...else Expand source
if(x>120)
{
 // Action when x is greater than 120
}
else
{
  // Action when x is smaller than or exactly 120
}
Example for... Expand source
for(int i=0; i<=255; i++) 
{
  // action repeated 255 times, with i++ every iteration 
}
Example switch... Expand source
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
}
Example while... Expand source
var = 0;
while(var<120)
{
  // Action repeated 120 times
  var++;
}
Example break... Expand source
for(int i=0; i<200; i++)
{
  digitalWrite(5,i);
  sensorValue = analogRead(0);
  if(sensorValue>200)
  {
    i = 0; // Exit loop early
    break;
  }
  delay(100);
}
Example return... Expand source
int checkSensor()
{
  if(analogRead>200)
  {
    return 1; // checkSensor function returns a 1
  }
  else
  {
    return 0; // checkSensor function returns a 1
  }
}