The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by feigenbaumel, 2022-09-02 10:24:31

Priinciples of Engineering 2022

Textbook for 9th grade STEM class

Principles of Engineering

Series Circuit Load

To use a sensor in an electronic system, it must be integrated into an electric circuit. There are
different ways to do this, but the simplest way is to integrate a sensor into the series circuit you
learned about in your study of electronics. In this case, we generally use sensors with variable
electrical resistance. The electrical dimension of the resistance allows easy control of circuit
voltages, based on the principles of solving a series circuit (Ohm’s Law/voltage divider). This way,
we can easily activate the components that constitute the output of the system. In addition, the
electrical dimension of resistance is relatively easy to manipulate and convert to other electrical
dimensions, voltage and current, compared to more complex voltage or current conversions to
other dimensions.
Look at the following circuit you analyzed previously in your study of electronics.

This is a series circuit fed by a voltage source and containing two resistors. The voltage across the
resistor R2 can be calculated by the voltage division rule:

The equivalent resistance, Rtotal, is the sum of the resistors.
The voltage of the resistor R2 is also the voltage at point A. If you recall, “voltage point” is the
voltage between the point in question and the negative trigger of voltage source (or in our case,
Ground).
As such, the voltage at point A is determined by the voltage source and the resistor values. If we
want to connect an output device, load, we can connect it in place of R2 as follows:

CIJE | Unit 12: Sensors in Circuits 151

Principles of Engineering

We can set the R1 so that the voltage division will
provide the voltage we want on the load. If we want
high voltage on the load, we need low voltage to fall
across R1 and we will therefore set the R1to be small
R1 in relation to the resistance load.
If we want low voltage on the load, we need a higher
voltage to fall across R1 and we can therefore set R1
to be high in relation to the resistance load.

Do we always need to adjust the R1? Is there an
easier solution? Are you familiar with a component
that can change the resistance as needed? We can
use a component in electronics called a variable
resistor (potentiometer).

If we connect the variable resistor in place of the R1 we will get the following circuit:

R1 R1
A

Output

Now we can control the voltage to receive load more easily and according to the voltage division:

= =
+

We also have control over the R resistance of the potentiometer (RL specifies the output device’s
resistance).

152 Unit 12: Sensors in Circuits | CIJE

Principles of Engineering

Work sheet: Sensors in a Voltage Divider

Exercise 1 of 4

The following circuit is built from an R1 resistor, a temperature sensor (Rtemp) and voltage source.
The output voltage of the circuit is the voltage across the sensor.

The characteristics of the sensor:

Vout

RTemp

a. What would be the circuit’s voltage output at a temperature of 20°C? This is a two-step
process. First, we must calculate the thermistor resistance at that
temperature, and then we must compute Vout of the voltage divider
using that resistance. (You can find the exact resistance for any
given temperature by using two points on the line and solving the linear equation, y = mx
+ b, of the line.)
________________________________________________________________________
________________________________________________________________________

b. Will the rise in temperature cause a rise or fall in the voltage output? Explain your answer.
________________________________________________________________________
________________________________________________________________________

c. What are the maximum operating conditions for the thermistor?
________________________________________________________________________

d. If the microcontroller reading the Vout can't receive more than 5 volts, what is the
minimum value of R1 that can be used to ensure the controller does not receive too much
voltage?
________________________________________________________________________
________________________________________________________________________

CIJE | Unit 12: Sensors in Circuits 153

Principles of Engineering

Exercise 2 of 4

Below is an electrical circuit containing a thermistor, and a graph showing the resistance of the
thermistor as a function a temperature.

1. What is the thermistor resistance at a temperature of 50°C?
________________________________________________________________________

2. Calculate the voltage between the thermistor terminals installed in the electric circuit
when the temperature is 50°C (i.e. – fine Vout)
________________________________________________________________________

3. How will the voltage between the thermistor terminals change (increase or decrease) if
the temperature rises? Explain.
________________________________________________________________________
________________________________________________________________________

4. Challenge: Imagine that a fault in the circuit causes a short circuit between the R2 resistor
terminals (the resistance between the resistor terminals becomes equal to zero). Will the
voltage between the thermistor terminals (Vout) increase, decrease, or remain
unchanged?
________________________________________________________________________
________________________________________________________________________

154 Unit 12: Sensors in Circuits | CIJE

Principles of Engineering

Exercise 3 of 4

Below is a diagram of a temperature sensor circuit that includes an R resistor in series with the
sensor. Also in the diagram is the nature of resistance, the Rn sensor, as temperature dependent.
Sensor circuit:

• voltage at point A in the range of 0V to 1V will be shown as “0”
• voltage at point A in the range of 3V to 5V will be shown as “1”

1. Write a temperature dependent sensor resistance equation (hint: y=mx + b).
________________________________________________________________________

2. Calculate at what temperature the resistance value of the sensor will be 35kΩ.
________________________________________________________________________

3. Challenge: Write a general expression for the temperature dependence voltage at point
A. To do this you must combine the equation from answer 1 with the voltage divider
equation, where the equation from question 1 is being used to find R2 in the voltage
divider equation.
________________________________________________________________________

4. For what temperature range will the voltage value at point A be “0” (i.e. - 0V to 1V)?
________________________________________________________________________

5. For what temperature range will the voltage value at point A be “1” (i.e. - 3V to 5V)?
________________________________________________________________________

6. What is the output voltage range of the sensor at a temperature range of 300°C – 500°C?
________________________________________________________________________
CIJE | Unit 12: Sensors in Circuits 155

Principles of Engineering

Exercise 4 of 4

The diagram below shows a temperature sensor circuit with a light sensor located in a work
environment that requires temperature and lighting control. The drawing also includes
characteristics of both sensors. The luminescence intensity is measured in units called lux.

1. Calculate the voltage measurement at point A at an ambient temperature of 60°C and
light intensity of 100 lux.

2. What will the temperature and lighting level be when the voltage at A is the smallest it
can be? Explain, and calculate the voltage based on the data presented in the graph.

3. Is it possible for the voltage drop across both sensors to have identical value? Explain your
answer and specify at what sensor resistance and at what temperature and light intensity
this might occur (base your answer on the data in the graph).

156 Unit 12: Sensors in Circuits | CIJE

Unit 13: The
Capstone
Project

Principles of Engineering

Preface

This unit provides guidance on the key steps required for the development of the Capstone
Project and identifies the key deliverables at the different stages of project development.

Section Units

➢ Section 1 – Brainstorming
➢ Section 2 – Background and Market Research
➢ Section 3 - Empathize
➢ Section 4 - Define
➢ Section 5 - Ideate
➢ Section 6 – Engineering Drawings
➢ Section 7 - Prototype
➢ Section 8 – Test and Reflect
➢ Section 9 - Website
➢ Section 10 - Video

Learning Objectives

By the end of this unit students will be able to:
➢ Understand, describe and apply the different steps required to design, develop, prototype
and test a device developed for a Capstone Project.
➢ Present using different media, the Capstone Project to different audiences for the:
o Website
o Video Pitch

