Uploaded by zuhaib asad

Homework 2

advertisement
ESC-100: Exploring Engineering
Fall 2019
Homework 2: An Introduction to Arduino Programming
Due: Thursday, September 26 at start of lab
The exercises below are meant to achieve two goals: a) to help you develop your computational thinking
skills (i.e., the application of engineering design principles to programming language), and b) to provide an
introduction to the specific syntax required for Arduino programming. Do not worry if you do not have a
background in programming!
While we generally encourage discussion of concepts with your peers, given the heavily guided nature of
the exercises below, you are expected to work through the entire assignment on a purely individual basis.
Please direct any questions or difficulties you are having to your section instructor. Start early to ensure
you request any needed assistance with enough notice!
Part A: Writing a Functional Decomposition for a Program
Disclaimer: Do not worry about how complicated the hardware set up may look in the video for this Part A!
You will only be writing a code functional decomposition for it; you will build up your hardware and
programming skills from a very basic starting point in Part B below.
Consider the circuit whose operation is shown in this video: link. Note that the beginning of the video
corresponds to after the program was uploaded (the “Program Start” in the flowchart below); nothing
happens thereafter until the first button push and release.
Write a functional decomposition for what the Arduino program (code) must do. You may find it helpful to
write this decomposition in flowchart style, since the program (like all programs) executes each line of code
sequentially. To help you get started, the beginning of the decomposition might look something like this:
Program
Start/Upload
…
Part B: Using Arduino Programming to Control Output Voltage
Please watch Video 1 of the Introduction to Arduino Programming online lecture linked here: link. After
watching the video, complete the exercises below and answer the questions in bold in your homework
submission (for this and the following parts).
1. In the video, you were just directed to modify the circuit to remove the button. Draw the new circuit
schematic corresponding to the modified circuit.
2. Run the program with the digitalWrite HIGH command in void loop, as it was in the video. Then, move
the digitalWrite HIGH command to the end of void setup (after the PinMode command), and re-run the
program. How does the behavior of the LED compare in the two cases? Why does this make
sense based on the explanation of void setup and void loop in the video?
3. Replace the “HIGH” in your digitalWrite command with “LOW”, and re-run the program. What happens
to the LED, and why do you think this is?
4. Move the digitalWrite LOW command down to void loop. Immediately before it, i.e., on the preceding
line, type the digitalWrite HIGH command. Run the program; you should see the LED light up, but more
dimly than in the Problem 2 case above (this is more difficult to see for some LED colors than others).
Why does this make sense, again, based on your understanding of void loop and the answers
to the previous two Problems?
Part C: Incorporating Delay(s)
After completing Part B, please watch Video 2 of the Introduction to Arduino Programming online lecture
linked here: link.
1. Move the delay command down one line so that it is between the digitalWrite HIGH and digitalWrite
LOW commands, re-upload, and observe the behavior of the LED. What does it do, and why?
2. Now add the same delay command to the first line of void loop so that you have delay → digitalWrite
HIGH → delay → digitalWrite LOW. Re-upload and observe the behavior of the LED. What does it do
this time, and why?
Part D: Adding a Second LED
Now, add a second LED to your system, so that your setup looks something like this (your RedBoard pin
choices and placement of components may vary a bit):
Note: we are no
longer using the
power rail (the
leftmost red column)
on the breadboard;
we are connecting
Pins 7 and 6 directly
to our two LED
circuits
1. Combining what you have learned in the preceding parts, please write an Arduino Program that results
in the behavior shown in the video here: link. Note that all pauses are 1 s = 1000 ms long.
a. In your homework submission, include your code (either handwritten, typed, or printed
directly from the Arduino software).
b. At the beginning of Design Studio on Thursday 9/26, you will demonstrate the working
setup for your instructor; this will be part of your grade for this homework.
In next week’s homework, you will further develop your Arduino skills by learning how to use input (e.g.,
pushing a button) to control output (e.g., lighting up an LED) using Arduino programs. Eventually, you will
work on creating the system shown in Part A of this homework!
Download