emailconfirmed, nsInternRO, nsInternRW, Administrators
3,356
edits
No edit summary |
m (→Data Types) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
Arduino is based on the C programming language, though it has some minor differences. | Arduino is based on the C programming language, though it has some minor differences. | ||
[http://de.wikibooks.org/wiki/C-Programmierung C-Programmierung (Wiki Book) Deutsch] | * [http://de.wikibooks.org/wiki/C-Programmierung C-Programmierung (Wiki Book) Deutsch] | ||
* [http://en.wikibooks.org/wiki/C_Programming C-Programming (Wiki Book) English] | |||
Ein günstiges (10,- €) und sehr gutes Buch ist: Helmut Erlenkötter "C Programmieren von Anfang an", ISBN 3499600749 | Ein günstiges (10,- €) und sehr gutes Buch ist: | ||
* Helmut Erlenkötter "C Programmieren von Anfang an", ISBN 3499600749 | |||
== Data Types == | == Data Types == | ||
Line 17: | Line 19: | ||
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(). | ||
{{Template:Datatypes}} | |||
== Variables == | == Variables == | ||
Line 41: | Line 28: | ||
e.g. | e.g. | ||
< | <source lang="c"> | ||
int x; | int x; | ||
byte b; | byte b; | ||
</ | </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. | ||
< | <source lang="c"> | ||
// This is nice lower camel case | // This is nice lower camel case | ||
boolean isEnabled; | boolean isEnabled; | ||
</ | </source> | ||
Variables can be declared and instantiated in one step: | Variables can be declared and instantiated in one step: | ||
Line 58: | Line 45: | ||
e.g. | e.g. | ||
< | <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}; | ||
</ | </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. | ||
< | <source lang="c"> | ||
// Global variable | // Global variable | ||
int x = 7; | int x = 7; | ||
Line 73: | Line 60: | ||
int a = 8; | int a = 8; | ||
} | } | ||
</ | </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! | ||
< | <source lang="c"> | ||
// This is not ok! | // This is not ok! | ||
int x = 5; | int x = 5; | ||
Line 92: | Line 79: | ||
int a = 12; | int a = 12; | ||
} | } | ||
</ | </source> | ||
=== Arrays === | === Arrays === | ||
Line 100: | Line 87: | ||
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. | ||
< | <source lang="c"> | ||
type name[length]; | type name[length]; | ||
</ | </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: | ||
< | <source lang="c"> | ||
// Create an array without assigning values | // Create an array without assigning values | ||
int myArray[]; | int myArray[]; | ||
Line 117: | Line 104: | ||
// 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"; | ||
</ | </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. | ||
< | <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 | ||
</ | </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. | ||
< | <source lang="c"> | ||
char* myWords[] = {"One", "Two", "Three"}; | char* myWords[] = {"One", "Two", "Three"}; | ||
</ | </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(): | ||
< | <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 132: | ||
// code that gets executed as long as the arduino is powered | // code that gets executed as long as the arduino is powered | ||
} | } | ||
</ | </source> | ||
Line 158: | Line 145: | ||
e.g. the two main Arduino functions: | e.g. the two main Arduino functions: | ||
< | <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 154: | ||
doWhatever(); | doWhatever(); | ||
} | } | ||
</ | </source> | ||
Instructions can be summarised as a function, e.g.: | Instructions can be summarised as a function, e.g.: | ||
< | <source lang="c"> | ||
void randomizeColor() { | void randomizeColor() { | ||
// Instructions | // Instructions | ||
Line 178: | Line 165: | ||
return; | return; | ||
} | } | ||
</ | </source> | ||
or | or | ||
< | <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; | ||
} | } | ||
</ | </source> | ||
Line 193: | Line 180: | ||
e.g.: | e.g.: | ||
< | <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); | ||
</ | </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! | ||
Line 207: | Line 194: | ||
----- | ----- | ||
Diese Seite ist Teil des Werkmoduls ''[[GMU:Wearables]]'' von Michael Markert für GMU - Gestaltung medialer Umgebungen an der Bauhaus-Universität Weimar. | Diese Seite ist Teil des Werkmoduls ''[[GMU:Wearables]]'' von Michael Markert für GMU - Gestaltung medialer Umgebungen an der Bauhaus-Universität Weimar. | ||
[[Category:Arduino]] | |||
[[Category:C]] | |||
[[Category:Electronics]] | |||
[[Category:Grundlagen]] | |||
[[Category:Hardware]] | |||
[[Category:Interfaces]] | |||
[[Category:Michael Markert]] | |||
[[Category:Programmiersprachen]] |