Upload
talangocday
View
218
Download
0
Embed Size (px)
Citation preview
7/25/2019 Arduino Programmer
1/10
Language Reference
Contents1. Structure................................................................................................................ 2
1.1. setup()..............................................................................................................2
1.2. loop()................................................................................................................2
2. Control Structures.................................................................................................. 32.1. If....................................................................................................................... 3
2.2. Ifelse............................................................................................................. 4
2.3. For.................................................................................................................... 5
2.4. Switc case...................................................................................................... !
2.5. "ile................................................................................................................ #
2.!. $owile.........................................................................................................#
2.#. %rea&................................................................................................................ '
2.'. Continue........................................................................................................... '
2.. Return...............................................................................................................'
2.1. *oto...............................................................................................................
Create+ ,- an /u /goc age 1of 10
7/25/2019 Arduino Programmer
2/10
Language Reference
1. STRUCTURE
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 therduino board.
Exampleint button!in " #$
void setup()
%
&erial.begin(')$ pin*ode(button!in, +!UT)$
-
void loop()
%
...-
1.2. loop()
fter 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 rduino board.
Example
const int button!in " #$
setup initializes serial and the button pin
void setup()
%
&erial.begin(')$
pin*ode(button!in, +!UT)$
-
loop checks the button pin each time,
and will send serial if it is pressed
void loop()
%
if (digital/ead(button!in) "" 0+10)
&erial.write(202)$
Create+ ,- an /u /goc age 2of 10
https://www.arduino.cc/en/Reference/Setuphttps://www.arduino.cc/en/Reference/Loophttps://www.arduino.cc/en/Reference/Setuphttps://www.arduino.cc/en/Reference/Loop7/25/2019 Arduino Programmer
3/10
Language Reference
else
&erial.write(232)$
delay(4)$
-
2. CONTROL STRUCTURES
2.1. If
if (conditional) and "", 5", 6, 7 (comparison operators)if, which is used in con8unction 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 is9
if (some:ariable 7 ;)%
do something here
-The program tests to see if some:ariable is greater than ;. +f it is, the program takes a particular
action. !ut another way, if the statement in parentheses is true, the statements inside the brackets
are run. +f not, the program skips over the code.
The brackets may be omitted after anifstatement. +f this is done, the nerite(3?@pin, 0+10)$
if (< 7 4=)
digital>rite(3?@pin, 0+10)$
if (< 7 4=)% digital>rite(3?@pin, 0+10)$ -
if (< 7 4=)%digital>rite(3?@pin4, 0+10)$
digital>rite(3?@pin=, 0+10)$
- all are correct
Comparison Aperators9
< "" y (< is eBual to y)
< 5" y (< is not eBual to y)
< 6 y (< is less than y)< 7 y (< is greater than y)
< 6" y (< is less than or eBual to y)< 7" y (< is greater than or eBual to y)
Warnin!
eware of accidentally using the single eBual sign (e.g. if (< " 4) ). The single eBual sign is theassignment operator, and sets < to 4 (puts the value 4 into the variable
7/25/2019 Arduino Programmer
4/10
Language Reference
eBual to 4 or not. The latter statement is only true if < eBuals 4, but the former statement will
always be true.
This is because C evaluates the statement if (
7/25/2019 Arduino Programmer
5/10
Language Reference
2.#. $or
for statements
%es&ription
The for statement is used to repeat a block of statements enclosed in curly braces. n incrementcounter 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
datapins.There are three parts to the for loop header9
for (initialization$ condition$ increment) %
statement(s)$-
The initialization happens first and e* pinint !>*pin " 4$ 3?@ in series with GH ohm resistor on pin 4
void setup()
% no setup needed
-
void loop()
% for (int i"$ i 6" =;;$ iII)% analog>rite(!>*pin, i)$
delay(4)$
-
-
Co'in Tips
Create+ ,- an /u /goc age 5of 10
7/25/2019 Arduino Programmer
6/10
Language Reference
The C for loop is much more fle
7/25/2019 Arduino Programmer
7/10
Language Reference
,rea&> ?
@*et Co+eA
S-ntaswitc(6ar) < casela,el=
// statements ,rea&> casela,el= // statements ,rea&> +efault=
// statements ,rea&>?@*et Co+eA
araeters6ar= te 6aria,le wose 6alue to copare to te 6arious casesla,el= a 6alue to copare te 6aria,le to
2.+. W*ile
wile loops
$escriptionwile loops will loop continuousl- an+ in7nitel- until te epressioninsi+e te parentesis () ,ecoes false. Soeting ust cangete teste+ 6aria,le or tewile loop will ne6er eit. 8is coul+ ,e in-our co+e suc as an increente+ 6aria,le or an eternal
con+ition suc as testing a sensor.S-ntawile(epression)< 00 stateent(s)?
araetersepression : a (,oolean) C stateent tat e6aluates to true or false;aple6ar B >wile(6ar 2)?
2.,. %o"*ile
8e+o loop wor&s in te sae anner as tewile loop wit te eception tatte con+ition is teste+ at te en+ of te loop so te+o loop willalwaysrun atleast once.+o
Create+ ,- an /u /goc age 7of 10
https://www.arduino.cc/en/Reference/SwitchCase?action=sourceblock&num=1https://www.arduino.cc/en/Reference/SwitchCase?action=sourceblock&num=2https://www.arduino.cc/en/Reference/SwitchCase?action=sourceblock&num=1https://www.arduino.cc/en/Reference/SwitchCase?action=sourceblock&num=27/25/2019 Arduino Programmer
8/10
Language Reference
< 00 stateent ,loc&? wile (test con+ition)>
Example+o< +ela-(5)> 00 wait for sensors to sta,iliEe B rea+Sensors()> 00 cec& te sensors
? wile ( 1)>
2.-. rea/
,rea& is use+ to eit fro a+ofor orwile loop ,-passing te noral loopcon+ition. It is also use+ to eit fro aswitc stateent.Example
for( B > 255> DD)< analog"rite("pin )> sens B analogRea+(sensorin)>
if(sens G tresol+)< // bail out on sensor detect B > ,rea&> ?
+ela-(5)>?
2.0. Continue
8e continue stateent s&ips te rest of te current iteration of a loop (+ofororwile). It continues ,- cec&ing te con+itional epression of te loop an+procee+ing wit an- su,seHuent iterations.Example
for( B > 255> DD)< if( G 4 12)< // create jump in values continue> ?
analog"rite("pin )>
+ela-(5)>?
2.. Return
8erinate a function an+ return a 6alue fro a function to te calling function if+esire+.Syntax:return>return 6alue> 00 ,ot fors are 6ali+
Create+ ,- an /u /goc age 8of 10
7/25/2019 Arduino Programmer
9/10
Language Reference
Parameters6alue= an- 6aria,le or constant t-peExamples:J function to copare a sensor input to a tresol+int cec&Sensor()
else< return > ??
8e return &e-wor+ is an+- to test a section of co+e witout a6ing to 9coentout9 large sections of possi,l- ,ugg- co+e.6oi+ loop()
00 te rest of a +-sfunctional s&etc ere00 tis co+e will ne6er ,e eecute+?
2.1. 3oto
8ransfers progra ow to a la,ele+ point in te prograSyntaxla,el=goto la,el> 00 sen+s progra ow to te la,elTip
8e use ofgotois +iscourage+ in C prograing an+ soe autors of Cprograing ,oo&s clai tat tegotostateent is ne6er necessar- ,ut use+
Ku+iciousl- it can siplif- certain progras. 8e reason tat an- prograers
frown upon te use ofgotois tat wit te unrestraine+ use ofgotostateentsit is eas- to create a progra wit un+e7ne+ progra ow wic can ne6er ,e+e,ugge+."it tat sai+ tere are instances were a goto stateent can coe in an+- an+
siplif- co+ing. ne of tese situations is to ,rea& out of +eepl- neste+forloopsoriflogic ,loc&s on a certain con+ition.Examplefor(,-te r B > r 255> rDD)< for(,-te g B 255> g G :1> g::)< for(,-te , B > , 255> ,DD)< if (analogRea+() G 25)< goto ,ailout>? 00 ore stateents ...
? ??,ailout=
Create+ ,- an /u /goc age 9of 10
7/25/2019 Arduino Programmer
10/10
Language Reference
Create+ ,- an /u /goc age 10of 10