Sie sind auf Seite 1von 6

setup()

The setup() function is called when a sketch starts. Use it to initialize variables, pin modes, start using libraries, etc. The setup function will only run once, after each powerup or reset of the Arduino board. Example
int buttonPin = 3; void setup() { Serial.begin(9600); pinMode(buttonPin, INPUT); } void loop() { // ... }

loop()
After creating a setup() function, which initializes and sets the initial values, the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the Arduino board. Example
const int buttonPin = 3; // setup initializes serial and the button pin void setup() { Serial.begin(9600); pinMode(buttonPin, INPUT); } // loop checks the button pin each time, // and will send serial if it is pressed void loop() { if (digitalRead(buttonPin) == HIGH) Serial.write('H'); else Serial.write('L');

delay(1000); }

if (conditional) and ==, !=, <, > (comparison operators)


if, which is used in conjunction with a comparison operator, tests whether a certain condition has

been reached, such as an input being above a certain number. The format for an if test is:
if (someVariable > 50) { // do something here }

The program tests to see if someVariable is greater than 50. If it is, the program takes a particular action. Put another way, if the statement in parentheses is true, the statements inside the brackets are run. If not, the program skips over the code. The brackets may be omitted after an if statement. If this is done, the next line (defined by the semicolon) becomes the only conditional statement.
if (x > 120) digitalWrite(LEDpin, HIGH); if (x > 120) digitalWrite(LEDpin, HIGH); if (x > 120){ digitalWrite(LEDpin, HIGH); } if (x > 120){ digitalWrite(LEDpin1, HIGH); digitalWrite(LEDpin2, HIGH); }

// all are correct

The statements being evaluated inside the parentheses require the use of one or more operators: Comparison Operators:
x x x x x x == != < > <= >= y y y y y y (x (x (x (x (x (x is is is is is is equal to y) not equal to less than y) greater than less than or greater than y) y) equal to y) or equal to y)

W a rn i n g:
Beware of accidentally using the single equal sign (e.g. if (x = 10) ). The single equal sign is the assignment operator, and sets x to 10 (puts the value 10 into the variable x). Instead use the double equal sign (e.g. if (x == 10) ), which is the comparison operator, and tests whether x is equal to

10 or not. The latter statement is only true if x equals 10, but the former statement will always be true. This is because C evaluates the statement if (x=10) as follows: 10 is assigned to x (remember that the single equal sign is the assignment operator), so x now contains 10. Then the 'if' conditional evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE. Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using an 'if' statement. Additionally, the variable x will be set to 10, which is also not a desired action. if can also be part of a branching control structure using the if...else] construction.

if / else
if/else allows greater control over the flow of code than the basic if statement, by allowing multiple tests to be grouped together. For example, an analog input could be tested and one action taken if the input was less than 500, and another action taken if the input was 500 or greater. The code would look like this:
if (pinFiveInput < 500) { // action A } else { // action B }

else can proceed another if test, so that multiple, mutually exclusive tests can be run at the same time. Each test will proceed to the next one until a true test is encountered. When a true test is found, its associated block of code is run, and the program then skips to the line following the entire if/else construction. If no test proves to be true, the default else block is executed, if one is present, and sets the default behavior. Note that an else if block may be used with or without a terminating else block and vice versa. An unlimited number of such else if branches is allowed.
if (pinFiveInput < 500) { // do Thing A } else if (pinFiveInput >= 1000) { // do Thing B } else { // do Thing C }

Another way to express branching, mutually exclusive tests, is with the switch case statement.

for statements
Desciption
The for statement is used to repeat a block of statements enclosed in curly braces. An increment counter is usually used to increment and terminate the loop. The for statement is useful for any repetitive operation, and is often used in combination with arrays to operate on collections of data/pins. There are three parts to the for loop header:

for (initialization; condition; increment) {


//statement(s); }

The initialization happens first and exactly once. Each time through the loop, the condition is tested; if it's true, the statement block, and the increment is executed, then the condition is tested again. When the condition becomes false, the loop ends.

Example
// Dim an LED using a PWM pin int PWMpin = 10; // LED in series with 470 ohm resistor on pin 10

void setup() { // no setup needed } void loop() { for (int i=0; i <= 255; i++){ analogWrite(PWMpin, i); delay(10); } }

Coding Tips
The C for loop is much more flexible than for loops found in some other computer languages, including BASIC. Any or all of the three header elements may be omitted, although the semicolons are required. Also the statements for initialization, condition, and increment can be any valid C statements with unrelated variables, and use any C datatypes including floats. These types of unusual for statements may provide solutions to some rare programming problems. For example, using a multiplication in the increment line will generate a logarithmic progression:
for(int x = 2; x < 100; x = x * 1.5){ println(x); }

Generates: 2,3,4,6,9,13,19,28,42,63,94 Another example, fade an LED up and down with one for loop:
void loop() { int x = 1; for (int i = 0; i > -1; i = i + x){ analogWrite(PWMpin, i); if (i == 255) x = -1; delay(10); } }

// switch direction at peak

switch / case statements


Like if statements, switch...case controls the flow of programs by allowing programmers to specify different code that should be executed in various conditions. In particular, a switch statement compares the value of a variable to the values specified in case statements. When a case statement is found whose value matches that of the variable, the code in that case statement is run.

The break keyword exits the switch statement, and is typically used at the end of each case. Without a break statement, the switch statement will continue executing the following expressions ("fallingthrough") until a break, or the end of the switch statement is reached. Example
switch (var) { case 1: //do something when var equals 1 break; case 2: //do something when var equals 2 break; default: // if nothing else matches, do the default // default is optional }

Syntax
switch (var) { case label: // statements break; case label: // statements break; default: // statements }

Das könnte Ihnen auch gefallen