The focus of this chapter was calibrating and testing the servos, and building indicator lights to monitor the servo signals. In addition to some hardware setup, many concepts related to electronics, programming, and even a few good engineering concepts were introduced along the way.
Hardware Setup
Electronics
Programming
Engineering
Question Solutions
digitalWrite(13, HIGH) // 5 V digitalWrite(13, LOW) // 0 V
Exercise Solutions
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 }
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 }
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 }
Project Solutions
/* 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 }