Vous êtes sur la page 1sur 5

Efan Altaee

Chapter 5 Challenges
Questions
1.What kind of electrical connection is a
whisker?
> open, momentary, single-pole, single-throw
tactile switch.
2.When a whisker is pressed, what voltage
occurs at the I/O pin monitoring it? What
binary value will the digitalRead function
return? If digital pin 8 is used to monitor the
whisker circuit, what value
does digitalRead return when a whisker is
pressed, and what value does it return when a
whisker is not pressed?
> Zero (0) volts, resulting in binary zero (0)
returned by digitalRead.
digitalRead(8) == 0 when whisker is pressed.
digitalRead(8) == 1 when whisker is not pressed.
3.If digitalRead(7)== 1, what does that
mean? What does it mean
if digitalRead(7)== 0? How
about digitalRead(5)==
1 and digitalRead(5)== 0?
> digitalRead(7)== 1 means the right whisker is
not pressed.
digitalRead(7)== 0 means the right whisker is
pressed.
digitalRead(5)== 1 means the left whisker is not
pressed.
digitalRead(5)== 0 means the left whisker is
pressed.
4.What statements did this chapter use to
call different navigation functions based on
whisker states?
> chapter used if, ifelse, and ifelse ifelse
statements to evaluate whisker conditions and call
navigation functions.
5.What is the purpose of having
nested if statements?
> If one condition turns out to be true, the code
might need to evaluate another condition with a
nested if statement.
Exercises
1.Write a routine that uses a single variable
named whiskers to track whisker contacts. It
should store a 3 when no whiskers are
contacted, a 2 if the right whisker is contacted,
a 1 if the left whisker is contacted, or 0 if both
whiskers are contacted. Hint: multiply the
result by two.
> Since digitalRead returns 1 or 0, your code can
multiply digitalRead(5) by 2 and store the result in
the whiskers variable. It can then add the result of
digitalRead(7) to the whiskers variable and the
result will be 3 for no whiskers.
2.Modify the loop function in Roaming With
Whiskers so that it makes the BOE Shield-Bot
stop and not restart when both whiskers
contact at the same time.
> In the if((Left == 0) && (wRight == 0)) block,
remove the backward and turn Left function and
replace them with calls to servoLift.detach and
servoRight.detach.
3.Add a function named pause to Roaming
With Whiskers. It should make the BOE Shield-
Bot stay still for a certain amount of time.
> void pause(int time) // Pause drive
wheels
{
servoLeft.writeMicroseconds(1500); // Left
wheel stay still
servoRight.writeMicroseconds(1500); // Right
wheel stay still
delay(time); // Maneuver for time
ms
}
4.Modify the loop function so that the BOE
Shield-Bot stays still for 0.5 seconds before
backing up and turning.
> Make sure not to call this pause in the else
condition because the forward function is only
supposed to go forward for 20 ms before checking
the whiskers again.
Projects
1.Modify Roaming With Whiskers so that the
BOE Shield-Bot stops and makes a 4 kHz beep
that lasts 100 ms before executing its usual
evasive maneuver. Make it beep twice if both
whisker contacts are detected during the same
sample. HINT: Use the pause function you
developed in the Exercises section to make it
pause immediately after the tone starts
playing. Also, a 0.2 second pause after the
tone call separates the 0.1 second tone from
servo motion, or allows you to hear a second
tone.
> The key to solving this problem is to write a
statement that makes a beep with the required
parameters. As soon as the beep starts, call the
pause function to keep the BOE Shield-Bot still
while it beeps. Make sure not to add any pause
calls to the else statements code block. It needs
to repeatedly go forward for 20 ms, without any
pauses
2.Modify Roaming With Whiskers so that the
BOE Shield-Bot roams in a 1 yard (or 1 meter)
diameter circle. When you touch one whisker,
it will cause the BOE Shield-Bot to travel in a
tighter circle (smaller diameter). When you
touch the other whisker, it will cause the BOE
Shield-Bot to navigate in a wider diameter
circle.
> Start with the Circle sketch from Chapter 4
Solutions. Comment the detach calls and move the
circle code to the loop function and reduce the
delay to 50 ms so that it can check the whiskers for
contacts 20 times per second. Then, add the
whisker monitoring code with an if statement that
reduces or increases a variable that slows the right
wheel when the right whisker is pressed, or speeds
up the right wheel if the left whisker is pressed.

Vous aimerez peut-être aussi