Sparki has a servo motor that moves its head around.
When the chip detects from the position sensor that the position is not where it is supposed to be, it tells the motor to move towards the correct position until it reaches it. You can tell the chip what position it is supposed to be by sending it a special signal.Don’t worry, Sparki’s servo code below already takes care of sending this special signal. All you have to tell it is what angle! 50 times a second (or every 20 milliseconds), this signal sends a on-off pulse. The length of this on-off pulse tells the servo what position it should be in. If it is 1 ms, then it should be 90 left. If it is 1.5 milliseconds, then the servo should be centered. If it is 2.5 ms, then it should be 90 degrees right:
For Sparki, the angles are these:
You can use any number from -90 to 90 in place of degree. This is equivalent to looking left and looking right. You can also use these keywords (in all caps):
SparkiDuino already has code examples for you to use:
File > Examples > Servo
How It Works
Servo motors are special types of motors that can go to the exact position that you tell them to. They can do this because in addition to the motor, it has a sensor that measures the position of the motor, and a chip that uses the position information from the sensor to control the motor.When the chip detects from the position sensor that the position is not where it is supposed to be, it tells the motor to move towards the correct position until it reaches it. You can tell the chip what position it is supposed to be by sending it a special signal.Don’t worry, Sparki’s servo code below already takes care of sending this special signal. All you have to tell it is what angle! 50 times a second (or every 20 milliseconds), this signal sends a on-off pulse. The length of this on-off pulse tells the servo what position it should be in. If it is 1 ms, then it should be 90 left. If it is 1.5 milliseconds, then the servo should be centered. If it is 2.5 ms, then it should be 90 degrees right:
For Sparki, the angles are these:
Using the Part
With the basic Sparki code in place, you can tell the servo to move using these commands:
1 |
sparki.servo(degree); |
1 2 3 |
SERVO_LEFT SERVO_CENTER SERVO_RIGHT |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
/******************************************* Basic Servo test Move the servo on Sparki's head. The servo can rotate from -90 to 90 degrees. -90 is facing left, 0 is facing forward, and 90 is facing right. ********************************************/ #include <Sparki.h> // include the sparki library void setup() { } void loop() { sparki.servo(SERVO_LEFT); // rotate the servo to is -90 degree postion (left) delay(1000); sparki.servo(SERVO_CENTER); // rotate the servo to is 0 degree postion (forward) delay(1000); sparki.servo(SERVO_RIGHT); // rotate the servo to is 90 degree postion (right) delay(1000); sparki.servo(-90); // rotate the servo to is -90 degree postion (left) delay(1000); sparki.servo(0); // rotate the servo to is 0 degree postion (forward) delay(1000); sparki.servo(90); // rotate the servo to is 90 degree postion (right) delay(1000); } |