Download presentation
Presentation is loading. Please wait.
1
Arduino Part #3 Variables
2
What is a Variable? Lets have a discussion about a powerful and semi-confusing programming topic - variables. A variable is like a bucket. You choose what types of stuff you want in the bucket and can change the contents as often as you like. When you declare a variable, you are telling the program two things, first - what types of things you plan to put in the bucket, and secondly, what the name of the bucket is so you can refer to it later.
3
If you tell the program you will be putting fluids in the bucket, than you can go all day filling it with soda, water, and iced tea - but the second you try to fill it with rocks, the compiler will call you out on your discrepancy. Only fluids go in a bucket declared for fluids. To declare a variable, you write the type of contents it will hold followed by the name: ex fluid bucketVariable; Notice in the above declaration statement that the word fluid is a different color - that is because Arduino knows variable data types - and they get a special color to reduce confusion and of course, because they are cool.
4
Variables There are several types of variable data types you can declare. In this lesson we will discuss the integer data type. You probably know that an integer is a whole number (no decimals). For Arduino an integer is a number from -32,768 to 32,767. If you try to put a number bigger than that into an integer variable, the value will roll over to the opposite side like a game of Pac Man. If you add 5 to 32,767, you would get -32,764. If you subtracted 5 from -32,768 you would get 32,763. Integer is abbreviated int. Since an integer is an Arduino data type, it will change color to an orange. Here is an example of how to declare a variable < int led;> This tells the Arduino that a variable led is going to contain an integer.
5
Variables The name of the variable can be whatever you want with certain restrictions. There are also a couple good rules to follow... The variable name should be descriptive of its function, for example the ledPin variable could be the pin number that you put your LED into on your Arduino board. By rule, most variables start lowercase. Variable names cannot be the same as keyword names. Ex pinMode or digitalWrite.
6
Now what if we want to put something in the bucket
Now what if we want to put something in the bucket? We first need to declare the variable, this tells the Arduino what type of data is going into the variable. We want to put an integer in it so we use the “int” Declaration. When we assign the value, it is called initialization, and we use the equal sign to do so. It looks like this. int led; //first we declare the variable led = 13; //now we initialize the variable This means the led is equal to 13 anywhere in the program.
7
Variables Or, we can initialize and declare a variable at the same time... int led = 13; //declare and initialize a variable with a single statement This statement will go above the “void setup” section.
8
Lets Try it Out If your program from part 2 is not open go ahead an open up the Arduino IDE and then open your program from part 2. If you have to build the circuit again the instructions will be on the next slide, if you don’t then just skip that slide.
9
Lets Build a Circuit We are going to build this circuit in the diagram on the right. This circuit contains 5 things. Part 1 The Arduino board. Part 2 The breadboard. Part 3 Two wires. Part 4 An LED Part 5 A 330 ohms resistors
10
Lets Make the Changes Look at the code on the right and make the changes to your program
11
Lets Make the Changes Now this time before we upload the sketch, click on the checkmark button. This will verify that your sketch is correct. If it is not the error section at the bottom will tell you what the problem is. If your program is correct click the arrow button to send it to the Arduino.
12
Why We Use Variables You noticed that y0ur circuit performed exactly how it did the last time. The only change we made was to add the variables. So why do we do this. It is easier to change your program when you use variables, because we only have to make the change once compared to having to change every line. For example if we wanted to change the delay from 1000 to 200 we would have to go back and locate each delay and then make the change from a 1000 to 200. In this small program not the hard but in a large one with maybe 50 delay commands that would be very difficult
13
Variables Easy to Change
If we wanted to change the delay time using variables all we have to do is change the line “int time = 500” to maybe a That is the only line we have to change, because the program looks at the variable “time” and then inserts the value it is equal to. Lets try it out! In your program change the variable “time” to equal a different number. And download it do it a couple times with different numbers and observe the differences.
14
Credit Call me over when this circuit is complete so you get credit for building the circuit.
15
Review Ok What have learned? Variables are used to store information.
We need to tell the Arduino what is going to be stored in the variable and we do this by using the “int” command which stands for integer. Ex: int led this says that the variable will contain an integer. To initialize a variable we set it equal to a number for example: int delay = This declares the variable as an integer and assigns the value 1000 to it. We also saw that changing your program is easier with variables, you just need to change one line instead of many. Quiz time, go to schoology and take the Part 3 Quiz.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.