Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
Confluence
/
Arduino Programming (EN)

Arduino Programming (EN)

Sep 08, 2022

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
  }
}
, multiple selections available,
For you

Electrical Engineering
Results will update as you type.
  • Aesthetic elements in Eagle
  • Actuators and Outputs
  • Aktuatoren (DE)
  • Analog Input
  • Analog Sensors
  • Arduino Grundlagen
  • Arduino Programmieren
  • Arduino und Processing
  • Shift Registers
  • Carvey
  • Capacitors
  • Bread Boards
  • Digitaler Input
  • Digitaler Output
  • Eagle
  • Energy Harvesting
  • H-Bridge
  • I2C (en)
  • Interrupt Service Routine
  • Mosfet Transistor
  • LED
  • PCBs Herstellen
  • Pololu Servo Controller
  • Relays as Switches
  • Pulse Width Modulation
  • RFiD
  • Schrittmotorentreiber
  • SD Karten
  • Signal Filtering
  • Transistors as Switches
  • Widerstand
  • PCB Manufacturing
  • PlatformIO
  • Lynx Smart Motion Servo
  • Electronics Basics (EN)
  • Arduino Basics (en)
  • Arduino Programming (EN)
  • Digital Outputs (en)
  • Digital Input (en)
  • Serial Communication (en)
  • Pulse Width Modulation (en)
  • Resistors (en)
  • Arduino and P5.js
  • Arduino and ml5.js
  • Project box 2021
  • Servo Motors
  • H-Bridge (DE)
  • Networking
  • Bit Shifting
  • C++ (Arduino) Variables
  • Stepper Motor Drivers
  • Interrupt Service Routine (en)
  • Common Arduino Problems
  • Finite State Machine
  • Voltage Regulators
  • Arduino USB HID (Human Interface Devices)

{"serverDuration": 38, "requestCorrelationId": "0171380c219c4ca9a3e00b8cad82c2dc"}