Servo Lab

For the servo lab, I used a photo resistor to control my servo.


#include
Servo myServo;
int photoResistor = A0;
int servoPin = 13;
int angle = 0;

int minLight = 5000;
int maxLight = 0;

void setup() {
// put your setup code here, to run once:
myServo.attach(servoPin);
Serial.begin(9600);
while (millis() < 5000) { int reading = analogRead(photoResistor); if (reading < minLight) minLight = reading; if (reading > maxLight) maxLight = reading;
}

}

void loop() {
// put your main code here, to run repeatedly:
int reading = analogRead(photoResistor);
Serial.println(reading);

myServo.write(map(reading, minLight, maxLight, 0, 180));

}

One thought on “Servo Lab

Leave a Reply

Your email address will not be published. Required fields are marked *