#include <Servo.h>
#define led 11
#define led 12
//accesses the Arduino Servo Library
Servo myservo; // creates servo object to control a servo
int val; // variable to read the value from the analog pin
void setup()
{
myservo.attach(8);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
digitalWrite(11,HIGH);
delay(500);
digitalWrite(12,HIGH);
delay(500);// ensures output to servo on pin 8
}
void loop()
{
val = analogRead(1); // reads the value of the potentiometer from A1 (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // converts reading from potentiometer to an output value in degrees of rotation that the servo can understand
myservo.write(val); // sets the servo position according to the input from the potentiometer
delay(15); // waits 15ms for the servo to get to set position
}
Moving object
Locomotive project
Comments
Please log in or sign up to comment.