position = _positions[i];
-
break;
-
}
-
}
30
return position;
-
}
-
};
-
The code defines several member variables:
_members
contains a list of up to
ten team member names,
_num_members
contains the actual number of people
on the team, and we store the position (angle) of the team member’s name
on the Blaminatr display in
_positions
.
The constructor expects an array of strings that contains the team members’
names and that is terminated by a NULL pointer. We store a reference to the
list, and then we calculate the number of team members. We iterate over the
array until we find a NULL pointer. All this happens in lines 10 to 13.
Then we calculate the position of each team member’s name on the Blaminatr’s
display. Every team member gets his or her fair share on the 180-degree
display, and the Blaminatr will point to the share’s center, so we divide the
share by 2. We store the positions in the
_positions
array that corresponds to
the
_members
array. That means the first entry of
_positions
contains the position
of the first team member, and so on.
With the
get_position
method, we get back the position belonging to a certain
name. We walk through the
_members
array and check whether we have found
the right member using the
strcmp
function. As soon as we’ve found it, we
return the corresponding entry of the
_positions
array. If we can’t find a team
member with the name we are looking for, we return 0.
Implementing a
Blaminatr
class is easy now:
Motors/Blaminatr/Blaminatr.ino
#include <Servo.h>
const unsigned int MOTOR_PIN = 9;
const unsigned int MOTOR_DELAY = 15;
class Blaminatr {
Team _team;
Servo _servo;
public:
Blaminatr(const Team& team) : _team(team) {}
void attach(const int sensor_pin) {
_servo.attach(sensor_pin);
Chapter 13. Controlling Motors with Arduino • 232
report erratum • discuss
www.it-ebooks.info