ARDUINO
ARDUINO
SUMÁRIO
1.ESTRUTURAS
← 1.1 setup()
← 1.2 loop()
C o n t r ol S t r u c t u r e s
← 1.3 if
← 1.4 if...else
← 1.5 for
← 1.6 switch case
← 1.7 while
← 1.8 do... while
← break
← continue
← return
← goto
Further Syntax
← ; (semicolon)
← {} (curly braces)
← // (single line comment)
← /* */ (multi-line comment)
← #define
← #include
A r i t h m e t i c O p e r a t or s
← = (assignment operator)
← + (addition)
← - (subtraction)
← * (multiplication)
← / (division)
← % (modulo)
Comparison Operators
← == (equal to)
← != (not equal to)
← < (less than)
← > (greater than)
← <= (less than or equal to)
← >= (greater than or equal to)
B o o l e a n O p e r at o r s
← && (and)
← || (or)
← ! (not)
P o i nt e r A c c e s s O p e r a t o r s
← * dereference operator
← & reference operator
B i t w i s e O p e r a t or s
← & (bitwise and)
← | (bitwise or)
← ^ (bitwise xor)
← ~ (bitwise not)
← << (bitshift left)
← >> (bitshift right)
Compound Operators
← ++ (increment)
← -- (decrement)
← += (compound addition)
← -= (compound subtraction)
← *= (compound multiplication)
← /= (compound division)
← &= (compound bitwise and)
← |= (compound bitwise or)
VARIÁVEIS
Constants
← HIGH | LOW
← INPUT | OUTPUT
← true | false
← integer constants
← floating point constants
Data Types
← void
← boolean
← char
← unsigned char
← byte
← int
← unsigned int
← word
← long
← unsigned long
← float
← double
← string - char array
← String - object
← array
Conversion
← char()
← byte()
← int()
← word()
← long()
← float()
V a r i a bl e S c o p e & Q u a l i f i e r s
← variable scope
← static
← volatile
← const
Utilities
← sizeof()
FUNÇÕES
Digital I/O
← pinMode()
← digitalWrite()
← digitalRead()
Analog I/O
← analogReference()
← analogRead()
← analogWrite() - PWM
Advanced I/O
← tone()
← noTone()
← shiftOut()
← shiftIn()
← pulseIn()
Time
← millis()
← micros()
← delay()
← delayMicroseconds()
Math
← min()
← max()
← abs()
← constrain()
← map()
← pow()
← sqrt()
Trigonometry
← sin()
← cos()
← tan()
Random Numbers
← randomSeed()
← random()
External Interrupts
← attachInterrupt()
← detachInterrupt()
Interrupts
← interrupts()
← noInterrupts()
Communication
← Serial
← 1.1 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()
{
// ...
}
← 1.2 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
int buttonPin = 3;
delay(1000);
}
← 1.3 if
if (conditional) and ==, !=, <, > (comparison operators)
if (x > 120)
digitalWrite(LEDpin, HIGH);
if (x > 120){
digitalWrite(LEDpin1, HIGH);
digitalWrite(LEDpin2, HIGH);
} // all are
correct
Warning:
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.
← 1.4 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.
1.5 for
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.
← Syntax
← switch (var) {
← case label:
← // statements
← break;
← case label:
← // statements
← break;
← default:
← // statements
← }
←
← Parameters
← var: the variable whose value to compare to the various cases
← label: a value to compare the variable to
← 1.7 while
Description
while loops will loop continuously, and infinitely, until the
expression inside the parenthesis, () becomes false. Something must
change the tested variable, or the while loop will never exit. This
could be in your code, such as an incremented variable, or an
external condition, such as testing a sensor.
Syntax
while(expression){
// statement(s)
}
Parameters
expression - a (boolean) C statement that evaluates to true or false
Example
var = 0;
while(var < 200){
// do something repetitive 200 times
var++;
}
←
← 1.8 do... while
The do loop works in the same manner as the while loop, with the
exception that the condition is tested at the end of the loop, so the
do loop will always run at least once.
do
{
// statement block
} while (test condition);
Example
do
{
delay(50); // wait for sensors to
stabilize
x = readSensors(); // check the sensors