Preface
Please refer to the official Arduino Programming Language reference (also stored locally if Arduino installed)
Arduino is based on the C programming language, though it has some minor differences.
C-Programmierung (Wiki Book) Deutsch - C-Programming (Wiki Book) English
Ein günstiges (10,- €) und sehr gutes Buch ist: Helmut Erlenkötter "C Programmieren von Anfang an", ISBN 3499600749
Data Types
The length of the following datatypes is referring to Arduino. Note that lengths can vary depending on the platform - even if the language is C!
All datatypes are more or less similar in most of the modern programming languages. In some languages, variables must be explicitly bound to a certain datatype (as in C), in other languages, the compilers do this job for you.
Variables can be typecasted (= converted from one datatype to another) by using the relevant functions: int(), long() or float().
void 0 bytes, 0bit, 0
boolean 1 byte, 1 bit, 0/1
char 1 byte, 8 bit, -128..127
byte 1 bytes, 8 bit, 0..255
int 2 bytes, 16 bit, -32768..32767
unsigned int 2 bytes, 16 bit, 0..65535
long 4 bytes, 32 bit, -2147483648..2147483647
unsigned long 4 bytes, 32 bit, 0..4294967295
float 4 bytes, 32 bit -3.4028235E+38..3.4028235E+38
double 4 bytes, 32 bit
array
String
...
Variables
Variables have to be declared by:
type name;
before they are used!
e.g.
int x;
byte b;
Variable names mustn't contain any special characters, especially no spaces. Try to use descriptive names for your variables. It's widely regarded good practise to use lower camel case for improved readability.
// This is nice lower camel case
boolean isEnabled;
Variables can be declared and instantiated in one step:
type name = value;
e.g.
int x = 5;
long r = random(255);
int values[6] = {0,1,2,3,4,5};
Variable Scope
If a variable is declared inside a function, it is a local variable, if declared outside any function, it's scope is global, that means it can be accessed from anywhere.
// Global variable
int x = 7;
void myFunction() {
// Local variable
int a = 8;
}
Never give local variables and global variables the same name. Two local variables can have the same name only when they are defined in different functions!
// This is not ok!
int x = 5;
void myFunction() {
int x = 3;
}
// This however is ok
void functionA() {
int a = 7;
}
void functionB() {
int a = 12;
}
Arrays
Arrays are data Structures holding multiple elements of the same datatype.
Declaring Arrays
Declaring Arrays is pretty much like declaring variables only for arrays the name is followed by square brackets '[]' enclosing an optional length parameter.
type name[length];
You don't have to define a length parameter but if if you do, the Array wont hold more elements than specified.
For example:
// Create an array without assigning values
int myArray[];
// Create a integer Array with 3 spaces
int someNumbers[3] = {1, 2, 3};
// Create a integer Array with an unlimited length
int moreNumbers[] = {1, 2, 3, 4};
// Create a String (in C this is an Array of chars)
int myWord[] = "hello";
Accessing Arrays
You can access variables stored in an Array by their index. Indices go from 0 to length-1. In an Array with 3 elements the first element has the index 0 and the last element has the index 2.
int someNumbers[] = {45, 5, 6};
int firstElement = someNumbers[0]; // = 45
int secondElement = someNumbers[1]; // = 5
int thirdElement = someNumbers[2]; // = 6
Creating an Array of Strings
Since Strings itself already are Arrays, creating an Array of Strings is basically creating two-dimensional Arrays. Since all this goes pretty deep into C programming it's best to just follow the example. Note the extra asterisk after the type.
char* myWords[] = {"One", "Two", "Three"};
Main Functions
An Arduino program consists of two basic functions: start() and loop():
void start() {
// init code that is executed ONCE only: at the start of the program (eg. when power is attached)
}
void loop() {
// code that gets executed as long as the arduino is powered
}
Functions
returnType functionName(parameterType parameter) { // Instruction doWhatever(); }
e.g. the two main Arduino functions:
void setup() {
// Anweisungen, die einmal beim Start des Programms ausgeführt werden
doWhatever();
}
void loop() {
// Anweisungen, die fortlaufend ausgeführt werden
doWhatever();
}
Instructions can be summarised as a function, e.g.:
void randomizeColor() {
// Instructions
doWhatever();
// here may be a "return;" (but it mustn't, it's optional)
return;
}
or
int multipliziere(int a, int b) {
// Multipliziere a und b und gebe diesen Wert zurück
return a * b;
}
Calling Functions
variable = funktionsName(parameter);
e.g.:
int x = multipliziere(2,5); // x = 10;
delay(1000);
c = min(x,y);
... see the Arduino Reference to find more pre-defined functions, that are ready to be used!
Diese Seite ist Teil des Werkmoduls WS08:Wearables von Michael Markert für GMU - Gestaltung medialer Umgebungen an der Bauhaus-Universität Weimar.