Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
22 views

[4] Exploration_ Arduino Structure using Variables (student) [Physical Computing][Arduino][Intro to Arduino](副本)

This document discusses the use of variables in Arduino programming, emphasizing their role in improving code readability and control over components. It provides instructions for building circuits with LEDs and potentiometers on a breadboard, along with coding exercises in Tinkercad. Additionally, it covers the mapping of values for controlling LED brightness and the importance of using variables in programming.

Uploaded by

zichenzengca
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

[4] Exploration_ Arduino Structure using Variables (student) [Physical Computing][Arduino][Intro to Arduino](副本)

This document discusses the use of variables in Arduino programming, emphasizing their role in improving code readability and control over components. It provides instructions for building circuits with LEDs and potentiometers on a breadboard, along with coding exercises in Tinkercad. Additionally, it covers the mapping of values for controlling LED brightness and the importance of using variables in programming.

Uploaded by

zichenzengca
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Arduino Exploration

Arduino Structure using Variables

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: 6e to LED short leg 6: 11e to Pin 6 9: LED long leg to 20e

Step 2- Setup program in Tinkercad:


1. Click the Code button at the top right of 2. Change the editor from Blocks to Text
the screen. by selecting Text in the drop down
menu.

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.

When prompted to run the code on your Arduino device:


1. Click the download button from the 2. Open the code in the Arduino program
code editor:

found on your computer. Click the arrow


button at the top to upload the program
to your device.

Code #1: Explore using variables to address components


CodeHS | 2
Type the following code into the loop function.

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

Code #2: Explore using variables to control components

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 (+)

2: Potentiometer to 2-4i 4: 3f to Pin A0


Type the following code into the editor.

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.

2) What do you think the analogRead command does?


I think the analogwrite command makes the yellow LED have the brightness of potvalue.

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

5) The syntax of the map function is:


map(value, fromLow, fromHigh, toLow, toHigh)
Explain what the parameters value, fromLow, fromHigh, toLow, and toHigh are. (Note: You
can find this information in the Parameters section of the above linked resource.)
value: the number to map.

fromLow: the lower bound of the value’s current range.

fromHigh: the upper bound of the value’s current range.

toLow: the upper bound of the value’s current range.

toHigh: the upper bound of the value’s target range.

6) Fill in the commands below if you wanted to map a value saved in the variable count from
one range to another:

a. From a range of 0-10 to a range of 0-100

map(__count __, _____0_____, ______10____, __0________, ___100_______)


b. From a range of 0-10 to a range of 10-0

map(__count ____, _____0_____, ______10____, ____10______, _____0_____)


c. From a range of 10-0 to a range of 100-0

map(___count ____, ____10______, _____0_____, _____100_____, ____0______)

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

1. Why do we use variables in our programs?


Because we want to make the program easy to understand.

2. What does a potentiometer do?

It changes the value of brightness

3. Which ports are connected to port 4c in a common breadboard?

4. Which ports are connected to port 23i in a common breadboard?

5. How are negative and positive ports connected in a breadboard?

CodeHS | 7

You might also like