25
STEP2:
Connect Crowtail-9G
Servo to D5 port of Crowtail-
Motor Base Shield. Connect
Crowtail-Thumb Joystick to
A4&A5 port of Crowtail-Motor
Base Shield. The complete
connection is as follows.
Open the downloaded folder “Crowtail Deluxe Kit for Arduino demo code”, navigate to the folder “lib”,
and add “Servo” folder to the Arduino library. Open the
P11_Servo_Control
with Arduino IDE and
upload it.
At first, the servo will rotate to 90 degrees of its midpoint. When you rock the joystick left and right
along the x-axis, you can see that the servo will rotate to the corresponding angle as the joystick
moves. Whenever you press the joystick cap, the servo will rotate its midpoint 90 degrees.
What will you see
1. Import the servo library and create an instance of the servo.
2. Declare two variables to store the joystick value and servo angle value.
3. Attaches the servo instance object on pin 5 and initialize it with 90 degrees.
4. Read the joystick value
5. If the joystick button is pressed, let the servo rotate to 90 degrees.
6. If the joystick button is not pressed, let the servo rotate to the corresponding angle as the joystick
moves using map() function.
Code overview
Code usage
Import the servo library. Servo.h is a wonderful library of the servo, it provides a very convenient
function to control the rotation of servo. With using this library, we don't need to bother to check the
contrast between the rotation angle of the PWM and the servo, we only need to input the angle we
want the servo to rotate in the rotation function.
Servo library: #include <Servo.h>
Create a servo object to control the servo. After import the servo library, we need to create a servo
object to tell that we need to control this servo.
Create servo instance: Servo myservo;
Declare which pin the servo is connected to. Different from the other modules which use variable to
initialize which pin them should be connected to, servo use ServoObject.attach() function to tell which
Attach function: myservo.attach(5);
Summary of Contents for Crowtail Deluxe Kit
Page 47: ......