Crash Course Programming for Arduino: Difference between revisions

From Medien Wiki
No edit summary
(converted for new wiki & geshi syntax)
Line 17: Line 17:
Variables can be typecasted (= converted from one datatype to another) by using the relevant functions: int(), long() or float().
Variables can be typecasted (= converted from one datatype to another) by using the relevant functions: int(), long() or float().


<code lang="c">
<source lang="c">
void 0 bytes, 0bit, 0
void 0 bytes, 0bit, 0
boolean 1 byte, 1 bit, 0/1
boolean 1 byte, 1 bit, 0/1
Line 32: Line 32:
String
String
...
...
</code>
</source>


== Variables ==
== Variables ==
Line 41: Line 41:


e.g.  
e.g.  
<code lang="c">
<source lang="c">
int x;
int x;
byte b;
byte b;
</code>
</source>


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 [http://en.wikipedia.org/wiki/Camel_case#Variations_and_synonyms lower camel case] for improved readability.
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 [http://en.wikipedia.org/wiki/Camel_case#Variations_and_synonyms lower camel case] for improved readability.


<code lang="c">
<source lang="c">
// This is nice lower camel case
// This is nice lower camel case
boolean isEnabled;
boolean isEnabled;
</code>
</source>


Variables can be declared and instantiated in one step:
Variables can be declared and instantiated in one step:
Line 58: Line 58:


e.g.  
e.g.  
<code lang="c">
<source lang="c">
int x = 5;
int x = 5;
long r = random(255);
long r = random(255);
int values[6] = {0,1,2,3,4,5};
int values[6] = {0,1,2,3,4,5};
</code>
</source>
===Variable Scope===
===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.
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.


<code lang="c">
<source lang="c">
// Global variable
// Global variable
int x = 7;
int x = 7;
Line 73: Line 73:
   int a = 8;
   int a = 8;
}
}
</code>
</source>


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!
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!


<code lang="c">
<source lang="c">
// This is not ok!
// This is not ok!
int x = 5;
int x = 5;
Line 92: Line 92:
   int a = 12;
   int a = 12;
}
}
</code>
</source>


=== Arrays ===
=== Arrays ===
Line 100: Line 100:
Declaring Arrays is pretty much like declaring variables only for arrays the name is followed by square brackets '[]' enclosing an optional length parameter.
Declaring Arrays is pretty much like declaring variables only for arrays the name is followed by square brackets '[]' enclosing an optional length parameter.


<code lang="c">
<source lang="c">
type name[length];
type name[length];


</code>
</source>


You don't have to define a length parameter but if if you do, the Array wont hold more elements than specified.
You don't have to define a length parameter but if if you do, the Array wont hold more elements than specified.


For example:
For example:
<code lang="c">
<source lang="c">
// Create an array without assigning values
// Create an array without assigning values
int myArray[];
int myArray[];
Line 117: Line 117:
// Create a String (in C this is an Array of chars)
// Create a String (in C this is an Array of chars)
int myWord[] = "hello";
int myWord[] = "hello";
</code>
</source>


==== Accessing Arrays ====
==== 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.
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.
<code lang="c">
<source lang="c">
int someNumbers[] = {45, 5, 6};
int someNumbers[] = {45, 5, 6};
int firstElement = someNumbers[0]; // = 45
int firstElement = someNumbers[0]; // = 45
int secondElement = someNumbers[1]; // = 5
int secondElement = someNumbers[1]; // = 5
int thirdElement = someNumbers[2]; // = 6
int thirdElement = someNumbers[2]; // = 6
</code>
</source>


==== Creating an Array of Strings ====
==== 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.
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.
<code lang="c">
<source lang="c">
char* myWords[] = {"One", "Two", "Three"};
char* myWords[] = {"One", "Two", "Three"};
</code>
</source>


== Main Functions ==
== Main Functions ==


An Arduino program consists of two basic functions: start() and loop():
An Arduino program consists of two basic functions: start() and loop():
<code lang="c">
<source lang="c">
void start() {
void start() {
   // init code that is executed ONCE only: at the start of the program (eg. when power is attached)
   // init code that is executed ONCE only: at the start of the program (eg. when power is attached)
Line 145: Line 145:
   // code that gets executed as long as the arduino is powered
   // code that gets executed as long as the arduino is powered
}
}
</code>
</source>




Line 158: Line 158:
e.g. the two main Arduino functions:
e.g. the two main Arduino functions:


<code lang="c">
<source lang="c">
void setup() {
void setup() {
// Anweisungen, die einmal beim Start des Programms ausgeführt werden
// Anweisungen, die einmal beim Start des Programms ausgeführt werden
Line 167: Line 167:
doWhatever();
doWhatever();
}
}
</code>
</source>


Instructions can be summarised as a function, e.g.:
Instructions can be summarised as a function, e.g.:


<code lang="c">
<source lang="c">
void randomizeColor() {
void randomizeColor() {
// Instructions
// Instructions
Line 178: Line 178:
return;
return;
}
}
</code>
</source>
or
or
<code lang="c">
<source lang="c">
int multipliziere(int a, int b) {
int multipliziere(int a, int b) {
// Multipliziere a und b und gebe diesen Wert zurück
// Multipliziere a und b und gebe diesen Wert zurück
return a * b;
return a * b;
}
}
</code>
</source>




Line 193: Line 193:


e.g.:
e.g.:
<code lang="c">
<source lang="c">
int x = multipliziere(2,5); // x = 10;
int x = multipliziere(2,5); // x = 10;
delay(1000);
delay(1000);
c = min(x,y);
c = min(x,y);
</code>
</source>


... see the Arduino Reference to find more pre-defined functions, that are ready to be used!
... see the Arduino Reference to find more pre-defined functions, that are ready to be used!

Revision as of 22:20, 28 November 2009

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 GMU:Wearables von Michael Markert für GMU - Gestaltung medialer Umgebungen an der Bauhaus-Universität Weimar.