dimanche 10 février 2013

Controlling the steering servo with a Potentiometer: Code and video


To make the control of the servo motor easier, there is a special library made for: servo.h (http://www.arduino.cc/en/Reference/Servo)
I will use it here.

Here is the code:

#include <Servo.h>
Servo frontservo; // declare servo name as "frontservo"
int potpin = A0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup()
{
frontservo.attach(9); // attach the servo on pin 9 to the servo object
Serial.begin(9600); // set the serial port rate to 9600 bauds
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 179 degres)
frontservo.write(val); // sets the servo position according to the scaled value
Serial.println(val); // send the value via the serial port
delay(15); // waits for the servo to get there
}

On the computer side, I will use the serial monitor of the Arduino programm (Ctrl Shift M).

The video:



So you can see thanks to this , that the wheels are centered when the value is around 100 ( between 90 and 100). The maximum movement on the is right about 130, on the left about 60. 

Aucun commentaire:

Enregistrer un commentaire