You are on page 1of 10

Chapter 7 Challenges

Questions
1. What is the frequency of the signal
sent by tone(9, 38000, 8)? How long does
this call send the signal for? What I/O pin
does the IR LED circuit have to be connected
to in order to broadcast this signal?

38 kHz is the frequency of the signal. Although the 8 in


tone calls ms parameter calls for a signal that lasts for 8
ms, the book said that testing indicated it lasted for 1.1
ms. The IR LED must be connected to the digital pin 8
socket for the tone signal to make it flash on/off at 38 kHz.

1. What has to happen after the tone command


to find out if the IR receiver detects reflected
infrared?

The code has to pause for 1 ms with delay(1) and then


store the IR value by copying whatever digitalRead returns
to a variable; for example, delay(1) followed by irLeft =
digitalRead(10).
1. What does it mean if the IR detector sends a
low signal? What does it mean when the detector
sends a high signal?

A low signal means that 38 kHz IR was detected, which


means that the 38 kHz flashing light the IR LED
transmitted must have reflected off an object in its path. In
other words: object detected. A high signal means no 38
kHz IR was detected, so, there wasnt any object to reflect
the IR LEDs light. In other words: object not detected.

1. What happens if you change the value of a


resistor in series with a red LED? What happens if
you change the value of a resistor in series with an
infrared LED??

For both red and infrared LEDs, a smaller resistor will


cause the LED to glow more brightly. A larger resistor
results in dimmer LEDs. In terms of results, brighter IR
LEDs make it possible to detect objects that are farther
away. But it can also have annoying side effects, like
seeing the reflection of the surface it is rolling on and
misinterpreting it as an obstacle.
Exercises
1. Modify a line of code in IrInterferenceSniffer
so that it only monitors one of the IR detectors.
Remove the left IR detector from the code. Comment out
int irLeft = digitalRead(10) by placing // to its left. Then,
change if((irLeft == 0) || (irRight == 0)) to if(irRight == 0).
1. Modify AvoidTableEdge so that when it
makes the BOE Shield-Bot back up, it also turns a
little so that it doesnt get stuck going straight back
at the same drop-off.
Linear wheel speed control is in the -100 to 100 range. So
-75 should be a little slower than full speed backward.
With that in mind, try changing maneuver(-200, -200, 250)
to maneuver(-75, -200, 250).
Projects
1. Design a BOE Shield-Bot application that
sits still until you wave your hand in front of it, then it
starts roaming.
Add this code at the end of the setup function in
FastIrRoaming:
int irLeft = 1; // Declare IR variables and
int irRight = 1; // initialize both to 1

while((irLeft == 1) && (irRight == 1)) // Wait for

detection

irLeft = irDetect(9, 10, 38000); // Check for object on

left

irRight = irDetect(2, 3, 38000); // Check for object on

right

1. Design a BOE Shield-Bot application that


slowly rotates in place until it detects an object. As
soon as it detects an object, it locks onto and chases
the object. This is a classic SumoBot behavior.
Rotating in place and waiting to see an object is almost
the same as project 1. The maneuver function to make it
rotate slowly. Heres the code:
int irLeft = 1; // Declare IR variables and

int irRight = 1; // initialize both to 1

while((irLeft == 1) && (irRight == 1)) // Wait for

detection

irLeft = irDetect(9, 10, 38000); // Check for object on

left

irRight = irDetect(2, 3, 38000); // Check for object on

right

1. Design a BOE Shield-Bot application that


roams, but if it detects infrared interference, it
sounds the alarm briefly, and then continues
roaming. This alarm should be different from the low
battery alarm.
Combine FastIrRoaming and the tone portion of
IrInterferenceSniffer in a single sketch. This example also
turns on the lights while it sounds the alarm. The Code:
void setup() // Built-in initialization

block

pinMode(10, INPUT); pinMode(9, OUTPUT); // Left IR

LED & Receiver

pinMode(3, INPUT); pinMode(2, OUTPUT); // Right

IR LED & Receiver

pinMode(8, OUTPUT); pinMode(7, OUTPUT); // LED

indicators -> output

tone(4, 3000, 1000); // Play tone for 1

second

delay(1000); // Delay to finish tone


servoLeft.attach(13); // Attach left signal to

pin 13

servoRight.attach(12); // Attach right signal

to pin 12

void loop() // Main loop auto-repeats

// Sniff

int irLeft = digitalRead(10); // Check for IR on left

int irRight = digitalRead(3); // Check for IR on

right
if((irLeft == 0) || (irRight == 0))

for(int i = 0; i < 5; i++) // Repeat 5 times

digitalWrite(7, HIGH); // Turn indicator

LEDs on

digitalWrite(8, HIGH);

tone(4, 4000, 10); // Sound alarm tone

delay(20); // 10 ms tone, 10 between

tones

digitalWrite(7, LOW); // Turn indicator

LEDs off

digitalWrite(8, LOW);

}
// Roam

irLeft = irDetect(9, 10, 38000); // Check for object on

left

irRight = irDetect(2, 3, 38000); // Check for object on

right

if((irLeft == 0) && (irRight == 0)) // If both sides detect

maneuver(-200, -200, 20); // Backward 20

milliseconds

else if(irLeft == 0) // If only left side detects

{
maneuver(200, -200, 20); // Right for 20

ms

else if(irRight == 0) // If only right side

detects

maneuver(-200, 200, 20); // Left for 20 ms

else // Otherwise, no IR

detects

maneuver(200, 200, 20); // Forward 20 ms

You might also like