158 Unit 13: The Capstone Project | CIJE

Principles of Engineering

Section 1 - Brainstorming

Capstone is a project that uses skills that you’ve learned in this course as well as allows you the
opportunity to expand your learning. We will utilize the Design Process for capstone. Check
back with Unit 1 and Unit 3 on the design process and intro to capstone.

During brainstorming, the goal is to identify problems, not solutions (yet). A problem can be
something that is annoying, too expensive, too slow, too fast, wastes time, or can be done at a
higher quality.

Section 2 - Background and Market Research

Students research their chosen problem. Explore variety of sources and
properly cite them. This isn’t the “what do we build” phase, only really
researching the problem. For example – how many people does this
affect? How serious an issue is it? The result of your research may also
lead to discovering not widely known issues that will help define the
criteria and goals for your design.

Background Research

• It is about the problem. How serious is it? How complex is it? Does it interest you?
• To verify if the problem truly exists. There may be existing solutions to address the

problem you were thinking of, and you do not need solve it.
• Be careful not to consider assumptions as facts without checking.

CIJE | Unit 13: The Capstone Project 159

Principles of Engineering

• To motivate yourself to go through the phases of building a capstone project, you would
want to solve a problem that is personally interesting to you. You may find new
enthusiasm for an issue or abandon a problem you no longer want to solve.

Market research

• Do not spend a lot of time on market research, but just having a broad idea of the size,
demographic and psychographic of the people experiencing the problem will help guide
the background research of problems.

• Identify (approximate) the size of population that is affected by the chosen problem or
how much does this group of people spend to deal with the problem.

• For example, if your problem has to do with pet hamsters, you will research how many
pet hamster owners there are. The purpose is to estimate how many people experience
the problem and the potential impact your solution will have for those people. Your
motivation for a problem may change once you know it’s market size.

• Demographic is the statistical data of the person such as age, gender, income,
education, occupation, location, health status, and family structure.

• Psychographic is the personal characteristic of the person such as personality, lifestyle,
interests, opinions, values, habits, and attitudes.

• There are four common types of market research:

o Surveys: create a survey of questions for people to fill out.
o Interviews: Have one-on-one interview with a person and record their answers.
o Focus group: Moderate a planned discussion for a group of people.
o Observations: Observe people as they naturally go about their activity without

talking to them.

• Design questions for a survey or an interview that will give you more insight into your
design space:

o Questions that help determine how significant the problem within your sample
population is.

o How do you determine what demographic/psychographic of the target user(s)
experiencing this problem?

o What products solve all or part of this problem right now? What do users
like/dislike about them?

o Additional areas the users you’re curious about?

Scientific Research

• The research about the problem comes from a scientific source. If the problem is about
a medical condition or environmental issue, then consult a medical or scientific journal
about it. For example, if the problem is overheating in babies and you want to build a
system that protects a baby from getting too hot, then research the science of heat and
what is dangerous for babies.

160 Unit 13: The Capstone Project | CIJE

Principles of Engineering

Technical Research

• If the problem is about a machine such as wind turbines, then look up some technical
documentation about wind (geography, amount), types of turbines

Expert Source

• An expert source can either be an organization or specialist about the problem.
• For example, if the problem is enhancing the life of someone with Alzheimer disease,

look at the website of the Alzheimer Association www.alz.org/ or talk to a neurologist.
This can also be a person you know or a friend of a friend.

MLA Style of citation: Here are some examples of MLA citations for different sources.

https://www.easybib.com/ has a generator to create citations.
• Book:
Wingate, Lisa. Before We Were Yours. Random House, 2017.
• Website:
Sabat, Yaika. “Puerto Rican Writers, Poets, and Essayists.” BookRiot, Riot New Media
Group, 22 Nov. 2017, bookriot.com/puerto-rican-writers/.
• Print journal:
Brundan, Katy. “What We Can Learn From the Philologist in Fiction.” Criticism, vol. 61,
no. 3, summer 2019, pp. 285-310.
• Online newspaper:
Berthiaume, Lee. “Backlog of Applications for Vets’ Benefits Grows By The Thousands.”
Toronto Star, 11 Feb. 2020, A9. PressReader, www-pressreader-
com.i.ezproxy.nypl.org/canada/toronto-star/20200211.
List 4 sources of Scientific or Technical Research for your project:
1)

2)

3)

4)

CIJE | Unit 13: The Capstone Project 161

Principles of Engineering

Section 3 - Empathize (User Research)

The goal of Empathize step is to gain deep insights into the Target User
and form a vivid picture of who you are designing the product/solution
for. Knowing the psychological state, physical attributes and
environment of the target user will inform your design of the
product/solution to be effective and easy to use. You are finding out
the Who, What, Why, Where, When of the problem.

Observe

Describe results from observing target users in the setting where you anticipate they could
need help. This does not involve asking questions but observing their behavior without
interaction or (ideally) knowledge of being observed. Include any photos or insights into their
behavior. Users do not always know exactly what they want so by comparing what they say (in
the interview) versus how they behave (in the observation) will reveal valuable insights. For a
product to be part of the target user’s everyday lives, the designer must think about context in
terms of environment (home, classroom, workplace, outdoor, in the car) the user’s state of
being (multitasking, distracted, relaxed, tired, stressed, rushed), and the sequence of actions
surrounding the problem. Take note of any difficulties or inconveniences the user faces and
how they react to it.

Survey

Ask repeatable questions to a large number of potential users. The basic pattern is:

1. Determine who to survey,

2. Decide on the type of survey (Google forms, Survey Monkey, online or in-person),

3. Design the survey questions to reflect your research goals,

4. Distribute the survey,

5. Collect the responses and store it in an organized and accessible location,

6. Analyze the responses,

7. Write up the results and reflect on how it will influence your design.

Interview

1. Come up with a list of questions that explore the needs of the user without bias of the
problem. In other words, don’t ask “do you have this problem” but rather “what problems do
you have when…”

2. Be an active listener: Your list may not be exhaustive. And you may want to probe deeper
than your list based on their answers. Listen to responses and ask questions to find out more.

3. Often there is an interviewer and a scribe so that one person can focus on taking notes while
the interviewer can focus on the questions and answers

162 Unit 13: The Capstone Project | CIJE

Principles of Engineering

Setting up your Observations/Interviews
Defining the target user:
1. Who are the target users? (Describe)

2. What are the target users’ tasks or goals?

3. What are the target users’ experience level (is the product designed for professionals
who are familiar with the problem or beginners who do not)?

4. Where would the product/solution be used?

5. What are some difficulties or inconveniences that the target user experienced?

6. What are some strategies the target user has for dealing with their problems?

7. When does this problem occur? (daily/monthly/hourly, at night/during the day,etc)

List who you will observe: List who you will interview:
A) A)
B) B)
C) C)

8. Designate a location to record and share your data with your team.

