Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Maik Schmidt - Arduino A Quick-Start Guide, 2nd Edition (The Pragmatic Programmers) - 2015.pdf
Скачиваний:
146
Добавлен:
22.03.2016
Размер:
30.47 Mб
Скачать

Chapter 13. Controlling Motors with Arduino 230

Arduino Arts

You can use the Arduino not just for gadgets or fun projects, but also in artistic ways. Especially in the new-media art area, you will find many amazing projects built with the Arduino. One of them is Anthros,a a responsive environment that observes a small area using a webcam. The area contains some “tentacles,” and whenever a person crosses the area, the tentacles move in the person’s direction. Servos move the tentacles, and an Arduino controls the servos.

For all people interested in new-media art, Alicia Gibb’s thesis, “New Media Art, Design, and the Arduino Microcontroller: A Malleable Tool,”b is a must-read.

a.http://makezine.com/2010/04/19/arduino-powered-kinetic-sculpture/

b.http://aliciagibb.com/thesis/

motor moves to the angle you’ve specified. To see the effect a bit better, form a wire or some paper into an arrow and attach it to the motor’s gear.

It’s easy to control a servo via the serial port, and the circuit we’ve built can be the basis for many useful and fun projects. In the next section, we’ll use it to build an automatic blaming device.

Building a Blaminatr

Finger-pointing isn’t nice, but it can be oddly satisfying. In this section, we’ll build a device that I call Blaminatr. Instead of blaming someone directly, you can tell the Blaminatr to do so. In the following figure, you can see the device in action. Tell it to blame me, and it moves an arrow so it points to “Maik.”

report erratum • discuss

Building a Blaminatr 231

Blaminatrs are perfect office toys that you can use in many situations. For software developers, it can be a good idea to attach one to your continuous integration (CI) system. Continuous integration systems, such as Jenkins,3

 

help you continuously check whether your software is in good shape.

 

Whenever a developer checks in changes, the CI automatically compiles the

 

software and runs all tests. Then it publishes the results via email or as an

 

RSS feed. You can easily write a small piece of software that subscribes to

 

such a feed. Whenever someone breaks the build, you’ll find a notification in

 

the feed, and you can use the Blaminatr to point to the name of the developer

 

who has committed the latest changes.4

 

In the previous section, you learned all about the servo motor you need to

 

build the Blaminatr. Now we need only some creativity to build the device’s

 

display, and we need more elaborate software. We start with a class named

 

Team that represents the members of our team; that is, the potential “blamees”:

 

Motors/Blaminatr/Blaminatr.ino

Line 1

const unsigned int MAX_MEMBERS = 10;

-

 

-class Team {

-const char** _members;

5unsigned int _num_members;

-unsigned int _positions[MAX_MEMBERS];

-public:

-Team(const char** members) {

-_members = members;

10 _num_members = 0;

-const char** member = _members;

-while (*member++)

-_num_members++;

-

15 const unsigned int share = 180 / _num_members;

-unsigned int pos = share / 2;

-for (unsigned int i = 0; i < _num_members; i++) {

-_positions[i] = pos;

-pos += share;

20 }

-}

-

-int get_position(const char* name) const {

-int position = 0;

25 for (unsigned int i = 0; i < _num_members; i++) {

-if (!strcmp(_members[i], name)) {

3.http://jenkins-ci.org//

4.At http://urbanhonking.com/ideasfordozens/2010/05/19/the_github_stoplight/, you can see an alternative project. It uses a traffic light to indicate your project’s current status.

report erratum • discuss

Chapter 13. Controlling Motors with Arduino 232

-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);

report erratum • discuss

Building a Blaminatr 233

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

Line 1 const unsigned int MAX_NAME = 30;

-const unsigned int BAUD_RATE = 9600;

-const unsigned int SERIAL_DELAY = 5;

-

5 const char* members[] = { "nobody", "Bob", "Alice", "Maik", NULL };

-Team team(members);

-Blaminatr blaminatr(team);

-

- void setup() {

10 Serial.begin(BAUD_RATE);

-blaminatr.attach(MOTOR_PIN);

-blaminatr.blame("nobody");

-}

-

15 void loop() {

-char name[MAX_NAME + 1];

-if (Serial.available()) {

-unsigned int i = 0;

-while (Serial.available() && i < MAX_NAME + 1) { 20 const char c = Serial.read();

-if (c != -1 && c != '\n')

-name[i++] = c;

-delay(SERIAL_DELAY);

-}

25 name[i] = 0;

-Serial.print(name);

-Serial.println(" is to blame.");

-blaminatr.blame(name);

-}

30 }

report erratum • discuss

Chapter 13. Controlling Motors with Arduino 234

We define a list of member names that is terminated by a NULL pointer. The list’s first entry is “nobody,” so we don’t have to deal with the rare edge case when nobody is to blame. Then we use members to initialize a new Team object and pass this object to the Blaminatr’s constructor.

More Motors Projects

Motors are fascinating. Search the Net, and you’ll find numerous projects combining the Arduino with motors. Most of them probably deal with robotsa or remote-controlled cars.

You’ll also find useful and exciting project like the USB hourglass.b It uses an Arduino and a servo motor to turn a sand timer, and it observes the falling sand using an optical sensor. Whenever all the sand has fallen through, the device turns the timer automatically.

That’s all nice, but the device’s main purpose is to generate true random numbers. Falling sand is a perfect basis for generating true randomness (see Generating Random Numbers, on page 48), and the USB hourglass uses the signals from its optical sensor to generate random numbers, sending them to the serial port.

a.http://makezine.com/projects/building-a-simple-arduino-robot/

b.http://makezine.com/2009/12/23/usb-hourglass-random-number-generat/

In the setup function, we initialize the serial port and attach the Blaminatr’s servo motor to the pin we defined in MOTOR_PIN. Also, we initialize the Blaminatr by blaming “nobody.”

The loop function is nearly the same as in First Steps with a Servo Motor, on page 227. The only difference is that we do not control a servo directly, but instead call blame in line 28.

That’s it! You can now start to draw your own display and create your own arrow. Attach them directly to the motor or—even better—put everything into a nice box. Compile and upload the software and start to blame.

Of course, you can use motors for more serious projects. You can use them to build robots running on wheels or similar devices. But you cannot attach too many motors to a “naked” Arduino, because it isn’t meant for driving bigger loads. So if you have a project in mind that needs a significant number of motors, you should consider buying a motor shield5 or using a special shield, such as the Robotics Shield Kit.6

5.You can find them at http://adafruit.com or http://makershed.com.

6.http://www.parallax.com/product/130-35000

report erratum • discuss

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]