You are on page 1of 27

Arduino 3 -

Variables and More


Mr. McBrien
TEJ2O
Yesterday

 Serial Monitor
 Breadboards
Today

 Using Variables
 While Loops
What are Variables for?
?
 Calculations
 Comparisons
 Strings
 Counters:
 “do x times, then stop.”
 Status :
 Done/Not Done

4x + 3y = 23
 Red/blue
Variable Types
 char
 byte
 int
 unsigned int
 long
 unsigned long
 float
 Double
 bool
 String (you can also use an array of characters)
Declaring Variables
Before you can use a variable, you
have to tell the Arduino about it:
What kind of variable it is
Its name
(an initial value)

(Global)variables are only declared once; declarations


are placed above the setup section of the Sketch.
Syntax

 int InputVariable1;
 int InputVariable2 = 0 ; // Either line is correct.
Allowable Variable Names

 Most strings can be used as variable names


 Only commands can’t be used
 Remember that it’s case sensitive
Programming Tip
P4 Need!
 Use variable names that describe the purpose of
the value
 E.g.:
 NumberOfIterationsCompleted

Your variable name should make its purpose obvious.


Initialising Variables

 Many sketches will crash if there is no value for a


variable at the start
 After you declare your variable, you can give it an
initial value that fits with your Sketch logic
Integers

 Integers are numbers with no decimal.


 Usually,
these are used in programming for
counting purposes:
 “Move 4 balls into the end zone, and then
stop.”

We’ll see more on this in part 2.


Calculations in Arduino

What might you calculate?


Distance
Speed
Temperature
Anything
Calculations
 Real numbers are typically involved in calculations.
 Syntax for calculations is simple
 Using variables or constants:
 CalculatedValue = Value1 + Value2;
 CalculatedValue = Value1 - Value2;
 CalculatedValue = Value1 * Value2;
 CalculatedValue = Value1 / Value2;
 Etc.
Activity 1

 Useyour serial monitor code to ask


the user their age.
 Convert their age to days, and tell
them:
 “You’re 4000 days old!”
Part 2 – Loops
Mr. McBrien
TEJ3M
Arduinos Repeat the Loop Section

 …but what happens when


we want an activity to
stop after something
happens?
For Loops
 Allprogramming languages support the idea of
performing a function a number of times
 In the Arduino:
For Loops
What if we’re not sure how many times
we want to perform the function?
 …One of the key ideas of the Arduino is that it can
react to its environment.
Conditional Loops

 Sometimes a function needs to continue


until a situation changes:
 “Keep moving forward until a bump sensor
is triggered”
 “Movethe robot arm down until the laser
beam is blocked”
 More?
While
 While loops keep iterating while a condition is
met.
 TheSketch will keep checking the condition,
and stop when the condition is no longer true:
 E.g.:“While the temperature is below 100
degrees, keep applying power to the heater.”
Waiting for the Serial Monitor

If there is no command in the while


statement, it waits until the condition
is no longer met.
Activity 2 - Nightlight

 Implement the system from the While loop


tutorial:
 https://
www.arduino.cc/en/Tutorial/WhileStatementCondition
al?from=Tutorial.WhileLoop
Reading Pins?

 The nightlight implementation reads


information from a pin
 This is used to decide if it’s dark.
 We will cover reading inputs in a few days.
Key Points
 Variables keep track of numbers, situations,
etc.
 Variableshave types that match their
application.
 Every variable is declared and initialised.
 Some loops don’t run forever.
 Whileloops stop until their condition is no
longer met.
Homework

Variables
Nightlight
References

 https://
www.arduino.cc/en/Reference/VariableDeclar
ation

 https://www.arduino.cc/en/reference/for
 https://www.arduino.cc/en/reference/while
 https://www.arduino.cc/en/Tutorial/WhileSta
tementConditional?from=Tutorial.WhileLoop

You might also like