Design your interview questions: CIJE | Unit 13: The Capstone Project 163

Principles of Engineering

Section 4 - Define

Review various problems from the Research phase and select one
problem to solve. Determine why this problem was chosen from your
research. State exactly what it is about this problem that needs to be
solved. Formulate a thesis statement that defines the problem and
state what you hope to solve.

Driving Question

A driving question creates interest and a What is your driving question?
feeling of challenge that helps to initiate and
focus your effort. For example:
How do we prevent blind people from
bumping into obstacles that are positioned
above the waist?
All work for the project should be to answer
the driving question.

Purpose

A project’s purpose explains the reason for What is your purpose for the innovation?
its existence (needs) and what it means to be
done (objectives). Defining the purpose helps
to filter the tasks that serves the purpose
from the tasks that do not and maintain the
focus. For example: The who, what, where,
why and when

Thesis Statement

A thesis statement is a sentence that Write your thesis:
summarizes the problem to be solved based
on your research in relation to the user. Blind
people often bump into obstacles positioned
above the waist undetectable by their cane.

164 Unit 13: The Capstone Project | CIJE

Principles of Engineering

Design Goals

Design goals are targets for design work. They are statements that a team makes about the
quality of experience they would like a product to attain. These statements are used to make
decisions when choosing among design options.

Constraints

Design Constraints Design Constraints:

Limitations on a design such as

• Budget: $75 in addition to supplies from
the classroom.

• Time: How much classroom time do you
have to work on the project until the due
date?

• Technology: What technology do you
have access to? What do you need to
learn to use it?

• Ease of manufacturing

• Product weight

• Product size

Criteria

Design Criteria Design Criteria:

Requirements that a project must achieve to

be successful such as

• Allow the target user to do…

• Ease of use

• Safety of implementing the solution

• Product appearance

• Handicap accessibility

• Portability

• Wearability

• Usability:

CIJE | Unit 13: The Capstone Project 165

Principles of Engineering

Section 5 - Ideate

You’ve researched and defined one problem to focus on, now Ideate
multiple potential solutions for that problem. Students often go straight
to brainstorming “features” of one solution instead of exploring
different possible solutions which limits the imagination and anchors to
premature decisions. Explore multiple potential solutions as well as
features, then thoughtfully narrow down to one that fulfills the design
criteria and constraints.

In the previous phases, a single problem was defined. There are often multiple solutions to a
single problem. Brainstorm as a group different ways to solve the problem and share ideas
with each other. Discuss potential solutions as a group because one approach may sound great
to one member but not the rest. Use different perspectives of each group members to come
up with novel ideas. However, group members cannot read each other minds so it is important
to practice clear communication. Write out every idea as well as draw it so that every group
member can understand and share the same mental image.

Before brainstorming as a group, use this space for writing AND SCKETCHING your potential
solutions. Be as detailed as possible in your drawings:

166 Unit 13: The Capstone Project | CIJE

Principles of Engineering

Solutions and Features

To evaluate your various solutions, the group must develop features, or criteria, that can be
used to compare the various solutions.

Talk about thinking through Brainstorm as a group solutions and features.
solutions and deriving features Remember: Defer judgment or criticism/be respectful,
and benefits from them be inclusive of ideas, consider different solutions not
Example Features of renewable just features of the same solution, embrace out of the
energy solutions: box notions, aim for quantity (of ideas)
• Battery to store energy while

source in unavailable
• Maintenance
• Availability vs. demand
• Installation costs
• Reliability
• Adaptability

Decision Matrix

A decision matrix graphs potential ideas against the design criteria and constraints.
Example: A school wants to generate and use their own renewable energy

Criteria/Constraints Solutions/Features Solar Panels Wind Robot cleaner Modular 3D print
Renewable X X X
X X X
Works 24/7 X X
X X
Snow resistance X
X
Scalable X

Easily Manufacturable

Your team’s problem statement:

Solutions/Features Solution 1

Criteria/Constraints
Criteria 1

CIJE | Unit 13: The Capstone Project 167

Principles of Engineering

Section 6 - Engineering Drawings

Use hand drawings, flow charts, electrical schematics, assembly
drawings and CAD drawings to plan how the design will be wired and
how components will be assembled. Measure and annotate all
dimensions (with units) and attachment locations. Designate and label
components, pin numbers and power source.

System Diagram

Draw system diagram showing inputs and outputs
Example:

Input Process Output
• • •

Wiring Diagram

You can use a website, like
www.TinkerCad.com to create a
wiring diagram like the example
shown here.
Note: the wiring diagram is not
meant to be a physical portrayal
of your project, and
components make look slightly
different than in real life.

168 Unit 13: The Capstone Project | CIJE

Principles of Engineering

Electrical Schematic

An electrical schematic is a map of where the wires connect parts together. You can draw one
by hand or you can use one of the few free online tools:

