PAGE 2 | GREEN ENERGY HANDBOOK © 2 0 0 8 - 2018 M I C H A E L E T T E R S H A N K
https://www.robotscience.co.za
/*
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
modified on 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Knob
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // map the analogue input values to the servo output values (between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
Now replace the variable resistor [voltage divider] with a light dependent resistor [LDR] and pull up resistor and observe what
happens when you shine a bright light on the LED. Try swapping the LDR and the pull up resistor to a pull down resistor.