Upload
bridget-bates
View
217
Download
0
Embed Size (px)
Citation preview
1
Introduction to Coding
2
Example Codes
A lot of example codes are given
with Arduino IDE
A code can often be based on a previous example
rather than being created from scratch
3
BlinkA simple
code used to blink an LED plugged into
pin 13
The following slides will go through the ‘blink’ code in
detail, line by line
4
Comments
• The first lines of code are displayed in gray• This indicates that they are comments for humans to read
and not part of the actual code
5
Comments
• There are 2 ways to incorporate a comment into a code• // - Single line comment• /* */ - Multiline comment where everything between the
two asterisks is part of the comment
6
Variables
• The next line of code introduces a variable called ‘led’ and sets it equal to 13• The name of the variable is chosen by the programmer• In this case ‘x = 13’ or ‘table = 13’ would all suffice• It is common to choose a variable name that correlates to
what it is being used for• In this case 13 will represent the pin the LED is plugged into
7
Variables
• The word ‘int’ preceding the variable name is used to tell the Arduino what we plan on storing in the variable ‘led’• A variable of the ‘int’ type can store any integer between
32,768 and -32,768. It cannot store a fraction or decimal• A variable of the ‘long’ type has a range of -2,147,483,648 to
2,147,483,648. However, it uses up more memory• In this case, in order to declare the variable ‘led’ as a ‘long’
type we would have written long led = 13; • A variable of the ‘float’ type has a range of -3.4028235E+38
and 3.4028235E+38. It can contain decimals (float led = 13; )
8
Routines
• Each Arduino program is called a SKETCH and has two required functions, called ROUTINES. One is the ‘setup’ and one is the ‘loop’• Each routine is preceded by the word ‘void’ and followed
with a set of parentheses ‘( )’ and curly braces ‘{ }’• void setup ( ) { } - All of the code within the curly braces is
part of the setup and runs once when the program begins.• void loop ( ) { } - This function is run AFTER the setup has
finished. All of the code within the curly braces will be run repeatedly until power is removed.
9
pinMode
• In this routine there is only one line of code, ‘pinMode’• Each digital pin 0 – 13 can act as either an Input or an
Output to your system, and needs to be declared as either• This is because fundamentally Inputs and Outputs behave as
opposites. An Input is ready to absorb energy sent into it by a sensor, while an Output contains higher amounts of energy waiting to be spit out and turn something on• In this case pin 13 is an Output since we have an LED
plugged in there. We can use the variable ‘led’ in place of the number thirteen since we already declared led = 13;
10
digitalWrite
• The next piece of code is the ‘void loop’ routine of the sketch•Once a pin is set to output it can be set to either HIGH (5 Volts)
or LOW(0 volts). This essentially means turn ON or OFF. • There are two pieces of information in the line of code: the pin
we want to control and whether is should be on or off•We have declared led= 13 so we can write ‘led’ as the pin #• HIGH means to turn on the pin, or send out 5V through pin 13
11
delay
• The next line of code is the ‘delay ’ function• This is used when we want the Arduino to pause and not
perform any action• The delay is measured in microseconds, so 1000 = 1 second• In this ‘loop’ the first line turns on a led plugged into pin 13,
and the second line tells the controller to wait for 1 second
12
RunTo compile your sketch, click the checkmark.
Make sure your Arduino is plugged into an available USB port.
Click the arrow to download the program to Arduino. If everything is attached correctly. The LED should blink.