• Circuitlab (https://www.circuitlab.com/)
• EasyEDA (https://easyeda.com/)
• TinkerCAD (http://tinkerCAD.com/) - separate from their wiring diagram software
Electrical Schematic Example

Assembly or CAD drawing Turbine 150mm

You can sketch how the product goes
together by hand or you can use Tinkercad to
create a 3D model of your project. Be sure to
call out the components and important
features and sizes.

Tip: Save all versions of your project to show
each iteration on your website!

DC motor Arduino Uno
LCD

40mm

75mm

CIJE | Unit 13: The Capstone Project 169

Principles of Engineering

Budget

You will have $75 to spend on your project. To keep a fair playing ground with all the projects in
the Innovation Day competition, please do not accept donations or go above your budget. If you
use the red Arduino Uno from the class kit, or any components from the class kit, they are $0 in
your budget. It’s important to know how much each component costs before purchasing, to
ensure you have enough funding.

Note: Sometimes it takes 30+ days to ship an item. Choose a different company to purchase it
from. It might be a little more expensive, but it will arrive in time.

To find an accurate cost estimate only use reputable websites. Some of these include:

• www.Sparkfun.com

• www.Adafruit.com

• www.mpja.com

• www.amazon.com (check seller)

Bill of materials

Use this space to create your own bill of materials:

Component Quantity Cost Source/Link

Arduino Uno 1 $0 Class kit

170 Unit 13: The Capstone Project | CIJE

Principles of Engineering

Section 7 - Prototype

This step is called
prototyping. Assign each
group member a task and
distribute the workload of
building, coding and
testing, the prototype
according to the strengths
of individual team members. Build a rough
working model of your project for demonstration
and testing purposes. Test for performance,
usability, and durability, as well as to eliminate
problems or glitches.

Initial prototypes are often built “quickly”,

sometimes at smaller scales, with readily available

materials and components to work through issues

of space as well as function. Consider the entire

package: how the pieces will fit together, how

electronics will be housed, where buttons,

switches and other interactions will be placed, Example Build
how it will physically fulfill its design criteria for its

purpose, what sensors and data needs to be considered for the final product.

Suggestion of some • Use an external battery • Instructables.com
building skills: pack to be independent of
laptop Websites to buy:
• Prototype on a breadboard
• Prototype in Tinkercad • Woodworking/CNC/3D print • Adafruit.com
• Balsawood • Sparkfun.com
circuits • Perf Board • Amazon.com
• Design 3D models in CAD • Servocity.com
• Soldering Websites to learn:
• Use a H-Bridge to control Websites for design:
• Adafruit.com
motors • Sparkfun.com • Tinkercad.com
• Use an LCD to display data • Arduino.cc • Circuits.io
• Hackster.io • Onshape.com

CIJE | Unit 13: The Capstone Project 171

Principles of Engineering

Section 8 - Test and Reflect

Once there is a functioning prototype (the features work), test if it
fulfills the purpose of the project. Give your prototype to someone else
to use it and observe them. Record both your observation and feedback
from your Test User. Reflect upon these test data to make
improvements to the prototype. Continue the pattern of testing and
redesigning your product to perfect the product. Consider a plan for
expanding the project into a future enterprise.
Document the process. Include any failures or modifications, and why changes were needed.

1. Pre-test all the features of your product. Design the test for the user. Who will test it?
What directions will you give them (if any)? How will you know if all the features work?
What questions will you ask the tester/user?

2. Give your functioning prototype to a few people to operate and take note of their
feedback:

3. Reflect upon your observation and feedback to improve the prototype. Write your
reflection and new ideas for improvement here:

4. Outline a plan for expanding your project into a business. Estimate how much it would
cost to operate your business (cost of supplies + cost of labor, etc.). Survey target
customers to discover how much they would pay for your product. Take notes of all your
business ideas here:

172 Unit 13: The Capstone Project | CIJE

Principles of Engineering

Section 9 - Website

The website is the place to share all about your project. Present a short
summary about the project and introduce every team member (may
include their photos). Design the website to have an appealing aesthetic
(colors, pictures, and graphics) as well as informative about the goal of
the project. The website needs to have a video that clearly demonstrate
the project working.

1. Pick a website platform (Google sites, Wix, Weebly .etc):

2. What is the one or two most important things you want someone visiting your website to
know?

3. Design an original logo for your project:

4. Write one or two sentences for each team members outlining what tasks they are
responsible for:
_________________________________________________________________________
_________________________________________________________________________
_________________________________________________________________________

CIJE | Unit 13: The Capstone Project 173

Principles of Engineering

Sample outline for a website:

1. Header
1.1. Title, Logo, Video:

2. Description
2.1. Pitch/Thesis:
2.2. Worksheet for a pitch
2.3. Features and benefits.

3. Abstract (The Problem and the solution) 250 words or less
3.1. Summarize the project scope

4. Background
4.1. The need/ the problem
4.2. Interview / survey results
4.3. Who, What, Where, Why, When

5. Market (research)
5.1. Competitors
5.2. Existing products

6. Design and Methodology
6.1. Construction
6.2. System flow / Operation chart of how the prototype function
6.3. Schematic, Picture of circuits, hand drawn diagram
6.4. Coding, Arduino Code and flowchart
6.5. Bill of materials / Costs
6.6. Prototype photos / CAD rendering

7. Discussion
7.1. Testing Process / Procedure
7.2. Reflections on User feedback
7.3. Future work / improvements

8. The Team
8.1. Names
8.2. Roles
8.3. Bios

9. Acknowledgement
9.1. Outside resources/people who helped with or inspired the project to
ensure the desired result and objective is met.

174 Unit 13: The Capstone Project | CIJE

Principles of Engineering

Section 10 - Video

The video pitch is a (1-3 minute) visual description of your project that explains
the concept, its benefits and features, in the context of the user with the intent
to persuade of it’s need. Video is a visual and audio medium that can be used
to make a logical, credible and emotional appeal through fact sharing,
storytelling and reenactment.

Pitch Elements:

• Hook the viewer - within the first 30 seconds:
o Grab the viewer’s attention,
o Introduce your product clearly
o Show how it will benefit the user

• Keep it short and sweet
o Presentations with the greatest engagement are between 1 minutes and 3
minutes.

• Focus on benefits + Support with features
o A feature is a technical part of a product. The benefit is how the product makes
our lives better (what problem is it solving?)

• Be real and personal,
o Tell a Story
o Speak directly to the viewer
o Be honest and real
o Bring energy and show your passion!
o If you believe in it, they’ll believe in it!

• Make sure you can be heard
o Part of this is projecting your voice (loud
without shouting) and part of this is technical
(making sure the microphone is in the right
place

• Appeal with
o Logos (logic)
o Ethos (credibility)
o Pathos (emotion)

CIJE | Unit 13: The Capstone Project 175

Principles of Engineering

Plan your pitch

1. Product name:

2. Problem addressed (numbers affected, if possible, why it is important to you, why
it was chosen: What’s the Story of the problem?

3. Explain how it was solved (your solution):
4. Benefits:

5. Features:
6. Who is it for?

7. What evidence do you have?

8. What are your possible next steps to further develop the product?

9. Demonstrate a prototype or model solving the problem

10. Next steps in further development of the project
176 Unit 13: The Capstone Project | CIJE

Principles of Engineering

Storyboard Worksheet

Plan your video: with words and drawing, sketch out who will talk, what will be said and
when/what order

Example: Bob stands under tree
pointing to his cat:
“9 out of 10
cats get stuck in trees…”

CIJE | Unit 13: The Capstone Project 177

Principles of Engineering

Video Making Process

1. Practice
• Take time to practice a few times (either on or off
camera)
• Make adjustments of timing and wording

2. Record the video based on your story line
• Use a phone or dedicated digital camera but be
careful of the distance with the microphone
• Use proper lighting: either natural light (outdoor,
clouds and shade gives even light), near big
windows

3. Transfer/upload the file(s)
• Computer (laptop, desktop)
• cloud (iCloud, Google Drive, Chromebook)

4. Edit and process
• Based on your storyboard, edit your pieces of recordings
into one fluid, easy to follow storyline/pitch
• Licensed software: Adobe Premiere, Adobe Spark, iMovie
• Free online: WeMovie, PowerDirector
• Process the final version into an mp4 or mov file

5. Upload / Get shareable link
• Upload the file to a cloud location if it isn’t
already on one
• Get a shareable link that anyone with the link can see

178 Unit 13: The Capstone Project | CIJE

Unit 14: Flow
Control

Principles of Engineering

Preface

This section will go through the different methods of allowing a code to make decisions. It will
explore the if, while, for and switch statements.
The unit will also discuss analogWrite, Data types and various Arduino functions in more details
than already discussed.

Unit Sections

➢ Flow Control
➢ Making Decisions – if/else Statements
➢ The while Statement
➢ The for Statement
➢ The switch Statement
➢ Lab: Flow Control

Learning Objectives

Following this unit students will be able to:
➢ Explain the need for if/else statements
➢ Describe the basic logical comparison operators
➢ Apply syntax of if/else statements

Introduction - Pseudo Code

Pseudo code is writing down what you want to have happen in the system in plain English and
then translating it to code. This is where you get to tell the story. For example: When the user
presses button A, the green light turns on, but when the user presses button B, a buzzer makes
a loud sound. This “story” or pseudo code can be translated into if statements and other code.

180 Unit 14: Flow Control | CIJE

Principles of Engineering

Flow Control

Flow control is used to allow the processor to make decisions about doing something (running
some lines of code) or not doing something (not doing the code) based on the values of variables.
There are four primary flow control commands that you can use in the Arduino:

1. If statement – Used to do something one time if the comparison is true, and skips the
actions if the comparison is not true.

2. While statement – Used when you want to keep doing something as long as a condition
is true. The while statement stops doing the code block when the condition is not true.

3. For statement – Used when you want to do something a certain number of times. Usually
the for statement use a counter variable.

4. Switch statement – Used when you want to be able to select one of several possible things
that the program might do. The switch statement has several alternatives, one of which
will be executed.

1. Making Decisions – if/else Statements

In previous units, we learned how to turn on and off (or toggle) an LED. After that, we learned
how to make a reading using an analog sensor. But what if we had to use the data from the sensor
to determine whether to turn a light on? Let's use an everyday example of this: street lights
turning on at dusk. A photosensor in the street light detects when there is not enough light and
the electronic circuitry in the light triggers the lamp.

Let's break down the process:

1. A light sensor makes a reading throughout the 24-hour period.
2. The circuitry determines whether the reading indicates it is dark or light outside.
3. If it is dark, it turns on the lamp. If it is light, it turns the lamp off.

The first step seems fairly clear. We must make measurements with our sensor to have data to
make a decision.

The next step is to use that data to decide. To do this we must set a boundary, a threshold, to
categorize our data. In the street lamp example, we have two categories for our data, light and
dark. But now we need to decide what readings we are going to call dark and what we are going
to call light. Let's say that one hour before sunset, we get a reading of 453 from our sensor, and
that one hour after sunset we get a reading of 125. Then we need to set our threshold somewhere
between 125 and 453. Again, the key point is the we, the designers, decide on a suitable
threshold. We choose the thresholds that give us the behavior we need.

The third and final step is that, once we determine whether our data is above or below a certain
threshold, we need to tell the system what to do for each case. For the street light, if our sensor
indicates that it is dark, then we must turn the lamp on. Notice that we use the word if to talk
about making decision. Once again, we are making decisions about the behavior based on our
design of the system.

In computer programming we determine behaviors based on specific conditions with the 'if',
'if-else', and 'if-elseif-else' structures. This is one of the fundamental structures of programming,
and if you want to write most anything, you must understand 'if' statements.

CIJE | Unit 14: Flow Control 181

Principles of Engineering

Tip reminder - A block of code is those lines of code that are between braces {}

There are three main variations of if-statements:

➢ if

if (logical comparison)
{
//do code within braces if true, or else skip
}

➢ if – else

if (logical comparison)
{
//do code within braces if logical comparison is true

else }

{
// do code within braces if logical comparison is false

}

➢ if – else if – else

if (logical comparison 1)
{
// do code within braces if logical comparison 1 is true
// then skip the next two statements
}

else if (logical comparison 2)
{
// do code within braces if logical comparison 2 is true
// then skip the next statement
}

else
{
// execute code if both logical comparisons are false
}

Logical Comparison

But what is a logical comparison? It compares two things and has only two possible answers, true
or false (yes or no).

Logical Comparison Equivalent Question
A>B Is A greater than B?
A<B Is A less then B?
A == B Is A equal to B?
A >= B Is A greater than or equal to B?
A <= B Is A less than or equal to B?

182 Unit 14: Flow Control | CIJE

Principles of Engineering

Formally, the operators used in these comparisons (the greater-than symbol, the less-than
symbol, etc.) are referred to as relational operators.

Let's look at some examples of using these relational operators. Here we have a variable named
x that has a given value. The comparison is typically how we would write it in code. The meaning
is just the value of the variable substituted in x.

value of x comparison meaning true or false
3 (x < 4) ((3) < 4) true
7 (x > 7) ((7) > 7) false
10 ((10) == 10) true
(x == 10)

It is extremely important to notice the equals-to comparison is two equals signs (==) NOT one (=).
Recall from the discussion on variables that the single equals sign (=) is the assignment operator.

Programming an Automatic Street Light:

Now we are ready to program our street light to turn on when it is dark. As we outlined at the
beginning of the section, the first thing we should do is get a reading from our LDR. We already
know how to code this! We did this in the previous unit.

Next, we need to use an if-statement to turn on the light. Recall from above, when we tested our
actual circuit, we saw we needed to choose between a value of 125 and 453 in our specific case.
The designer of this light chooses to trigger the light when the reading is below 400. So, 400 is
our threshold to change the behavior of the light. A code to do this is:

CIJE | Unit 14: Flow Control 183

Principles of Engineering

There are several changes from the LDR code from the previous section. First, since we are
turning on an LED, we have to create a pin variable and set the pinMode() to OUTPUT. We also
commented out the serial monitor code since once we have determined the value we need for
the if-statement, we don't need to see that information on the monitor.

The 'if' statement is:

if(readValue < 400)
{

digitalWrite(ledPin, HIGH); // turn on LED if comparison is true
}

At the top of loop(), the Arduino takes a measurement of the pin connected to the LDR (the
analogRead() command) and stores it in the variable readValue. Let's say that a measurement
taken in the middle of the night with analogRead() gives a value of 56. Then the computer sees:

if (56 < 400)
{

digitalWrite(ledPin, HIGH);
}

Since (56 < 400) is a true statement, the LED will turn on.

Now suppose that it is the middle of the day, and the measurement from the LDR with
analogRead() gives a value of 751. Then the computer sees:

if (751 < 400)
{

digitalWrite(ledPin, HIGH);
}

Since (751 < 400) is a false statement, the computer will skip to the bottom of the if-statement.
Since the if-statement is the last line of loop(), the code will go back up to the top of loop() and
do another analogRead().

We have a light that turns on when it gets dark! It appears we are done, but there is still one
more step.

When it gets dark, our light turns on, just like we need it to. But think ahead to sunrise. What
happens to the light in the morning? Notice that when it is bright outside, we are just skipping
the turn-on. So, after the light turns on once, it will never turn off again.

We also need to include a way to turn the light off when it is bright outside. We could include
another if-statement to do this; however, there is a better way—use the if-else construction.

In this specific case, we either want the light on or off. In our case, this would be:

if (valueRead< 400)
{

digitalWrite(ledPin, HIGH);
}
else
{

digitalWrite(ledPin, LOW);
}

184 Unit 14: Flow Control | CIJE

Principles of Engineering

In cases where we want either-or type of behavior (either the light is on or the light is off), the if-
else construction is usually the best option.
It is important to understand how big of an accomplishment what we have just done is. We have
used sensors to collect data, and then we have used the data to make a decision. In a very real
sense, this is the essence of engineering!

Multiple Requirements: AND and OR

There are cases where we need to test more than one condition to get the correct behavior. One
case would be an excluded middle, where we want to only do something at either end of a sensor
range. For instance, if we wanted to have an alarm that went off if a pressure was either too high
or too low. In this case, we could use an OR statement. In an OR statement, if just one of the
conditions is true, then the whole statement is true. In the Arduino language, two "||" means
“OR". So:

if((sensorValue < 1) || (sensorValue > 10))
{

digitalWrite(4,HIGH); //turn on a buzzer on pin 4
}

would sound an alarm if the pressure was less than 1 or greater than 10 (in whatever units your
sensor was measuring).
Similarly to the "or" statement is the "and" statement, "&&". When used in a comparison, it
means “and” both cases must be true for the whole statement to be true. For instance, you want
a personal desk fan that turns on only if the temperature is above 75 AND the weight sensor in
the seat is activated (i.e. someone is sitting in the chair):

if((weightValue > 5) && (tempValue < 75))
{

//turn on fan
}

With either of these constructions, you may add as many conditions as you need.

CIJE | Unit 14: Flow Control 185

Principles of Engineering

Lab: if/else Statements

1. Build the circuits shown below

2. Upload the following code to your Arduino board

The number inserted here is often referred to
as the threshold number. This will be different
for each person’s circuit.
It should be a number that is somewhere
between the “dark” and “light” readings from
you photoresistor.
To find the threshold number most appropriate
for your project open the Arduino serial
monitor and see your sensor’s readings.

The if – else statement checks a
logically expression.
If true it performs what is in the first
bracket.
Or else is automatically performs
what is in the second set of brackets.
3. The above code may or may not work. Using the serial monitor to assist you, select an
appropriate threshold value that turns the light on and off as you cover and uncover it.
4. Edit the above code to blink the LED when the sensor is covered.
5. Edit the code to blink the LED when the sensor is uncovered.
6. Edit the code so that the LED turns on when it is extremely bright or extremely dark,
but remains off under normal room lighting. Hint: you can add another if statement.

186 Unit 14: Flow Control | CIJE

Principles of Engineering

7. What threshold number, and why, might you choose if you wanted the light to turn on
immediately as soon as the sun began to set (such as in a baseball stadium)?

________________________________________________________________________

8. What threshold number, and why, might you choose when designing an automatic
outdoor porch light so that the light doesn't flicker with each passing cloud?

________________________________________________________________________

9. In the example code above, we setup our if statement as if(analogRead(A0) > 500).
Is there a difference if we set it up as follows:

int information = analogRead(A0);
if(information > 500)

________________________________________________________________________

10. Below are two different versions of our example code. Suppose Arduino returned exactly
500 from the analogRead pin.

if (analogRead(A0) > 500) if (analogRead(A0) > 500)
{ {
digitalWrite(13, HIGH); digitalWrite(13, HIGH);
} }

if (analogRead(A0) < 500) else
{ {
digitalWrite(13, LOW); digitalWrite(13, LOW);
} }

What would the light do according to the code on the left? And the code on the right?

________________________________________________________________________

11. If the code doesn’t instruct the light to turn on or off, what will the state of the light be?

________________________________________________________________________

12. Using the code to the right, if an LED was int x = 7;
connected to pin 13, what would it do?
void setup() {
_______________________________________ pinMode(13, OUTPUT);

13. Using the sample code below, what will the serial }
monitor print?
void loop() {
_______________________________________ if (x == 7)
{
digitalWrite(13, HIGH);
delay(1000);
x = 8;
}
else
{
digitalWrite(13, LOW);
delay(1000);
x = 7;
}

}

CIJE | Unit 14: Flow Control 187

Principles of Engineering

14. Suppose we have two lights, a green light in pin 13, and a red light in pin 12. We want
the code to operate so that if the analogRead value is above 400 the red light turns on,
and if above 600 the green lights turns on. Below 400 all lights should be off.

Using the following code, if the analogRead value is 800, which light(s) will turn on?

Red, Green or Both? _________________

void setup() { Desired Outcome
pinMode(12, OUTPUT);
pinMode(13, OUTPUT); Above 600
•Green light on D13
} Above 400
void loop() { •Red light on D12
0 - 400
if (analogRead(A0) > 600) •No light
{

digitalWrite(13, HIGH);
}

if analogRead(A0) > 400)
{

digitalWrite(12, HIGH);
}

else
{

digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
}

15. If we want only one light to go on at a time we can setup multiple parameters in our if
statement using two ampere symbols. For example:

if ( (analogRead(A0)<10) && (analogRead(A0)>5) )

Edit the following code so that only specific lights illuminate, as specified in the previous
question above.

void setup() {
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);

}

void loop() {
if (______________________________________________)
{
digitalWrite(13, HIGH);
}
if (______________________________________________)
{
digitalWrite(12, HIGH);
}
else
{
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}

}

188 Unit 14: Flow Control | CIJE

Principles of Engineering

16. Lastly, suppose our analogRead value goes above 600, turning on the green light, and
then drops below, turning on the red light. What will happen to the green light?

________________________________________________________________________

17. Edit the code from question 12 to fix the problem encountered in the previous question.

18. Using a thermistor, wired the same as the LDR, design a
temperature warning system using three LEDs; a green for low
temperature, a yellow for medium temperatures, and a red for
high temperatures.

Since there are three different behaviors, an 'if' and 'else' Thermistor
alone will not be sufficient.

Hint: you can try to use an 'if' - 'else if' - 'else' with different thresholds.

19. Suppose x = 10 and y = 12. What will be the result of the comparison (TRUE or FALSE)?

(x > 10 || y > 10) __________________

(x < 12 || y < 12) __________________

(x < 10 && y < 10) __________________

(x > 8 && y < 12) || (x <12 && y > 10) __________________

20. Explain why the following 'if' statement boolean is illogical (i.e. – useless).

if(x > 5 || x < 10) ____________________________________________

if(x > 10 && x < 5) ____________________________________________

21. Challenge: Given the following void loop. If the code initialized with int a = 5; what
would the serial monitor print on the screen?

➢ a++ is a shortcut or adding 1 to
the variable ‘a’. Same as a=a+1;

➢ a==10 checks ‘if a is equal to 10’

➢ a=10 sets ‘a’ equal to 10

CIJE | Unit 14: Flow Control 189

Principles of Engineering

2. The while Statement

Now let us consider the while statement. The while statement keeps repeating the code inside
the block (between the braces) until the comparison fails (becomes false). In this example, we
use analogRead(A3). A3 is an analog input pin and reading it will give a value between 0 and
1023 correlating and proportional to a voltage between 0 and 5 volts.

Consider the example:

Snsr_Vlu = analogRead(A3);
while (Snsr_Vlu < 428)
{

digitalWrite (13, HIGH);
delay (300);
digitalWrite (13, LOW);
delay (300);
Snsr_Vlu = analogRead(A3);
}

Note, here our block of code is 5 lines. A block can be very short or very long. If the comparison
is TRUE, the processor does the entire block, then checks the condition again.

Perhaps the red LED connected to pin 13 is a warning light. If the sensor value gets too low, it
will start flashing. If the program reads pin A3 before the while instruction, and gets a value of
400, the comparison value will be TRUE. When the comparison results in a TRUE, it executes the
code between the braces, turning the light on and then off. Then it will check the sensor again
and maybe read a new value to Snsr_Vlu.

Unlike the if statement, when the while statement completes the block, it goes back and again
does the comparison. If the condition is still TRUE, it will repeat the code and continue checking
and repeating until the condition is FALSE.

Another way to do this same program is as follows:

while (analogRead(A3) < 428)
{

digitalWrite (13, HIGH);
delay (300);
digitalWrite (13, LOW);
delay (300);
}

In this case, the program has to execute the analogRead(A3) to get a number to be compared to
428. "analogRead(A3)" is a subroutine “call.” This call returns a digital value between 0 and
1023. This result is compared to 428 and it is either greater or it isn’t.

Challenge: how might we use the while statement to keep repeating code inside the while loop
forever? Think about how this is similar to what void loop() does.

190 Unit 14: Flow Control | CIJE

Principles of Engineering

3. The for Statement

Unlike in while loops, which will execute for as many times as the conditional statement is true,
often we need a certain block of code to be executed a fixed number of times. The for loop is
particularly good at this. There are three things that go between the parentheses in a for
statement. They are separated by “;”

for (int cntr = 0; cntr < 6; cntr = cntr +1)
{

//Block code goes here
}

Can you see the three sections of the for loop in the parentheses? The first section defines the
variable cntr and sets it to 0. Though you can start from any number, we almost always will begin
at 0 (zero). Remember that we count from 0 not 1 in Arduino programming. There is nothing
special about the variable name cntr. The counter variable can be named anything as long as it
starts with a letter and does not contain any spaces. Often just the letter i or j is used.
The next section is a comparison like the one in the if and while statement. If the result of the
comparison is TRUE, then the block of code is executed. If FALSE, then the entire block is skipped.
The last section is the increment section. In this case, we will increase cntr by 1. When the
program encounters the 'for' statement, it first creates a variable in this case, an integer with the
name cntr and sets it equal to 0. Next, it will compare the variable cntr to see if it is less than 6.
If it is, it will execute the block of code.
After it has executed the block of code, it will come back, and add 1 to cntr and again do the
comparison. Now cntr will be 1, but still less than 6. Since again the comparison was TRUE, it will
execute the block again.
Can you tell how many times the block of code will be executed? _________________________

For Loop Process
1. A variable in initialized

at a specific value
2. The code checks if the

test statement is true
3. If true, the code in the

{ } is executed
4. The increment portion

of the code is run
5. The code returns to

the test statement to
see if it’s true

CIJE | Unit 14: Flow Control 191

Principles of Engineering

4. The switch Statement

Last is the switch statement. This flow control statement is most often used when we want to
decide between a (relatively) small list of options. Suppose we have four LEDs. These lights are
numbered 1 (a red one), 2 (a blue one), 3 (a green one), and 4 (a yellow one).

An input from a sensor is going to return one of these four numbers and we want to turn on the
appropriate light and turn off all the others.

switch (snsr)
{
case 1:

digitalWrite (red, HIGH);
break;
case 2:
digitalWrite (blue, HIGH);
break;
case 3:
digitalWrite (green, HIGH);
break;
case 4:
digitalWrite (yellow, HIGH);
break;
}

In the switch statement, the variable
name snsr is a 1, 2, 3, or 4. Depending
on its value, we determine which of the
case statements will execute. If for
example snsr = 3, then the switch
statement will jump down to case 3:
The first line in case 3 is a call
statement to the subroutine
digitalWrite (). It will then turn the
green LED on. Finally, the break
statement will cause the program to
jump to the closing brace of the switch
statement. The switch statement does
not repeat. The program moves on.

You many have noticed that If/else
statements can accomplish these tasks
as well. When uses appropriately,
switches have a few advantages over
if/else-statements. However, in this
text we will emphasize if/else over
switch as it more general and more
widely used in the Arduino community.

192 Unit 14: Flow Control | CIJE

Principles of Engineering

Lab: Flow Control

1. Suppose you wanted to write a program that would make the red LED on the board

blink 14 times and then stop. What kind of flow control would you use? ____________
2. Suppose you connected an external switch to the Arduino board. When you hold down

the switch you put a HIGH value on pin 12 and you want the LED to be on. When you
release the switch, you get a LOW on pin 12. What kind of flow control would be

appropriate? _________________
3. Suppose you have a sensor that measures voltage connected to Arduino analog pin A2.

The voltage goes up and down. Complete the following code so that an LED connected to
pin 13 to turn on when the voltage reading is above 512 and off when it is 512 or below.

if(analogRead(___) _____)
{
digitalWrite(_____, ________);
}

else
{
digitalWrite(_____, ________);
}

4. Complete the following lines of code so that the program will run three, 3-second delays

i.e. - delay(3000); three times.

for (int c = ___; c < ____; c ____)
{
delay(_________);
}

while Loop

5. For each of the following while loops, predict what will be the output:

int i = 1; int i = 1;
while (i < 20) while (i < 5)
{ {

Serial.println(i); i = i + 1;
i = i * 2; Serial.println(i);
} }

__________________ __________________

6. Write a program with a while loop that counts from 1 to 10 and prints each number on a
separate line.

7. Write a program with a while loop that counts down from 1000 by dividing by 2 (i.e. –
100, 50, 25…) and stopping before it is less than 1. Print each number on the same line
going across the page, and go to the next line at the end. (What happens if you use int's
vs. float's?)

CIJE | Unit 14: Flow Control 193

Principles of Engineering

for Loop

8. For each of the following for loops, predict what will be the output:

a) for (int i = 0; i < 10; i++) e) for (int i = 0; i < 10; i--)
{ {
Serial.println(i); Serial.println(i);

} }

_____________________ _____________________

b) for (int i = 1; i < 10; i+= i) f) for (int i = 0; i > 10; i++)
{ {
Serial.println(i); Serial.println(i);
} }

_____________________ _____________________

c) for (int i = 1; i < 10; i*= 2) g) for (int i = 0; i == 10; i++)
{ {
Serial.println(i); Serial.println(i);
} }

