Hardware Reference
In-Depth Information
Robot. For this, two TinkerKit digital inputs are used. TK5, placed on the left
of the robot controls the left motor, and TK7 placed on the right controls the
right motor. A logical 1 means that the motor turns, and a logical 0 stops the
motor. These inputs will be read periodically. The speed of the wheels will be
controlled by the potentiometer.
The sketch looks like Listing 22-1.
Listing 22-1: Sketch (fi lename: Chapter22.ino )
1 #include <ArduinoRobot.h>
2
3 void setup()
4 {
5 Robot.begin(); // Start the control board
6 }
7
8 void loop()
9 {
10 // Read in potentiometer values
11 int speed = Robot.knobRead();
12
13 // Potentiometer data is 0-1023, motors expect 0-255
14 // (we won't use negative values)
15
16 int motorSpeed = map(speed, 0, 1023, 0, 255);
17
18 // Motor variables
19 int leftMotor = 0;
20 int rightMotor = 0;
21
22 if (Robot.digitalRead(TK5) == true)
23 leftMotor = motorSpeed;
24
25 if (Robot.digitalRead(TK7) == true)
26 rightMotor = motorSpeed;
27
28 // Now control the motors
29 Robot.motorsWrite(leftMotor, rightMotor);
30
31 // Sleep for a tenth of a second
32 delay(100);
33 }
On line 1, the Arduino Robot library is imported. On line 5 in setup() , Robot
.begin() is called. From here on, the user can call Robot functions.
loop() is declared on line 8. Because the motor speed will be controlled by
the value of the potentiometer, the analog value is read in on line 11. This value
is stored in an int called speed . The potentiometer gives values between 0 and
Search WWH ::




Custom Search