Congratulations for completing your first lesson and running your first sketch. Like I mentioned in lesson 1, having pre written code can be quite useful in order to learn from. For me I used plenty of code to save time and also to modify from. In this lesson we will go over some basic topics to get you on your way to understanding what the hell is happening. Lets take a closer look at the sketch from lesson 1.
Arduino "Blink" Sketch:
/* * Blink * * The basic Arduino example. Turns on an LED on for one second, * then off for one second, and so on... We use pin 13 because, * depending on your Arduino board, it has either a built-in LED * or a built-in resistor so that you need only an LED. * * http://www.arduino.cc/en/Tutorial/Blink */ int ledPin = 13; // LED connected to digital pin 13 void setup() // run once, when the sketch starts { pinMode(ledPin, OUTPUT); // sets the digital pin as output } void loop() // run over and over again { digitalWrite(ledPin, HIGH); // sets the LED on delay(1000); // waits for a second digitalWrite(ledPin, LOW); // sets the LED off delay(1000); // waits for a second }
The sketches we work with are written in "C", this is a powerful and very popular programming language, I will warn you it does take abit to get used to but with practice it will be a breeze.
We will dived up the sketch and discuss the different functions as well as what does what.
1# Comments: Comments are very important with programming. This allows you to write your code and then place comments beside to indicate what that code will do. To do this, notice the use of
" /* " and " */ " in the first line of code. This is basically like a bracket. Anything inside will be ignored by the arduino. It is for the user to input details about the code. Make a point of starting your code with a comment indicating the code and what its function plans to be. ** You can also comment things by using " // " and then typing your comment. You will notice what I mean as you continue the lesson.
/*
* Blink
*
* The basic Arduino example. Turns on an LED on for one second,
* then off for one second, and so on... We use pin 13 because,
* depending on your Arduino board, it has either a built-in LED
* or a built-in resistor so that you need only an LED.
*
* http://www.arduino.cc/en/Tutorial/Blink
*/
2# Variables: This is the first line we see of the code itself, to the right of the code you will notice it has a comment detailing the code. FYI all computer sentences end with a " ; ".
So basically we are looking at a computer sentence. This sentence is telling the computer that we want to create a box named "ledPin", and to put the number 13 in that box.
int ledPin = 13; // LED connected to digital pin 13
box-type
|
box-name
|
=
|
stuff-to-put-in-box
|
---|---|---|---|
int
|
ledPin
|
=
|
13
|
int: short for integer or whole number
ledPin: name of the box
= : which basically says that the variable (box) named ledPin should start out equaling whatever is after the = 13: a whole number (integer) which is assigned to ledPin
= : which basically says that the variable (box) named ledPin should start out equaling whatever is after the = 13: a whole number (integer) which is assigned to ledPin
3# Procedures: Used to group statements together so they can be referred to with one name. Think of it like step by step.
void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
This bunch of code is an example of a procedure, a procedure is a collection of statements,
returned value
|
procedure name
|
(input values)
|
{ statements }
|
---|---|---|---|
void
|
setup
|
()
|
{ pinMode(ledPin, OUTPUT); }
|
As we look at the procedure, we see that it is named setup and it has no input values and it returns void.
What is "VOID"? Void basically means there is nothing to do. There is nothing to show or do.
void setup() // run once, when the sketch starts
pinMode(ledPin, OUTPUT); // sets the digital pin as output
void loop() // run over and over again{digitalWrite(ledPin, HIGH); // sets the LED ondelay(1000); // waits for a seconddigitalWrite(ledPin, LOW); // sets the LED offdelay(1000); // waits for a second}
As you may notice we have a new procedure, this one called loop which also has no inputs or output. This procedure has multiple statements, one after the other. This code tells arduino to stop for a little bit.
To do this, the statement performs a procedure call.
procedure name
|
(input values)
|
;
|
---|---|---|
delay
|
(1000)
|
;
|
This means that somewhere out there, there's a procedure something like this:
void delay(number of milliseconds){"Dear Arduino. Stop what you're doing for (number of milliseconds)}
As you hopefully notice the first statement is "digitalWrite" this is saying here turn on the light, wait a second turn it off wait a second turn it on. And over and over and over lol
Final notes: Keep in mind the Arduino will always do whats included in the setup first and then will do the loop over and over and over and over.
No comments:
Post a Comment