_____________________ _____________________

d) for (int i = 0; i < 10; i*= 2) h) for (int i = 0; i = 10; i++)
{ {
Serial.println(i); Serial.println(i);
} }

_____________________ _____________________

9. Fill in the code below that will cause the LED to blink 12 times, on for one second and off
for two second?

for (int c = 0; c _______; c______)
{

digitalWrite (13, HIGH);

________(1000);

digitalWrite (13,______);

delay(______);
}

10. The following code segment never prints anything.

if (x < 5 && x > 19)
{

Serial.println(x+1);
}

Why not? ______________________________________________________________

194 Unit 14: Flow Control | CIJE

Principles of Engineering

11. Write a program that counts down from 10 to 0, prints "blastoff!" and then stops after
doing it one time.
Note: You can use an empty while loop to stop a code from continuing with the
'void loop'

12. Write a program, using a for loop, to print out n factorial (written n! in algebra)
I.e. – 5! = 5 x 4 x 3 x 2 x 1 = 120
Note: int x = 5! will not work in Arduino coding

13. Write a program to compute

Store the sum in a variable and print it out.
Note: In order to print more than the default two digits, use the second parameter of the
print function, as shown below:

Serial.println(x, 7); // will print 7 decimals

14. Challenge: Write a program to:
a. Sum the numbers from 1 to 50,000 using a for loop.
b. There is an equation for the sum of numbers from 1 to x. Write a program using
the equation to sum the numbers from 1 to 50,000.
c. Time the above program to see how long it takes. You can do this by storing the
time the computation beings and ends, using the micros() function, and
subtracting the two.
y = micros();
Serial.println(y);
this prints the time since the program started
d. What was the difference in time it took for the Arduino to compute the
answer using the two different methods? _________________________
e. Why do you think one method took longer than the other?
__________________________________________________________________
__________________________________________________________________

