Homework 03 – Servo Motor Controlled by Variable Resistor – Charlene Kulesa

31 Oct

I used the photoresistor (LDR) to control the speed of the LED lights and the movement of the servo motor. Here is my video and code:

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int LDR = 0; // analog pin used to connect the LDR
int val; // variable to read the value from the analog pin
int blue = 3;//setting blue LED variable
int green = 5;// setting green LED variable
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode (blue, OUTPUT);// sets pin 3
pinMode (green, OUTPUT);// sets pin 5
}

void loop()
{
val = analogRead(LDR); // reads the value of the LDR (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(10); // waits .10 sec for the servo to get there
digitalWrite (blue, HIGH); // Turns on the blue light
delay(val); // delay depending on val (LDR)
digitalWrite (blue, LOW); // Turns off the blue light
delay(val); // delay depending on val (LDR)
digitalWrite (green, HIGH); // Turns on the green light
delay(val); // delay depending on val (LDR)
digitalWrite (green, LOW); // Turns off the green light
}

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s