Upload
others
View
17
Download
0
Embed Size (px)
Citation preview
Arduino Sketch
Values
1) Variables
2) Constants
FunctionsStructure
1) Syntax
2) Main structure
3) Control
structures
4) Operators
• Values come in two versions:
1. Variables
– Value can be changed in the sketch
2. Constants
– Value cannot be changed in the sketch
Data typeVariable Name
“Container”
AssignOnly once!!
CallCopy!!
“Value”
To use variables:
1. Create the variable (Declaring)
2. Put data in variable (Assigning)
3. Copy the data out (Calling)
• What is the advantage to using variables?
To use variables:
1. Create the variable (Declaring)
2. Put data in variable (Assigning)
3. Copy the data out (Calling)
• What is the advantage to using variables?You can assign and change the variable easily whenever you need to change it throughout your program. It gives some meaning to understanding the variable too.
A variable is declared with the data type. In this example, the data type is “int”, which is an integer data type.
int led;
Data Type
Variable name
• Unique name• Don’t begin with
number
int data type is one of the most common data type used in Arduino variable.
There are many other data types in Arduino which will be covered in the future lessons.
Data type Data kind Range
Integer (int) +ve and –venatural numbers, including 0
-32,768 to 32,767
32,767 is the maximum integer value which the Arduino can store.
• What happens if we add 1 to this value?
• What will be the next value?
http://wallpaperswide.com/old_clock_2-wallpapers.html
When the hour hand reaches 12, what is the next hour?
Similarly if we go anti-clockwise, what is the minute before 1?
http://wallpaperswide.com/old_clock_2-wallpapers.html
32,767 is the maximum integer value which the Arduino can store.
• What happens if we add 1 to this value?The data will rollover.
• What will be the next value?It will rollover to start from -32,768, which is the smallest value possible. (Just like a clock concept.)
http://wallpaperswide.com/old_clock_2-wallpapers.html
int led;
led = 13;Assignment operator Data
int led = 13;
You can do both, declaration and assignment of variable in one statement.
int led = 13;
void setup() {pinMode (led, OUTPUT);
}
By using led, the integer data “13” is called into the code.
int led = 13;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}
Can you find where are the 3 stages of using a variable?• Declare• Assign• Call
Declaring variable
Calling variable
Assigning variable
int led = 13;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}
const int led = 13;
When you add “const” to any data type, even if you accidentally change the value for the variable later on in the sketch, the value will not change. This is an important feature that “locks in” an important value in a variable.