CIJE | Unit 14: Flow Control 195

Principles of Engineering

Lab: Timing

Background

Arduino has two functions that can be useful when recording timing of events in necessary.
millis() and micros() both return the amount of time (in milli or microseconds, respectively)
since the Arduino board began running the current program. This number will overflow (go back
to zero) after approximately 50 days for millis() and about 70 minutes for micros().

Activity

1. Use the serial monitor to print how many void setup()
milliseconds it has been since the Arduino {
turned on.
Serial.begin(9600);
}
void loop()
{

Serial.print("Time: ");
Serial.println(millis());
}

2. Store the time in a variable using the following format. Then print your variable. Make
sure you select the correct variable type.

Hint: variable = millis();

Data Type Range
char -128 to 127
int -32,768 to 32,767
long -2,147,483,648 to 2,147,483,648

3. Before printing your variable from the previous question, convert the variable from
milliseconds into seconds. Then print the number of seconds since the Arduino has
turned on on the serial monitor.

4. Create a countdown timer using millis() that counts downwards from 10 seconds.
Hint: variable = 10000 – millis();

5. Difficult: Create a blink a code to turn an LED on and off without using the delay function.

A powerful use of Arduino timing functions is to use them to create “non-blocking delays”.
This allows us to control what the Arduino is doing for a certain amount of time without
using the delay function, which freezes (or “blocks”) our code.

