// Servo Start (Back and Forth) w/ Arduino Servo Library
// Hardware Setup- a voltage divider connected to analog pin A5:
// 1) -Wire a photoresister between +5vdc and an empty row on the breadboard
// 2) -Wire a 10k resistor between that same row on the breadboard to ground.
// 3) -Jumper wire from that same row to analog pin A5
// 4) - Connect a servo to +, gnd and digital pin 9
// -----------------------------------------------------------
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int servoPin = 9; // variable for servo pin number
int a_in; // create variable for storing analog readings
int sensorPin = A5; // variable for analog pin as A5
void setup()
{
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
Serial.begin(9600); // start serial port at 9600 bps:
}
void loop() {
a_in = analogRead(sensorPin); // get 10 bit analog value
a_in = map(a_in, 0, 1023, 0, 180); // scale it to 8 bits
// Serial.println(a_in);
myservo.write(a_in); // tell servo to go to position 180
}
|