Professional Documents
Culture Documents
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.
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.
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.
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.
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.
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
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.
void setup() // Built in initialization block
{
servoLeft.attach(13); // Attach left signal to pin 13
servoRight.attach(12); // Attach right signal to pin 12
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.
void setup() // Built in initialization block
{
servoLeft.attach(13); // Attach left signal to pin 13
servoRight.attach(12); // Attach right signal to pin 12
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.
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.