Example: long old_time = millis(); The code begins by
recording the time.
while(millis() – old_time < 10000) The while loop then
{ runs as long as it has
not been 10 seconds
//do something here since the time was
recorded
}

196 Unit 14: Flow Control | CIJE

Principles of Engineering

Lab: Photogate
Objectives

Build a photogate, using a breadboard, that will measure the time it takes to slide a credit
card down the center of the board, and display it in miles per hour (mph) on the screen.

Procedure

Photogates are sensors that sense when something has bisected the sensing region of a
“gate”. The “gate” is a plane set up by the sensor that the user wishes to detect. For example,
in a race the “gate” would be the finish line. So, when a runner crosses the finish line, the
sensor must detect it. In this lab, the “gate” will consist of an LED directly opposite a
photoresistor. This setup will cause the photoresistor to have a very low resistance when
unobstructed. When an object (i.e. the credit card) passes between the LED and the
photoresistor (i.e. – it crosses the gate) the resistance of the photoresistor will go up and the
Arduino will sense this as “something has crossed the line”. By recording the time the “Start”
and “End” gates are crossed we can determine how long it took to slide the card across the
breadboard. We can then compute the speed in miles per hour.

1. Erect a basic photogate circuit by placing a photoresistor and LED facing each other
on both ends of the breadboard. You will need to connect the LED and photosensor
to ground and power like in the schematic on the right. The photo on left is not wired.

