Upload
others
View
13
Download
0
Embed Size (px)
Citation preview
http://robotsclub.wordpress.com
Arduino Programming 101Lesson 2: Creating Your First Sketch
http://robotsclub.wordpress.com
Topics to be CoveredProgramming Syntax
Arduino Syntax
Setting Up Your First Sketch
Completing Your First Sketch
The Final Product
http://robotsclub.wordpress.com
Programming SyntaxCode is written as a series of commands
In Arduino, commands are written line by line. However:
When reading code, compilers don’t think like people
Most Compilers (Arduino) don’t see tabs or returns
As a result, we have to tell the compiler where each line ends and when we want to target multiple lines, we must indicate the start and end of the lines to target
http://robotsclub.wordpress.com
Arduino Syntax
The Semi-Colon ; is used to end a command/line
Curly Brackets { } are used to indicate the beginning and end of a targeted section of code
Parenthesis ( ) are used to pass variables to functions
http://robotsclub.wordpress.com
Arduino Syntax (cont.)
Square Brackets [ ] are used to indicate arrays (or lists of variables)
Two Forward Slashes // are used to indicate a comment (or a single line that the compiler ignores)
/* & */ are used to indicate the beginning and end of a multi-lined comment (or multiple lines that the compiler ignores)
http://robotsclub.wordpress.com
We will begin with the basic code that every Arduino program requires.
Reference: Examples>01.Basics>BareMinimum
Every Arduino program begins with a setup method
When the Arduino resets, it runs the code in the setup method ONCE. This code often defines important variables and other functions that only need to be run once each time the program is run
!void setup() {! // put your setup code here, to run once:!!}!
Setting Up Your First Sketch
http://robotsclub.wordpress.com
The other necessary method for every Arduino program is the loop method
After the setup method iscompleted, the Arduino runs the loop method over and over again until it is powered down or reset.
This is much like the Mindstorms NXT programs where a mini-sumo bot would run all of its code inside a loop that repeated forever
!void loop() {! // put your main code here, to run repeatedly: ! !}!
http://robotsclub.wordpress.com
Completing Your First SketchGoal: Write an Arduino Program that will “blink” the On-Board LED
Step 1: Start with Bare Minimum Code
Step 2: Define LED pin for later use
All Arduino UNO’s have an LED built into pin # 13, so when you activate pin 13, the LED turns on
int led = 13;
void setup() {!!}!void loop() {!!}
http://robotsclub.wordpress.com
Step 3: In the Setup method, tell Arduino that pin 13 will be an OUTPUT pin, or a pin you will be using to send a signal from the Arduino
Quick Method Introduction: digitalWrite( )
Argument 1 should be pin number
Argument 2 is a constant value. Either HIGH or LOW. HIGH turns the signal on and LOW turns the signal off
digitalWrite(pin, value);
http://robotsclub.wordpress.com
For pausing (so the light stays on for a given amount of time), we will use the delay( ) method
Step 4: Writing the Loop Method
1) Turn LED on
2) Pause
3) Turn LED off
4) Pause
void loop() {!! digitalWrite(led, HIGH);!! delay(1000); !! digitalWrite(led, LOW);!! delay(1000); !!}
delay(milliseconds);
http://robotsclub.wordpress.com
The Final ProductThis completed code can be uploaded to the Arduino UNO
Plug the USB cord from the Arduino to your PC
Click Verify and then Upload in the Arduino Program
Enjoy the Blinking LED
int led = 13;!!void setup() { ! pinMode(led, OUTPUT); !}!!void loop() {! digitalWrite(led, HIGH);! delay(1000); ! digitalWrite(led, LOW);! delay(1000); !}!
Reference: Examples>01.Basics>Blink
http://robotsclub.wordpress.com
The EndQuestions? Comments? Concerns? Suggestions? Please let me know!