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!