2. Construct a voltage divider with a resistor and the photoresistor on your breadboard
(see diagram above). The resistor in the lower half of the voltage divider should be
somewhere in the range of the resistance of the photoresistor. You can determine
this range by connecting the photocell to the multi-meter and measuring its
resistance. Make sure the connecting wires still allow the card to pass.

CIJE | Unit 14: Flow Control 197

Principles of Engineering

3. Use the example code under File > Basics > AnalogReadSerial (or use your code from
previous sections) to display the values read into the Arduino by the voltage divider.
Can you see a difference when you block the light with your finger? Pick a threshold
value that will be used to dictate if the “gate” is being blocked or is unobstructed.
Notice that this system requires very similar behavior to the street light that triggers
automatically at night (Section 1). This means the code we used in that section is an
excellent starting point here (and vice-versa).

4. Look up the micros() function on the Arduino website, and store the time as soon as
the first gate is blocked. Store the time the second gate is blocked and subtract the
start and end times to find the total time traveled.

HINT: A code like this could be used to record the start time when the first
photogate is crossed

while (analogRead(A0) > 500) // >500 when the gate is NOT blocked
{

; //the program will wait here until the gate is blocked
}
long starttime = micros(); // Arduino will record the start time

5. Print the speed the card was traveling in miles per hour on the screen.

To convert the readings into miles per hour we need to calculate how fast the card
was moving. We know how long it took to go from one photogate to the other, in
milliseconds, and we know how far it traveled by measuring the distance between
the gates in inches. Dividing the distance by the time we can compute the speed in
inches per millisecond (speed). To convert from inches per millisecond (speed) into
miles per hour (also a speed), we need to find the conversion factor.

Note: 1 inches per millisecond (in/ms) = 58.82 miles per hour (mi/hr)

Note: because the calculations involve decimals, all variables must be of type
float

6. An LED-photoresistor photogate is limited by the photoresistors ability to distinguish
between direct light and the surrounding ambient light. This leads to a very narrow
passage area.

What other materials could be used to create a photogate if you wanted to time an

a. Olympic skier? __________________________________________________

b. speeding arrow? _______________________________________________

c. A passing airplane? _____________________________________________

198 Unit 14: Flow Control | CIJE

Unit 15:
Buttons,
Transistors &
analogWrite

Principles of Engineering

Preface

This section introduces students to transistors, buttons and the analogWrite command.

Unit Sections

➢ Analog Write
➢ Transistors
➢ Buttons and Digital Read

Learning Objectives

Following this unit students should be able to:
➢ Understand how to implement the analogWrite command to control the voltage to an
electrical component.
➢ Describe why a transistor would be used and how it functions.
➢ Explain how to interface a button or switch with the Arduino
➢ Incorporate a button or switch into an Arduino code

200 Unit 15: Buttons, Transistors & analogWrite | CIJE


Click to View FlipBook Version