Chapter 1 Solutions
Question Solutions
- The Arduino module.
- Binary numbers, that is, 0’s and 1’s. We also saw examples of how the numbers that represent characters are ASCII codes, like 109 = ‘m’.
- The setup function’s 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 variable’s name is used for assignment and comparison in the sketch. The variable’s 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.
- The arithmetic operators are + add, – subtract, * multiply, / divide, and % modulus.
- It will be treated as a float type because it has a decimal point.
- Initialization, condition, increment.
- 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.
Exercise Solutions
- Solution:
Serial.print("the value of i = "); Serial.println(i);
- Solution:
long bigVal = 80000000;
- Solution:
if(myVar % 2 == 0) { Serial.println("The variable is even. "); } else { Serial.println("The variable is odd. "); }
- Solution:
for(int i = 21; i <= 39; i+=3) { Serial.print("i = "); Serial.println(i); }
- Solution:
char c = "a"; Serial.print("Character = "); Serial.print(c); Serial.print(" ASCII value = "); Serial.println(c, DEC);
- Solution:
for(char c = ’A’; c <=’Z’; c++){}
Project Solutions
- 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. }
- 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. }