arduino processing introducere
TRANSCRIPT
-
8/9/2019 Arduino Processing Introducere
1/27
Arduino
Guide to the Arduino Leonardo and Micro
To connect the Arduino Leonardo or Micro to your computer, you'll need a Micro-B USB
cable. This USB cable provides power and data to the board. hen pro!rammin! the
Leonardo, you must choose Arduino Leonardo" or "Arduino Micro"rom the Tools >
Boardmenu in the Arduino #$%.
Diferences rom the Arduino Uno
o Single processor or sketches and USB communication.
o Serial re-enumeration on reset.
o No reset when you open the serial port.
o Keyoard and mouse emulation.
o Separation o USB and serial communication.
o Diferences in pin capailities.
!nstalling Dri"ers or the #eonardo and $icro
o %S&
!nstructions or 'indows
o #inu(
Uploading )ode to the #eonardo and $icro
*ood )oding +ractice 'ith the #eonardo and $icro
Diferences rom the Arduino Uno
#n !eneral, you pro!ram and use the Leonardo and Micro as you would other Arduino boards.There are, however, a "ew important di""erences.
Single processor for sketches and USB communication.
The Leonardo and Micro di""er "rom other Arduino boards in that they use a sin!le
microcontroller to both run your s&etches and "or USB communication with the computer.
The Uno and other boards use separate microcontrollers "or these two "unctions, meanin! that
the USB connection to the computer remains established re!ardless o" the state o" the main
microcontroller. By combinin! these two "unctions onto a sin!le processor, the Leonardo
allows "or more "leibility in its communication with the computer. #t also helps to lower the
cost o" the board by removin! the need "or an additional processor.
http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc1http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc2http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc3http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc4http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc5http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc6http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc7http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc8http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc9http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc10http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc11http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc12http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc13http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc2http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc3http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc4http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc5http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc6http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc7http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc8http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc9http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc10http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc11http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc11http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc12http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc13http://arduino.cc/en/Guide/ArduinoLeonardoMicro?from=Guide.ArduinoLeonardo#toc1 -
8/9/2019 Arduino Processing Introducere
2/27
Serial re-enumeration on reset.
Since the boards do not have a dedicated chip to handle serial communication, it means that
the serial port is virtual-- it's a so"tware routine, both on your operatin! system, and on the
board itsel". (ust as your computer creates an instance o" the serial port driver when you plu!
in any Arduino, the Leonardo)Micro creates a serial instance whenever it runs its bootloader.
The board is an instance o" USB's Connected Device Class (CDC)driver.
This means that every time you reset the board, the USB serial connection will be bro&en and
re-established. The board will disappear "rom the list o" serial ports, and the list will re-
enumerate. Any pro!ram that has an open serial connection to the Leonardo will lose its
connection. This is in contrast to the Arduino Uno, with which you can reset the main
processor *the ATme!a+/ without closin! the USB connection *which is maintained by
the secondary ATme!aU or ATme!a01U processor/. This di""erence has implications "or
driver installation, uploadin!, and communication2 these are discussed below.
No reset when you open the serial port.
Unli&e the Arduino Uno, the Leonardo and Micro won't restart your s&etch when you open a
serial port on the computer. That means you won't see serial data that's already been sent to
the computer by the board, includin!, "or eample, most data sent in the setup()"unction.
This chan!e means that i" you're usin! any Serial print*/, println*/ or write*/ statments in your
setup, they won't show up when you open the serial monitor. To wor& around this, you can
chec& to see i" the serial port is open a"ter callin! Serial.begin()li&e so3
Serial.egin,//01
22 while the serial stream is not open3 do nothing4
while ,5Serial0 1
6*et )ode7
Keyboard and mouse emulation.
4ne advanta!e o" usin! a sin!le chip "or your s&etches and "or USB is increased "leibility in
the communication with the computer. hile the board appears as a virtual serial port to your
operatin! system *also called 5$5/ "or pro!rammin! and communication *as with the
Arduino Uno/, it can also behave as a *6#$/ &eyboard or mouse. See the 78ood 5odin!ractice7 section below "or a warnin! about usin! this "unctionality.
Separation of USB and serial communication.
4n the Leonardo and Micro, the main Serialclass re"ers to the virtual serial driver on the
board "or connection to your computer over USB. #t's not connected to the physical pins 9
and 0 as it is on the Uno and earlier boards. To use the hardware serial port *pins 9 and 0, :;
and T;/, use Serial1. *See the Serial re"erence pa!es"or more in"ormation./
Dierences in pin capabilities.
The Leonardo has some sli!ht di""erences in the capabilities and assi!nments o" various pins*especially "or S# and T#/. These are detailed on the hardware pa!e.
http://arduino.cc/en/Guide/ArduinoLeonardoMicro?action=sourceblock&num=1http://arduino.cc/en/Reference/Serialhttp://arduino.cc/en/Main/ArduinoBoardLeonardohttp://arduino.cc/en/Guide/ArduinoLeonardoMicro?action=sourceblock&num=1http://arduino.cc/en/Reference/Serialhttp://arduino.cc/en/Main/ArduinoBoardLeonardo -
8/9/2019 Arduino Processing Introducere
3/27
!nstalling Dri"ers or the #eonardo and $icro
S!
8he 9rst time you plug a #eonardo or $icro into a $ac3 the :Keyoard
Setup Assistant: will launch. 8here;s nothing to con9gure with the#eonardo3 so you can close this dialogue y clicking the red utton in thetop let o the window.
!nstructions or 'indows
The "ollowin! instructions are "or indows
-
8/9/2019 Arduino Processing Introducere
4/27
At the ne(t screen3 choose :Browse my computer or dri"er sotware:3 andclick Next.
-
8/9/2019 Arduino Processing Introducere
5/27
)lick the Browse...utton. Another dialog appears4 na"igate to the olderwith the Arduino sotware that you ?ust downloaded. Select the driversolder an click OK3 then click Next.
-
8/9/2019 Arduino Processing Introducere
6/27
@ou will recei"e a noti9cation that the oard has not passed 'indows #ogotesting. )lick on the utton Continue Anyway.
-
8/9/2019 Arduino Processing Introducere
7/27
Ater a ew moments3 a window will tell you the wiard has 9nishedinstalling sotware or Arduino #eonardo. +ress the Closeutton.
"inu#
There is no need to install drivers "or Ubuntu 09.9.=
Uploading )ode to the #eonardo and $icro
#n !eneral, you upload code to the Leonardo or Micro as you would with the Uno or other
Arduino boards. 5lic& the upload button in the Arduino #$% and your s&etch will be
automatically uploaded onto the board and then started. This wor&s more or less the sameway as with the Uno3 the Arduino so"tware initiates a reset o" the board, launchin! the
bootloader - which is responsible "or receivin!, storin!, and startin! the new s&etch.
6owever, because the serial port is virtual, it disappears when the board resets, the Arduino
so"tware uses a di""erent strate!y "or timin! the upload than with the Uno and other boards.
#n particular, a"ter initiatin! the auto-reset o" the Leonardo or Micro *usin! the serial port
selected in the Tools > Serial Portmenu/, the Arduino so"tware waits "or a new virtual
*5$5/ serial ) 54M port to appear - one that it assumes represents the bootloader. #t then
per"orms the upload on this newly-appeared port.
-
8/9/2019 Arduino Processing Introducere
8/27
These di""erences a""ect the way you use the physical reset button to per"orm an upload i" the
auto-reset isn't wor&in!. ress and hold the reset button on the Leonardo or Micro, thenhit
the upload button in the Arduino so"tware. 4nly release the reset button a"ter you see the
messa!e 7Uploadin!...7 appear in the so"tware's status bar. hen you do so, the bootloader
will start, creatin! a new virtual *5$5/ serial port on the computer. The so"tware will see thatport appear and per"orm the upload usin! it. A!ain, this is only necessary i" the normal
upload process *i.e. >ust pressin! the uploadin! button/ doesn't wor&. *?ote that the auto-reset
is initiated when the computer opens the serial port at 099 baud and then closes it2 this won't
wor& i" somethin! inter"eres with the board's USB communication - e.!. disablin! interrupts./
*ood )oding +ractice 'ith the #eonardo and $icro
A word of caution on using te !SB Mouse and e#$oard Li$raries 3 i" the Mouse or
@eyboard library is constantly runnin!, it will be di""icult to pro!ram your board. unctions
such as Mouse.move*/ and @eyboard.print*/ will move your cursor or send &eystro&es to aconnected computer and should only be called when you are ready to handle them. #t is
recommended to use a control system to turn this "unctionality on, li&e a physical switch or
only respondin! to speci"ic input you can control. hen usin! the Mouse or @eyboard
library, it may be best to test your output "irst usin! Serial.print*/. This way, you can be sure
you &now what values are bein! reported. :e"er to the Mouse and @eyboard eamples "or
some ways to handle this.
!sing te serial %onitor effectivel#3 Since serial is !oin! throu!h only one processor, the
board is capable o" "illin! your computer's serial bu""er "aster than the Uno or earlier boards.
ou may notice that i" you send serial continually, "or eample li&e this3
"oid loop,0
int sensor>eading C analog>ead,A/01
Serial.println,sensor>eading01
6*et )ode7
the Serial Monitor in the #$% slows down considerably as it tries to &eep up. #" you encounter
this, add a short delay to your loop so that the computer's serial bu""er is not "illed as "ast.%ven a millisecond delay will help3
"oid loop,0
int sensor>eading C analog>ead,A/01
Serial.println,sensor>eading01
delay,E01
6*et )ode7
http://arduino.cc/en/Guide/ArduinoLeonardoMicro?action=sourceblock&num=2http://arduino.cc/en/Guide/ArduinoLeonardoMicro?action=sourceblock&num=3http://arduino.cc/en/Guide/ArduinoLeonardoMicro?action=sourceblock&num=2http://arduino.cc/en/Guide/ArduinoLeonardoMicro?action=sourceblock&num=3 -
8/9/2019 Arduino Processing Introducere
9/27
Serial applications usin! native libraries other than :;T; library read the serial bu""er "aster,
so you may not encounter this error much outside o" the Serial Monitor, rocessin!, or other
:;T;-based serial applications
or more details on the Arduino Leonardo and Micro, see the Leonardo hardware pa!eand
Micro hardware pa!e. or additional in"ormation on the USB capabilities, see theMouse and
@eyboard re"erence pa!es.
Language e!erence
Arduino pro!rams can be divided in three main parts3 structure, values*variables and
constants/, and functions.
Structure
setup,0
loop,0
$ontrol Structures
i
i...else
or
switch case
while
do... while
reak
continue
return
goto
%urther Synta#
1,semicolon0
,curly races0
22,single linecomment0
2F F2,multi-line
Gariales
$onstants
=!*=H #%'
!N+U8H %U8+U8H!N+U8I+U##U+
#JDIBU!#8!N
trueH alse
integer constants
oating pointconstants
Data &ypes
"oid
oolean
char
unsigned char
yte
int
unsigned int
word
long
Lunctions
Digital '(
pin$ode,0
digital'rite,0
digital>ead,0
)nalog '(
analog>eerence,0
analog>ead,0
analog'rite,0 -PWM
Due only
analog>ead>esolution,0
analog'rite>esolution,0
)d*anced '(
tone,0
no8one,0
shit%ut,0
shit!n,0
pulse!n,0
&ime
http://arduino.cc/en/Main/ArduinoBoardLeonardohttp://arduino.cc/en/Main/ArduinoBoardMicrohttp://arduino.cc/en/Reference/MouseKeyboardhttp://arduino.cc/en/Reference/MouseKeyboardhttp://arduino.cc/en/Reference/MouseKeyboardhttp://arduino.cc/en/Reference/MouseKeyboardhttp://arduino.cc/en/Reference/Setuphttp://arduino.cc/en/Reference/Loophttp://arduino.cc/en/Reference/Ifhttp://arduino.cc/en/Reference/Elsehttp://arduino.cc/en/Reference/Forhttp://arduino.cc/en/Reference/SwitchCasehttp://arduino.cc/en/Reference/Whilehttp://arduino.cc/en/Reference/DoWhilehttp://arduino.cc/en/Reference/Breakhttp://arduino.cc/en/Reference/Continuehttp://arduino.cc/en/Reference/Returnhttp://arduino.cc/en/Reference/Gotohttp://arduino.cc/en/Reference/SemiColonhttp://arduino.cc/en/Reference/Braceshttp://arduino.cc/en/Reference/Commentshttp://arduino.cc/en/Reference/Commentshttp://arduino.cc/en/Reference/Constantshttp://arduino.cc/en/Reference/Constantshttp://arduino.cc/en/Reference/Constantshttp://arduino.cc/en/Reference/Constantshttp://arduino.cc/en/Reference/Constantshttp://arduino.cc/en/Reference/Constantshttp://arduino.cc/en/Reference/Constantshttp://arduino.cc/en/Reference/Constantshttp://arduino.cc/en/Reference/IntegerConstantshttp://arduino.cc/en/Reference/Fpconstantshttp://arduino.cc/en/Reference/Fpconstantshttp://arduino.cc/en/Reference/Voidhttp://arduino.cc/en/Reference/BooleanVariableshttp://arduino.cc/en/Reference/Charhttp://arduino.cc/en/Reference/UnsignedCharhttp://arduino.cc/en/Reference/Bytehttp://arduino.cc/en/Reference/Inthttp://arduino.cc/en/Reference/UnsignedInthttp://arduino.cc/en/Reference/Wordhttp://arduino.cc/en/Reference/Longhttp://arduino.cc/en/Reference/PinModehttp://arduino.cc/en/Reference/DigitalWritehttp://arduino.cc/en/Reference/DigitalReadhttp://arduino.cc/en/Reference/AnalogReferencehttp://arduino.cc/en/Reference/AnalogReadhttp://arduino.cc/en/Reference/AnalogWritehttp://arduino.cc/en/Reference/AnalogReadResolutionhttp://arduino.cc/en/Reference/AnalogReadResolutionhttp://arduino.cc/en/Reference/AnalogWriteResolutionhttp://arduino.cc/en/Reference/AnalogWriteResolutionhttp://arduino.cc/en/Reference/Tonehttp://arduino.cc/en/Reference/NoTonehttp://arduino.cc/en/Reference/ShiftOuthttp://arduino.cc/en/Reference/ShiftInhttp://arduino.cc/en/Reference/PulseInhttp://arduino.cc/en/Main/ArduinoBoardLeonardohttp://arduino.cc/en/Main/ArduinoBoardMicrohttp://arduino.cc/en/Reference/MouseKeyboardhttp://arduino.cc/en/Reference/MouseKeyboardhttp://arduino.cc/en/Reference/Setuphttp://arduino.cc/en/Reference/Loophttp://arduino.cc/en/Reference/Ifhttp://arduino.cc/en/Reference/Elsehttp://arduino.cc/en/Reference/Forhttp://arduino.cc/en/Reference/SwitchCasehttp://arduino.cc/en/Reference/Whilehttp://arduino.cc/en/Reference/DoWhilehttp://arduino.cc/en/Reference/Breakhttp://arduino.cc/en/Reference/Continuehttp://arduino.cc/en/Reference/Returnhttp://arduino.cc/en/Reference/Gotohttp://arduino.cc/en/Reference/SemiColonhttp://arduino.cc/en/Reference/Braceshttp://arduino.cc/en/Reference/Commentshttp://arduino.cc/en/Reference/Commentshttp://arduino.cc/en/Reference/Constantshttp://arduino.cc/en/Reference/Constantshttp://arduino.cc/en/Reference/Constantshttp://arduino.cc/en/Reference/Constantshttp://arduino.cc/en/Reference/Constantshttp://arduino.cc/en/Reference/Constantshttp://arduino.cc/en/Reference/Constantshttp://arduino.cc/en/Reference/Constantshttp://arduino.cc/en/Reference/IntegerConstantshttp://arduino.cc/en/Reference/Fpconstantshttp://arduino.cc/en/Reference/Fpconstantshttp://arduino.cc/en/Reference/Voidhttp://arduino.cc/en/Reference/BooleanVariableshttp://arduino.cc/en/Reference/Charhttp://arduino.cc/en/Reference/UnsignedCharhttp://arduino.cc/en/Reference/Bytehttp://arduino.cc/en/Reference/Inthttp://arduino.cc/en/Reference/UnsignedInthttp://arduino.cc/en/Reference/Wordhttp://arduino.cc/en/Reference/Longhttp://arduino.cc/en/Reference/PinModehttp://arduino.cc/en/Reference/DigitalWritehttp://arduino.cc/en/Reference/DigitalReadhttp://arduino.cc/en/Reference/AnalogReferencehttp://arduino.cc/en/Reference/AnalogReadhttp://arduino.cc/en/Reference/AnalogWritehttp://arduino.cc/en/Reference/AnalogReadResolutionhttp://arduino.cc/en/Reference/AnalogReadResolutionhttp://arduino.cc/en/Reference/AnalogWriteResolutionhttp://arduino.cc/en/Reference/AnalogWriteResolutionhttp://arduino.cc/en/Reference/Tonehttp://arduino.cc/en/Reference/NoTonehttp://arduino.cc/en/Reference/ShiftOuthttp://arduino.cc/en/Reference/ShiftInhttp://arduino.cc/en/Reference/PulseIn -
8/9/2019 Arduino Processing Introducere
10/27
comment0
Mde9ne
Minclude
)rithmetic perators
C,assignmentoperator0
,addition0
-,sutraction0
F,multiplication0
2,di"ision0
O,modulo0
$omparison perators
CC,ePual to0
5C,not ePual to0
Q,less than0
ead,0
it'rite,0
itSet,0
it)lear,0
http://arduino.cc/en/Reference/Definehttp://arduino.cc/en/Reference/Includehttp://arduino.cc/en/Reference/Assignmenthttp://arduino.cc/en/Reference/Arithmetichttp://arduino.cc/en/Reference/Arithmetichttp://arduino.cc/en/Reference/Arithmetichttp://arduino.cc/en/Reference/Arithmetichttp://arduino.cc/en/Reference/Modulohttp://arduino.cc/en/Reference/Ifhttp://arduino.cc/en/Reference/Ifhttp://arduino.cc/en/Reference/Ifhttp://arduino.cc/en/Reference/Ifhttp://arduino.cc/en/Reference/Ifhttp://arduino.cc/en/Reference/Ifhttp://arduino.cc/en/Reference/Booleanhttp://arduino.cc/en/Reference/Booleanhttp://arduino.cc/en/Reference/Booleanhttp://arduino.cc/en/Reference/Pointerhttp://arduino.cc/en/Reference/Pointerhttp://arduino.cc/en/Reference/Pointerhttp://arduino.cc/en/Reference/Pointerhttp://arduino.cc/en/Reference/UnsignedLonghttp://arduino.cc/en/Reference/Shorthttp://arduino.cc/en/Reference/Floathttp://arduino.cc/en/Reference/Doublehttp://arduino.cc/en/Reference/Stringhttp://arduino.cc/en/Reference/StringObjecthttp://arduino.cc/en/Reference/Arrayhttp://arduino.cc/en/Reference/CharCasthttp://arduino.cc/en/Reference/ByteCasthttp://arduino.cc/en/Reference/IntCasthttp://arduino.cc/en/Reference/WordCasthttp://arduino.cc/en/Reference/LongCasthttp://arduino.cc/en/Reference/FloatCasthttp://arduino.cc/en/Reference/Scopehttp://arduino.cc/en/Reference/Statichttp://arduino.cc/en/Reference/Volatilehttp://arduino.cc/en/Reference/Consthttp://arduino.cc/en/Reference/Sizeofhttp://arduino.cc/en/Reference/Millishttp://arduino.cc/en/Reference/Microshttp://arduino.cc/en/Reference/Delayhttp://arduino.cc/en/Reference/DelayMicrosecondshttp://arduino.cc/en/Reference/Minhttp://arduino.cc/en/Reference/Maxhttp://arduino.cc/en/Reference/Abshttp://arduino.cc/en/Reference/Constrainhttp://arduino.cc/en/Reference/Maphttp://arduino.cc/en/Reference/Powhttp://arduino.cc/en/Reference/Sqrthttp://arduino.cc/en/Reference/Sinhttp://arduino.cc/en/Reference/Coshttp://arduino.cc/en/Reference/Tanhttp://arduino.cc/en/Reference/RandomSeedhttp://arduino.cc/en/Reference/Randomhttp://arduino.cc/en/Reference/LowBytehttp://arduino.cc/en/Reference/HighBytehttp://arduino.cc/en/Reference/BitReadhttp://arduino.cc/en/Reference/BitWritehttp://arduino.cc/en/Reference/BitSethttp://arduino.cc/en/Reference/BitClearhttp://arduino.cc/en/Reference/Definehttp://arduino.cc/en/Reference/Includehttp://arduino.cc/en/Reference/Assignmenthttp://arduino.cc/en/Reference/Arithmetichttp://arduino.cc/en/Reference/Arithmetichttp://arduino.cc/en/Reference/Arithmetichttp://arduino.cc/en/Reference/Arithmetichttp://arduino.cc/en/Reference/Modulohttp://arduino.cc/en/Reference/Ifhttp://arduino.cc/en/Reference/Ifhttp://arduino.cc/en/Reference/Ifhttp://arduino.cc/en/Reference/Ifhttp://arduino.cc/en/Reference/Ifhttp://arduino.cc/en/Reference/Ifhttp://arduino.cc/en/Reference/Booleanhttp://arduino.cc/en/Reference/Booleanhttp://arduino.cc/en/Reference/Booleanhttp://arduino.cc/en/Reference/Pointerhttp://arduino.cc/en/Reference/Pointerhttp://arduino.cc/en/Reference/Pointerhttp://arduino.cc/en/Reference/Pointerhttp://arduino.cc/en/Reference/UnsignedLonghttp://arduino.cc/en/Reference/Shorthttp://arduino.cc/en/Reference/Floathttp://arduino.cc/en/Reference/Doublehttp://arduino.cc/en/Reference/Stringhttp://arduino.cc/en/Reference/StringObjecthttp://arduino.cc/en/Reference/Arrayhttp://arduino.cc/en/Reference/CharCasthttp://arduino.cc/en/Reference/ByteCasthttp://arduino.cc/en/Reference/IntCasthttp://arduino.cc/en/Reference/WordCasthttp://arduino.cc/en/Reference/LongCasthttp://arduino.cc/en/Reference/FloatCasthttp://arduino.cc/en/Reference/Scopehttp://arduino.cc/en/Reference/Statichttp://arduino.cc/en/Reference/Volatilehttp://arduino.cc/en/Reference/Consthttp://arduino.cc/en/Reference/Sizeofhttp://arduino.cc/en/Reference/Millishttp://arduino.cc/en/Reference/Microshttp://arduino.cc/en/Reference/Delayhttp://arduino.cc/en/Reference/DelayMicrosecondshttp://arduino.cc/en/Reference/DelayMicrosecondshttp://arduino.cc/en/Reference/Minhttp://arduino.cc/en/Reference/Maxhttp://arduino.cc/en/Reference/Abshttp://arduino.cc/en/Reference/Constrainhttp://arduino.cc/en/Reference/Maphttp://arduino.cc/en/Reference/Powhttp://arduino.cc/en/Reference/Sqrthttp://arduino.cc/en/Reference/Sinhttp://arduino.cc/en/Reference/Coshttp://arduino.cc/en/Reference/Tanhttp://arduino.cc/en/Reference/RandomSeedhttp://arduino.cc/en/Reference/Randomhttp://arduino.cc/en/Reference/LowBytehttp://arduino.cc/en/Reference/HighBytehttp://arduino.cc/en/Reference/BitReadhttp://arduino.cc/en/Reference/BitWritehttp://arduino.cc/en/Reference/BitSethttp://arduino.cc/en/Reference/BitClear -
8/9/2019 Arduino Processing Introducere
11/27
Bitwise perators
R,itwise and0
H,itwise or0
,itwise (or0
T,itwise not0
QQ,itshit let0
-
8/9/2019 Arduino Processing Introducere
12/27
myservo.attach(9);}
void loop(){
for(pos = 0; pos ! 0; pos = )
{myservo.$rite(pos);delay();
}for(pos = 0; pos%=0; pos&=){myservo.$rite(pos);delay();
}}
-
8/9/2019 Arduino Processing Introducere
13/27
+rocessing
Getting "tarted. $elco#e to %rocessing&This tutorial is for Processing 2+. If you see any errors or have comments, please let us know.Thistutorial was adapted from the book, Getting tarted with Processing,by !asey "eas and #en $ry,%&"eilly ' (ake 2)*).
Start by visitinghttp://processing.org/downloadand selecting the Mac, Windows, or Linux version,depending on what machine you have. Installation on each machine is straightorward:
!n Windows, you"ll have a .#ip ile. $ouble%clic& it, and drag the older inside to a location on
your hard dis&. It could be 'rogram (iles or simply the des&top, but the important thing is orthe processing older to be pulled out o that .#ip ile. )hen double%clic& processing.exe tostart.
)he Mac !S * version is also a .#ip ile. $ouble%clic& it and drag the 'rocessing icon to the+pplications older. I you"re using someone else"s machine and can"t modiy the +pplicationsolder, ust drag the application to the des&top. )hen double%clic& the 'rocessing icon to start.
)he Linux version is a .tar.g# ile, which should be amiliar to most Linux users. $ownload theile to your home directory, then open a terminal window, and type:tar xvfz processing-xxxx.tgz
-eplace xxxx with the rest o the ile"s name, which is the version number. )his will create aolder named processing%0.1 or something similar. )hen change to that directory:cd processing-xxxx
and run it:./processing
With any luc&, the main 'rocessing window will now be visible. 2veryone"s setup is dierent, so i theprogram didn"t start, or you"re otherwise stuc&, visit thetroubleshooting pageor possible solutions.
https://github.com/processing/processing-web/issues?state=openhttps://github.com/processing/processing-web/issues?state=openhttps://github.com/processing/processing-web/issues?state=openhttp://www.processing.org/learning/books/#reasfry2http://www.processing.org/learning/books/#reasfry2http://processing.org/downloadhttp://processing.org/downloadhttp://wiki.processing.org/w/Troubleshootinghttp://wiki.processing.org/w/Troubleshootinghttps://github.com/processing/processing-web/issues?state=openhttp://www.processing.org/learning/books/#reasfry2http://processing.org/downloadhttp://wiki.processing.org/w/Troubleshooting -
8/9/2019 Arduino Processing Introducere
14/27
The Processing evelopment nvironment.
'our (irst %rogra#3ou"re now running the 'rocessing $evelopment 2nvironment -or '$2. )here"s not much to it4 thelarge area is the )ext 2ditor, and there"s a row o buttons across the top4 this is the toolbar. 5elow the
editor is the Message +rea, and below that is the 6onsole. )he Message +rea is used or one linemessages, and the 6onsole is used or more technical details.
In the editor, type the ollowing:
ellipse(50, 50, 80, 80);
)his line o code means 7draw an ellipse, with the center 81 pixels over rom the let and 81 pixelsdown rom the top, with a width and height o 91 pixels.7 6lic& the un button, which loo&s li&e this:
I you"ve typed everything correctly, you"ll see this appear in the $isplay Window:
I you didn"t type it correctly, the Message +rea will turn red and complain about an error. I thishappens, ma&e sure that you"ve copied the example code exactly: the numbers should be contained
-
8/9/2019 Arduino Processing Introducere
15/27
within parentheses and have commas between each o them, and the line should end with asemicolon.
!ne o the most diicult things about getting started with programming is that you have to be veryspeciic about the syntax. )he 'rocessing sotware isn"t always smart enough to &now what you mean,and can be uite ussy about the placement o punctuation. 3ou"ll get used to it with a little practice.
;ext, we"ll s&ip ahead to a s&etch that"s a little more exciting. $elete the text rom the last example,and try this:
voidsetup()
size(!80, "#0);
$
voiddraw()
if(%ouse&ressed)
fill(0);
$ else
fill(#55);
$
ellipse(%ouse', %ouse, 80, 80);
$
)his program creates a window that is
-
8/9/2019 Arduino Processing Introducere
16/27
"howSo ar we"ve covered only the un button, though you"ve probably guessed what the Stop button nextto it does:
I you don"t want to use the buttons, you can always use the S&etch menu, which reveals the shortcut6trl% -or 6md% on the Mac or un. 5elow un in the S&etch menu is 'resent, which clears the resto the screen to present your s&etch all by itsel:
3ou can also use 'resent rom the toolbar by holding down the Shit &ey as you clic& the un button.
"ave)he next command that"s important is Save. It"s the downward arrow on the toolbar:
3ou can also ind it under the (ile menu. 5y deault, your programs are saved to the 7s&etchboo&,7which is a older that collects your programs or easy access. 6lic&ing the !pen button on the toolbar-the arrow pointing up will bring up a list o all the s&etches in your s&etchboo&, as well as a list oexamples that are installed with the 'rocessing sotware:
It"s always a good idea to save your s&etches oten. +s you try dierent things, &eep saving withdierent names, so that you can always go bac& to an earlier version. )his is especially helpul i > no,when > something brea&s. 3ou can also see where the s&etch is located on the dis& with Show S&etch(older under the S&etch menu.
3ou can also create a new s&etch by pressing the ;ew button on the toolbar:
-
8/9/2019 Arduino Processing Introducere
17/27
"hare+nother theme o 'rocessing is sharing your wor&. )he 2xport +pplication button on the toolbar:
will bundle your code into an application or your choice o Mac, Windows, or Linux depending onwhich platorm you are using. )his is an easy way to ma&e sel%contained, double%clic&able versions oyour proects.
3ou can also ind 2xport +pplication under the (ile menu.
In addition to exporting your code as applications, you can switch to a dierent modewithin'rocessing to export to other platorms. (or example, download and change to ?avaScript Modetoexport @)ML8 6anvas and WebAL. 6hange to+ndroidmode to export application or +ndroidphones and tablets. )hese modes both need to be added beore they can be used. Select 7+dd Mode...7
rom the menu that says 7?ava7 in the upper%right corner o the 'rocessing $evelopment2nvironment.
)xa#ples and e!erenceLearning how to program with 'rocessing involves exploring lots o code: running, altering, brea&ing,and enhancing it until you have reshaped it into something new. With this in mind, the 'rocessingsotware download includes do#ens o examples that demonstrate dierent eatures o the sotware.)o open an example, select 2xamples rom the (ile menu or clic& the !pen icon in the '$2. )heexamples are grouped into categories based on their unction, such as (orm, Motion, and Image. (indan interesting topic in the list and try an example.
I you see a part o the program you"re unamiliar with that is colored orange -this means it"s a part o
'rocessing, select its name, and then clic& on 7(ind in eerence7 rom the @elp menu. 3ou can alsoright%clic& the text -or 6trl%clic& on a Mac and choose (ind in eerence rom the menu that appears.)his will open the reerence or the selected code element in your web browser. )he reerence is alsoavailable online.
)he 'rocessing eerence explains every code element with a description and examples. )he reerenceprograms are much shorter -usually our or ive lines and easier to ollow than the longer code oundin the 2xamples older. We recommend &eeping the reerence open while you"re reading this boo& andwhile you"re programming. It can be navigated by topic or alphabetically4 sometimes it"s astest to do atext search within your browser window.
)he reerence was written with the beginner in mind4 we hope that we"ve made it clear and
understandable. We"re grateul to the many people who"ve spotted errors over the years and reportedthem. I you thin& you can improve a reerence entry or you ind a mista&e, please let us &now byclic&ing on the lin& at the top o each reerence page.
Coordinate "yste# and "hapesThis tutorial is for Processing version 1.1+. If you see any errors or have comments,
pleaselet us know. This tutorial is from the book,earning Processing, by !aniel"hi#man, publishe$ by Morgan %aufmann Publishers, &opyright '(() *lsevier Inc. ll
rights reserve$.
http://processing.org/learning/android/http://processing.org/learning/android/http://www.processing.org/reference/http://www.processing.org/reference/http://www.processing.org/reference/https://github.com/processing/processing-web/issues?state=openhttp://www.processing.org/learning/books/#shiffmanhttp://processing.org/learning/android/http://www.processing.org/reference/http://www.processing.org/reference/https://github.com/processing/processing-web/issues?state=openhttp://www.processing.org/learning/books/#shiffman -
8/9/2019 Arduino Processing Introducere
18/27
Coordinate "paceBeore we egin programming with +rocessing3 we must 9rst channel our eighth grade
sel"es3 pull out a piece o graph paper3 and draw a line. 8he shortest distance etween
two points is a good old ashioned line3 and this is where we egin3 with two points on
that graph paper.
8he ao"e 9gure shows a line etween point A ,E3/0 and point B ,3V0. ! you wanted to
direct a riend o yours to draw that same line3 you would gi"e them a shout and say
:draw a line rom the point one-ero to the point our-9"e3 please.: 'ell3 or the moment3
imagine your riend was a computer and you wanted to instruct this digital pal to displaythat same line on its screen. 8he same command applies ,only this time you can skip the
pleasantries and you will e rePuired to employ a precise ormatting0. =ere3 the
instruction will look like this4
line(",0,!,5);
J"en without ha"ing studied the synta( o writing code3 the ao"e statement should
make a air amount o sense. 'e are pro"iding acomman$,which we will reer to as a:unction:0 or the machine to ollow entitled :line.: !n addition3 we are speciying some
arguments or how that line should e drawn3 rom point A ,E3/0 to point B ,3V0. ! youthink o that line o code as a sentence3 the unction is a "er and the arguments are theo?ects o the sentence. 8he code sentence also ends with a semicolon instead o a
period.
8he key here is to realie that the computer screen is nothing more than a ancier piece
o graph paper. Jach pi(el o the screen is a coordinate - two numers3 an :(: ,horiontal0and a :y: ,"ertical0 - that determines the location o a point in space. And it is our ?o to
-
8/9/2019 Arduino Processing Introducere
19/27
speciy what shapes and colors should appear at these pi(el coordinates.
Ne"ertheless3 there is a catch here. 8he graph paper rom eighth grade ,:)artesiancoordinate system:0 placed ,/3/0 in the center with the y-a(is pointing up and the (-a(ispointing to the right ,in the positi"e direction3 negati"e down and to the let0. 8hecoordinate system or pi(els in a computer window3 howe"er3 is re"ersed along the y-
a(is. ,/3/0 can e ound at the top let with the positi"e direction to the right horiontallyand down "ertically.
"i#ple "hapes8he "ast ma?ority o the programming e(amples you;ll see with +rocessing are "isual in
nature. 8hese e(amples3 at their core3 in"ol"e drawing shapes and setting pi(els. #et;s
egin y looking at our primiti"e shapes.
Lor each shape3 we will ask oursel"es what inormation is rePuired to speciy the location
and sie ,and later color0 o that shape and learn how +rocessing e(pects to recei"e that
inormation. !n each o the diagrams elow3 we;ll assume a window with a width o E/
pi(els and height o E/ pi(els. 8his isn;t particularly realistic since when you really start
coding you will most likely work with much larger windows ,E/(E/ pi(els is arely a ew
millimeters o screen space.0 Ne"ertheless or demonstration purposes3 it is nice to work
with smaller numers in order to present the pi(els as they might appear on graph paper
,or now0 to etter illustrate the inner workings o each line o code.
-
8/9/2019 Arduino Processing Introducere
20/27
Apoint*+is the easiest o the shapes and a good place to start. 8o draw a point3 we
only need an ( and y coordinate.
Aline*+isn;t terrily diWcult either and simply rePuires two points4 ,(E3yE0 and ,(X3yX04
%nce we arri"e at drawing arect*+3 things ecome a it more complicated. !n+rocessing3 a rectangle is speci9ed y the coordinate or the top let corner o the
rectangle3 as well as its width and height.
http://www.processing.org/reference/point_.htmlhttp://www.processing.org/reference/line_.htmlhttp://www.processing.org/reference/rect_.htmlhttp://www.processing.org/reference/rect_.htmlhttp://www.processing.org/reference/point_.htmlhttp://www.processing.org/reference/line_.htmlhttp://www.processing.org/reference/rect_.html -
8/9/2019 Arduino Processing Introducere
21/27
A second way to draw a rectangle in"ol"es speciying the centerpoint3 along with width
and height. ! we preer this method3 we 9rst indicate that we want to use the :)JN8J>:
mode eore the instruction or the rectangle itsel. Note that +rocessing is case-
sensiti"e.
Linally3 we can also draw a rectangle with two points ,the top let corner and the ottom
right corner0. 8he mode here is :)%>NJ>S:.
-
8/9/2019 Arduino Processing Introducere
22/27
%nce we ha"e ecome comortale with the concept o drawing a rectangle3
anellipse*+is a snap. !n act3 it is identical torect*+with the diference eing that anellipse is drawn where the ounding o( o the rectangle would e. 8he deault mode
orellipse-is :)JN8J>:3 rather than :)%>NJ>.:
http://www.processing.org/reference/ellipse_.htmlhttp://www.processing.org/reference/ellipse_.html -
8/9/2019 Arduino Processing Introducere
23/27
!t is important to acknowledge that these ellipses do not look particularly circular.
+rocessing has a uilt-in methodology or selecting which pi(els should e used to create
a circular shape. Yoomed in like this3 we get a unch o sPuares in a circle-like pattern3
ut oomed out on a computer screen3 we get a nice round ellipse. +rocessing also gi"es
us the power to de"elop our own algorithms or coloring in indi"idual pi(els ,in act3 we
can already imagine how we might do this using :point: o"er and o"er again03 ut or
now3 we are content with allowing the :ellipse: statement to do the hard work. ,Lor more
aout pi(els3 start with4the pi(els reerence page3 though e warned this is a great deal
more ad"anced than this tutorial.0
Now let;s look at what some code with shapes in more realistic setting3 with window
http://processing.org/reference/pixels.htmlhttp://processing.org/reference/pixels.html -
8/9/2019 Arduino Processing Introducere
24/27
dimensions o X// y X//. Note the use o thesie,0unction to speciy the width and
height o the window.
size(#00,#00);
rectode(*++);
rect("00,"00,#0,"00);
ellipse("00,0,0,0);
ellipse(8",0,",1#);
ellipse(""2,0,",1#);
line(20,"50,80,"0);
line(""0,"50,"#0,"0);
http://www.processing.org/reference/size_.htmlhttp://www.processing.org/reference/size_.html -
8/9/2019 Arduino Processing Introducere
25/27
"erial %ort Co##unications8he Serial lirary is or reading and writing data to and rom e(ternal de"ices one yte at
a time. !t allows two computers to send and recei"e data. 8his lirary has the e(iility to
communicate with custom microcontroller de"ices and to use them as the input or output
to +rocessing programs. 8he serial port is a nine pin !2% port that e(ists on many +)s andcan e emulated through USB.
Issues with the "erial ibrary an$ specic platforms are $ocumente$ on the ProcessingWiki.
SerialSerial
a"ailale,0
read,0
read)har,0
readBytes,0readBytesUntil,0
readString,0
readStringUntil,0ufer,0
uferUntil,0
last,0
last)har,0write,0
clear,0
stop,0
list,0
Serial J"entserialJ"ent,0
Using the Serial +ort
)ll +latforms
If you're using a program to check to see if the serial port is working, then it won't be availableto Processing. That means that if you're using HyperTerminal or whatever to see if your serial deviceis working, then you need to quit out of that application before using that port with Processing.
erial.list!" will only list the ports that are currently available. o if you have a program that's
using the serial port that you want to use for your Processing sketch, it's not going to be available, oreven listed.
If you get an error reading something like this#
3467 '' ersion %is%atc9
:ar version ''-#.0-!
native lib ersion ''-#."-pre"
http://wiki.processing.org/w/Serial_Issueshttp://wiki.processing.org/w/Serial_Issueshttp://wiki.processing.org/w/Serial_Issueshttp://processing.org/reference/libraries/serial/Serial.htmlhttp://processing.org/reference/libraries/serial/Serial_available_.htmlhttp://processing.org/reference/libraries/serial/Serial_read_.htmlhttp://processing.org/reference/libraries/serial/Serial_readChar_.htmlhttp://processing.org/reference/libraries/serial/Serial_readBytes_.htmlhttp://processing.org/reference/libraries/serial/Serial_readBytesUntil_.htmlhttp://processing.org/reference/libraries/serial/Serial_readString_.htmlhttp://processing.org/reference/libraries/serial/Serial_readStringUntil_.htmlhttp://processing.org/reference/libraries/serial/Serial_buffer_.htmlhttp://processing.org/reference/libraries/serial/Serial_bufferUntil_.htmlhttp://processing.org/reference/libraries/serial/Serial_last_.htmlhttp://processing.org/reference/libraries/serial/Serial_lastChar_.htmlhttp://processing.org/reference/libraries/serial/Serial_write_.htmlhttp://processing.org/reference/libraries/serial/Serial_clear_.htmlhttp://processing.org/reference/libraries/serial/Serial_stop_.htmlhttp://processing.org/reference/libraries/serial/Serial_list_.htmlhttp://processing.org/reference/libraries/serial/serialEvent_.htmlhttp://wiki.processing.org/w/Serial_Issueshttp://wiki.processing.org/w/Serial_Issueshttp://processing.org/reference/libraries/serial/Serial.htmlhttp://processing.org/reference/libraries/serial/Serial_available_.htmlhttp://processing.org/reference/libraries/serial/Serial_read_.htmlhttp://processing.org/reference/libraries/serial/Serial_readChar_.htmlhttp://processing.org/reference/libraries/serial/Serial_readBytes_.htmlhttp://processing.org/reference/libraries/serial/Serial_readBytesUntil_.htmlhttp://processing.org/reference/libraries/serial/Serial_readString_.htmlhttp://processing.org/reference/libraries/serial/Serial_readStringUntil_.htmlhttp://processing.org/reference/libraries/serial/Serial_buffer_.htmlhttp://processing.org/reference/libraries/serial/Serial_bufferUntil_.htmlhttp://processing.org/reference/libraries/serial/Serial_last_.htmlhttp://processing.org/reference/libraries/serial/Serial_lastChar_.htmlhttp://processing.org/reference/libraries/serial/Serial_write_.htmlhttp://processing.org/reference/libraries/serial/Serial_clear_.htmlhttp://processing.org/reference/libraries/serial/Serial_stop_.htmlhttp://processing.org/reference/libraries/serial/Serial_list_.htmlhttp://processing.org/reference/libraries/serial/serialEvent_.html -
8/9/2019 Arduino Processing Introducere
26/27
river
-
8/9/2019 Arduino Processing Introducere
27/27
$unning processing as root will often get rid of the errors, but that's obviously not a good
solution for a million reasons !among them# beta code that runs as root and handles filesC yeahgreat..."