Você está na página 1de 16

Efan Altaee

Chapter 1 Challenges
Questions
1. What device will be the brain of your BOE
Shield-Bot?

The Arduino module.

2. When the Arduino sends a character to


your PC/laptop, what type of numbers are used to
send the message through the programming
cable?

Binary numbers, that is, 0s and 1s. We also saw


examples of how the numbers that represent
characters are ASCII codes, like 109 = m.

3. What is the difference between


the setup and loop functions?

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.

4. Whats the difference between a variable


name and a variable type?

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.
5. Whats the difference between global and
local variables?

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.

6. What are the arithmetic operators? What


does each one do?

The arithmetic operators are + add, - subtract, *


multiply, / divide, and % modulus.

7. What variable type will the Arduino editor


apply to 21.5 if it appears in your code? Why?

It will be treated as a float type because it has a


decimal point.

8. What three elements are included between


parentheses in a for loop?

Initialization, condition, increment.

9. Whats the difference between a block


comment and a line comment?

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);

2. Declare a long variable named bigVal, and


initialize it to 80 million.
long bigVal = 80000000;

3. Write an ifelse statement that takes the


modulus of a variable divided by 2 and compares it
to zero. If the result is zero, display The variable
is even. If not, display The variable is odd.
if(myVar % 2 == 0)
{
Serial.println("The variable is even. ");
}
else
{
Serial.println("The variable is odd. ");
}

4. 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);
}

5. 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.print(" ASCII value = ");
Serial.println(c, DEC);

6. 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.

// Robotics with the BOE Shield - Chapter 1,


Project 1

void setup()
{
Serial.begin(9600);

for(char c = ' '; c <= '~'; c++)


{
Serial.print("Character = ");
Serial.print(c);
Serial.print(" ASCII value = ");
Serial.println(c, DEC);
}
Serial.println("All done!");
}

void loop()
{
// Empty, no repeating code.
}

2. 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()
{
// Empty, no repeating code.
}
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.

2. What function sets a digital pins


direction?

The pinMode function.

3. What function sets pin 13 to 5 V? What


function sets it to 0 V?

The digitalWrite function does both, depending


on its value parameter:

digitalWrite(13, HIGH) // 5 V
digitalWrite(13, LOW) // 0 V

4. How can a sketch control the duration of a


5 V signal?
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.

5. 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.

6. Which call would make a servo turn faster?


seservoLeft.writeMicroseconds(1420).
Full speed clockwise is
servoLeft.writeMicroseconds(1300), and
stop is
servoLeft.WriteMicroseconds(1500).
Since 1420 is further from stop and closer
to full speed, its the correct value for
faster clockwise rotation even though it is
smaller.rvoLeft.writeMicroseconds(1440)
or
servoLeft.writeMicroseconds(1420).
7. How can a sketch control the duration of a
certain servo signal?

Servo.writeMicroseconds(value) followed by
delay(ms) followed by
Servo.writeMicroseconds(newValue) or
Servo.detach(pin) will keep the servo turning for
ms milliseconds.

Exercises
1. Write a loop function that makes an LED blink
5 times per second, with an on time thats 1/3rd of
its off time. (Disconnect the servos for this
exercise!)

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:

void loop() // Main loop auto-repeats


{ // 200 ms -> 5 blinks/second
digitalWrite(13, HIGH); // Pin 13 = 5 V, LED
emits light
delay(50); // ..for 0.05 seconds
digitalWrite(13, LOW); // Pin 13 = 0 V, LED no
light
delay(150); // ..for 0.15 seconds
}

2. 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.

void setup() // Built in initialization


block
{
servoLeft.attach(13); // Attach left signal to
pin 13
servoRight.attach(12); // Attach right signal to
pin 12

servoLeft.writeMicroseconds(1300); // 1.3 ms ->


clockwise
servoRight.writeMicroseconds(1500); // 1.5 ms
-> stop
delay(1200); // ..for 1.2 seconds
servoLeft.writeMicroseconds(1500); // 1.5 ms ->
stop
}

3. 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.

void setup() // Built in initialization


block
{
servoLeft.attach(13); // Attach left signal to
pin 13
servoRight.attach(12); // Attach right signal to
pin 12

servoLeft.writeMicroseconds(1700); // 1.7 ms ->


cc-wise
servoRight.writeMicroseconds(1300); // 1.3 ms
-> clockwise
delay(1500); // ..for 1.5 seconds
servoRight.writeMicroseconds(1700); // 1.7 ms
-> cc-wise
delay(1500);
servoLeft.writeMicroseconds(1500); // 1.5 ms ->
stop
servoRight.writeMicroseconds(1500); // 1.5 ms
-> stop
}

Projects
1. Look up the servo librarys detach function
and use it in place
of servoLeft and servoRight.writeMicrosecond
s(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
servothe 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.
/*
Robotics with the BOE Shield Chapter 2, Project
1
Generate a servo full speed counterclockwise
signal with pin 13 and
full speed clockwise signal with pin 12.
*/

#include <Servo.h> // Include servo


library

Servo servoLeft; // Declare left


servo signal
Servo servoRight; // Declare right
servo signal

void setup() // Built in initialization


block
{
servoLeft.attach(13); // Attach left signal
to pin 13
servoRight.attach(12); // Attach right
signal to pin 12

servoLeft.writeMicroseconds(1700); // Pin 13
counterclockwise
servoRight.writeMicroseconds(1300); // Pin 12
clockwise
delay(3000); // ..for 3 seconds
servoLeft.detach(); // Stop servo
signal to pin 13
servoRight.detach(); // Stop servo
signal to pin 12
}

void loop() // Main loop auto-


repeats
{ // Empty, nothing needs
repeating
}
2. 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.

Solution is the sketch ForwardLeftRightBackward,


from a later chapter.

Você também pode gostar