variables and constants - thinkcrateto use variables: 1. create the variable (declaring) 2. put data...

16

Upload: others

Post on 13-Jul-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Variables and Constants - ThinkCrateTo use variables: 1. Create the variable (Declaring) 2. Put data in variable (Assigning) 3. Copy the data out (Calling) •What is the advantage
Page 2: Variables and Constants - ThinkCrateTo use variables: 1. Create the variable (Declaring) 2. Put data in variable (Assigning) 3. Copy the data out (Calling) •What is the advantage

Arduino Sketch

Values

1) Variables

2) Constants

FunctionsStructure

1) Syntax

2) Main structure

3) Control

structures

4) Operators

Page 3: Variables and Constants - ThinkCrateTo use variables: 1. Create the variable (Declaring) 2. Put data in variable (Assigning) 3. Copy the data out (Calling) •What is the advantage

• Values come in two versions:

1. Variables

– Value can be changed in the sketch

2. Constants

– Value cannot be changed in the sketch

Page 4: Variables and Constants - ThinkCrateTo use variables: 1. Create the variable (Declaring) 2. Put data in variable (Assigning) 3. Copy the data out (Calling) •What is the advantage

Data typeVariable Name

“Container”

AssignOnly once!!

CallCopy!!

“Value”

Page 5: Variables and Constants - ThinkCrateTo use variables: 1. Create the variable (Declaring) 2. Put data in variable (Assigning) 3. Copy the data out (Calling) •What is the advantage

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?

Page 6: Variables and Constants - ThinkCrateTo use variables: 1. Create the variable (Declaring) 2. Put data in variable (Assigning) 3. Copy the data out (Calling) •What is the advantage

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.

Page 7: Variables and Constants - ThinkCrateTo use variables: 1. Create the variable (Declaring) 2. Put data in variable (Assigning) 3. Copy the data out (Calling) •What is the advantage

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

Page 8: Variables and Constants - ThinkCrateTo use variables: 1. Create the variable (Declaring) 2. Put data in variable (Assigning) 3. Copy the data out (Calling) •What is the advantage

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

Page 9: Variables and Constants - ThinkCrateTo use variables: 1. Create the variable (Declaring) 2. Put data in variable (Assigning) 3. Copy the data out (Calling) •What is the advantage

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

Page 10: Variables and Constants - ThinkCrateTo use variables: 1. Create the variable (Declaring) 2. Put data in variable (Assigning) 3. Copy the data out (Calling) •What is the advantage

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

Page 11: Variables and Constants - ThinkCrateTo use variables: 1. Create the variable (Declaring) 2. Put data in variable (Assigning) 3. Copy the data out (Calling) •What is the advantage

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

Page 12: Variables and Constants - ThinkCrateTo use variables: 1. Create the variable (Declaring) 2. Put data in variable (Assigning) 3. Copy the data out (Calling) •What is the advantage

int led;

led = 13;Assignment operator Data

int led = 13;

You can do both, declaration and assignment of variable in one statement.

Page 13: Variables and Constants - ThinkCrateTo use variables: 1. Create the variable (Declaring) 2. Put data in variable (Assigning) 3. Copy the data out (Calling) •What is the advantage

int led = 13;

void setup() {pinMode (led, OUTPUT);

}

By using led, the integer data “13” is called into the code.

Page 14: Variables and Constants - ThinkCrateTo use variables: 1. Create the variable (Declaring) 2. Put data in variable (Assigning) 3. Copy the data out (Calling) •What is the advantage

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

Page 15: Variables and Constants - ThinkCrateTo use variables: 1. Create the variable (Declaring) 2. Put data in variable (Assigning) 3. Copy the data out (Calling) •What is the advantage

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);

}

Page 16: Variables and Constants - ThinkCrateTo use variables: 1. Create the variable (Declaring) 2. Put data in variable (Assigning) 3. Copy the data out (Calling) •What is the advantage

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.