[4] Exploration_ Arduino Structure using Variables (student) [Physical Computing][Arduino][Intro to Arduino](副本)
[4] Exploration_ Arduino Structure using Variables (student) [Physical Computing][Arduino][Intro to Arduino](副本)
Corresponding Material
1.4- Variables
Discussion
Variables help us develop programs in many ways. They allow us to hold and alter values
throughout our programs that can then be used to control various commands and make our
programs more readable, both for us and others. Until this point, all components we connect to
our Arduino are known in our code by the pin number they are connected to. Wouldn’t it be
easier if we could give them names, such as red_led?
We’ll also be using breadboards to connect our circuits in this exploration. Breadboards
are used to create temporary circuits very easily. We plug
components into the breadboard to connect components
together. Inside the breadboard are metal strips that are
connected in various ways so the current can flow through
different components in our circuit.
In the center of the breadboard, ports are labeled with a
number and a letter. Five ports in each row are connected to one
another, a-e and f-j. This means that anything plugged into 1a is also
connected to anything in port 1b, 1c, 1d, and 1e.
The vertical ports on each side of the breadboard are connected vertically throughout the
entire board. These are usually used to provide power to the board, with the red (+) column
being connected to the power and the black or blue (-) column being connected to the ground.
Note: The vertical rows on either side are not connected to one another.
Needed Materials
(1) Arduino (1) Breadboard (9) male to male (2) LED (2) resistor (1) potentiometer
wire
Class Exercise
Step 1- Build Circuits (Simulation and Physical): Follow the diagram below to build a circuit with
two LEDs. Be sure to pay attention to the placement of the long LED legs.
Connections
1: GND pin to (-) 4: LED long leg to 7e 7: (-) to 19b 10: Resistor to
CodeHS | 1
20c and 24c
2: (-) to 6b 5: Resistor to 7c and 11c 8: 19e to LED short leg 11: 24e to Pin 5
3. Type the following code into the editor to set up each component:
1 int red = 6;
2
3 void setup() {
4 pinMode(red, OUTPUT);
5 pinMode(5, OUTPUT);
6 }
7
8 void loop() {
9
1 }
0
We will be adding commands to the loop function. When prompted to run the simulation,
click the Start Simulation button at the top of the screen. You can click the Code button to
minimize the code window while viewing the simulation.
8 void loop() {
9 analogWrite(red, 200);
1 analogWrite(5, 200);
0 }
1
1
Download the code to your Arduino and answer the following questions.
1) What do you see when you run the code on your Arduino?
I see two LED lights at the same time when you run the code on your Arduino.
2) What do you think is happening when we use the variable red instead of the value 6?
I think that every time the computer reads the variable red, it will switch it to 6.
3) What do you think will happen if you use a different variable name in place of red?
I think that result will be the same because very time the computer reads the variable red, it
will switch it to 6.
Go into the code editor and replace the red in the three places it exists in the program with your
own variable name. Make sure to follow the naming rules! Download this code to your Arduino.
4) Was your hypothesis correct? What happened when you ran the code?
My hypothesis was correct, when I ran the code, the two LEDs light at the same time.
Go into the code editor and add a second variable to control the other LED. Make sure to keep all
naming rules in mind!
5) How do you think variables could be used to control the brightness values in the
analogWrite commands?
I think the variable be used to control the brightness values in the analogWrite commands
could be “brightnessValue”
Using variables, complete the following program to set the brightness value of the red LED to
100 and the brightness of the yellow LED to 50:
CodeHS | 3
1 int red = 6;
2 int yellow = 5__________________;
3 int redBrigtness________________;
4 int yellowBrigtness_____________;
5
6 void setup() {
7 pinMode(red, OUTPUT);
8 pinMode(yellow, OUTPUT);
9 }
1
0 void loop() {
1 analogWrite(red, redBrigtness);
1 analogWrite(yellow, yellowBrigtness);
1 }
2
1
3
1
4
Edit Tinkercad & physical circuit: Place a potentiometer at pin A0 and connect to GND and 5V.
Additional Connections
CodeHS | 4
1: 5V to (+) 3: 2h to (-) 5: 4f to (+)
1 int yellow = 5;
2 int red = 6;
3 int potPin = 0;
4 int potValue = 0;
5
6 void setup() {
7 pinMode(yellow, OUTPUT);
8 pinMode(red, OUTPUT);
9 pinMode(potPin, INPUT);
10 }
11
12 void loop() {
13 potValue = analogRead(potPin);
14 potValue = map(potValue, 0, 1023, 0,
15 255);
16 analogWrite(yellow, potValue);
17 delay(15);
}
Download the code to your Arduino and answer the following questions.
1) What do you see when you run the code on your Arduino? What happens when you spin the
potentiometer?
I see that there is no LED lighting when I run the code on my Arduino, when I spin the
potentiometer, the yellow LED slowly lighting.
3) The values produced by the analogRead command are from 0 to 1023. What is the range of
values that can be used to control the brightness of an LED?
CodeHS | 5
0-255
4) Keeping the values produced by the analogRead command and the values that can be used
to control the brightness of an LED in mind, use this link
(https://www.arduino.cc/reference/en/language/functions/math/map/) to find out what the map
function does. Why do we need to use the map function in this program?
I need to use map function in this program because I won't let yellow LED have brightness
value both in the range 0-1023 and the range 0-255
6) Fill in the commands below if you wanted to map a value saved in the variable count from
one range to another:
CodeHS | 6
We are now using two variables, forwardPotValue and reversePotValue. Alter the program
so that when the potentiometer sets the yellow LED to the brightest value, it sets the red LED to
the lowest brightness and vice versa. When you’ve developed a successful program, record your
commands here:
1 int yellow = 5;
2 int red = 6;
3 int potPin = 0;
4 int potValue = 0;
5 int forwardPotValue = 0;
6 int reversePotValue = 0;
7
8 void setup() {
9 pinMode(yellow, OUTPUT);
10 pinMode(red, OUTPUT);
11 pinMode(potPin, INPUT);
12 }
13
14 void loop() {
15 potValue = analogRead(potPin);
16 forwardPotValue = map(potValue, 0, 1023, 0, 255);
17 reversePotValue = _map(potValue, 1023, 0, 255, 0);
18 analogWrite(yellow, forwardPotValue);
19 __________________________________________________;
20 delay(15);
21 }
Conclusion Questions
CodeHS | 7