Electronics by Manmohan Pal: Servo potentio meter control

Servo potentio meter control














_______________________________________________________________
_______________________________________________________________


//including the library that we need to control the servo motor
#include <Servo.h>
//declaring our servo
Servo servo;
void setup() {
//declaring A0 analog pin as input
pinMode(A0, INPUT);
//setting pin 13 as a servo pin, because here we connected our servo
servo.attach(13);
}
void loop() {
//here we are mapping the value of analogRead() from pin A0 (potentiometer)
//this value is between 0 and 1023 and we want to have 0 180 because this
//is the value that servo uses when it is mapped we can write it to servo
//with servo.write()
servo.write(map(analogRead(A0), 0, 1023, 0, 180));
//map function works similiar to smoething like this:
//analogRead(A0)/1023 * 180
//but it has some more advanced features
}

No comments:

Post a Comment