delay(MOTOR_DELAY);
}
void blame(const char* name) {
_servo.write(_team.get_position(name));
delay(MOTOR_DELAY);
}
};
A
Blaminatr
object aggregates a
Team
object and a
Servo
object. The constructor
initializes the
Team
instance while we can initialize the
Servo
instance by calling
the
attach
method.
The most interesting method is
blame
. It expects the name of the team member
to blame, calculates his position, and moves the servo accordingly. Let’s put
it all together now:
Motors/Blaminatr/Blaminatr.ino
const unsigned int MAX_NAME = 30;
Line 1
const unsigned int BAUD_RATE = 9600;
-
const unsigned int SERIAL_DELAY = 5;
-
-
const char* members[] = { "nobody", "Bob", "Alice", "Maik", NULL };
5
Team team(members);
-
Blaminatr blaminatr(team);
-
-
void setup() {
-
Serial.begin(BAUD_RATE);
10
blaminatr.attach(MOTOR_PIN);
-
blaminatr.blame("nobody");
-
}
-
-
void loop() {
15
char name[MAX_NAME + 1];
-
if (Serial.available()) {
-
unsigned int i = 0;
-
while (Serial.available() && i < MAX_NAME + 1) {
-
const char c = Serial.read();
20
if (c != -1 && c != '\n')
-
name[i++] = c;
-
delay(SERIAL_DELAY);
-
}
-
name[i] = 0;
25
Serial.print(name);
-
Serial.println(" is to blame.");
-
blaminatr.blame(name);
-
}
-
}
30
report erratum • discuss
Building a Blaminatr • 233
www.it-ebooks.info