Escolar Documentos
Profissional Documentos
Cultura Documentos
Questions
1. What device will be the brain of your BOE Shield-Bot?
Binary numbers, that is, 0s and 1s. We also saw examples of how the numbers that
represent characters are ASCII codes, like 109 = m.
The setup functions statements get executed once when the sketch starts. After
finishing the setup function, the sketch advances to the loop function. Its code block
gets repeated indefinitely.
The variables name is used for assignment and comparison in the sketch. The
variables type defines the kind and range of values it can store.
Global variables can be accessed and modified by any function in the sketch. Local
variables can only be accessed and modified within the block where they are declared.
1. What are the arithmetic operators? What does each one do?
The arithmetic operators are + add, - subtract, * multiply, / divide, and % modulus.
1. What variable type will the Arduino editor apply to 21.5 if it appears in
your code? Why?
A block comment starts with /* and ends with */, and allows you to write comments
that span multiple lines. A line comment starts with // and makes whatever is to its
right on that particular line a comment.
Exercises
1. Write a piece of code that displays the value of i = followed by the
value of stored in the variable i in the Serial Monitor. Both should display on the
same line, and then move the cursor to the beginning of the next line for displaying
more messages.
Serial.print("the value of i = ");
Serial.println(i);
else
1. Write a for loop that starts counting at 21 and stops at 39, and counts in
steps of 3.
for(int i = 21; i <= 39; i+=3)
Serial.print("i = ");
Serial.println(i);
1. Write a piece of code that displays the character a variable stores along
with its ASCII value.
char c = "a";
Serial.print("Character = ");
Serial.print(c);
Serial.println(c, DEC);
1. Write a for loop, but instead of counting from one value to another,
make it count from 'A' to 'Z' and display the letters in the alphabet.
for(char c = 'A'; c <='Z'; c++){}
Projects
1. Write a sketch to display the printable ASCII characters. The first
printable character is the space character, which is one press/release of your keyboards
space bar between apostrophes, like this: . The last printable character is the tilde
character ~. Alternately, you could use 32 for the loops start value and 126 for the
end value.
This sketch is a modified version of CountToTen that utilizes the solution to Exercise 5 for
displaying ASCII characters.
Serial.begin(9600);
Serial.print("Character = ");
Serial.print(c);
Serial.println(c, DEC);
Serial.println("All done!");
void loop()
1. Write a sketch that tells you if a variable is odd or even. Hint: when a
number is even, the remainder of the number divided by 2 is 0. Hint: variable % 2 ==
0.
This sketch is a modified version of SimpleDecisions that uses a variation of the solution
from Exercise 3 to display whether the variable is odd or even.
// Robotics with the BOE Shield - Chapter 1, Project 2
void setup()
Serial.begin(9600);
int a = 20;
if(a % 2 == 0)
Serial.print("a is even");
else
Serial.print("a is odd");
void loop()
}
Chapter 2 Challenges
Questions
1. How do you connect two leads together using a breadboard?
Plug the two leads into the same 5-socket row on the breadboard.
digitalWrite(13, HIGH) // 5 V
digitalWrite(13, LOW) // 0 V
Assuming a pin has just been set high, the delay call can keep it high for a certain
amount of time. Then, a digitalWrite call can set it low.
1. What are the pulse durations that tell a continuous rotation servo to turn
a. full speed clockwise,
b. full speed counterclockwise,
c. stay still.
(a)1.3 ms pulses for full speed clockwise, (b)1.7 ms pulses for full speed clockwise, and
(c)1.5 ms pulses for stay still.
Exercises
1. Write a loop function that makes an LED blink 5 times per second, with
an on time thats 1/3 of its off time. (Disconnect the servos for this exercise!)
rd
The total on + off time has to be 200 ms, which is 1/5th of a second. So, on for 50 ms,
off for 150 ms:
1. Write a setup function that makes the pin 13 servo turn full speed
clockwise for 1.2 seconds, while the pin 12 servo stays still. After that, set both servos
to stop.
Set pin 13 servo to full speed clockwise and the pin 12 servo to stop. Then, delay for
1200. Since servoRight is already stopped, all the code has to do is stop servoLeft.
1. Write a setup function that makes one servo turn the same direction for
3 seconds. The other servo should turn the opposite direction for the first 1.5 seconds
and the same direction for the second 1.5 seconds. Then, make both servos stop.
In this example, the pin 13 servo starts counterclockwise and the pin 12 servo starts out
clockwise. This goes on for 1.5 seconds. Then, the pin 12 servo is changed to
counterclockwise, and this goes on for another 1.5 seconds. After that, both servos are
stopped.
delay(1500);
Projects
1. Look up the servo librarys detach function and use it in place of
servoLeft and servoRight.writeMicroseconds(1500) to stop servos
after they turn for 3 seconds.
The detach function detaches the instance of Servo from its pin. This sketch verifies
that it stops the servo after 3 seconds of run time. The chapter examples sent pulses
telling the servo to stay still. In contrast, detach stops sending signals to the servo
the pin doesnt tell the servo to do anything, so it goes dormant instead of holding the
stop speed. The end result is the same, the servo motor stops. The advantage to
detach is that it prevents the servos from turning slowly if the servo is not precisely
calibrated.
/*
*/
1. Write a program that makes the pin 13 servo turn counterclockwise while
the pin 12 servo turns clockwise. After 3 seconds, make both servos turn
counterclockwise for 0.6 seconds. Then, make both turn clockwise for 0.6 seconds.
Then, make the pin 13 servo turn clockwise and the pin 12 servo turn counterclockwise
for 3 seconds.