Video

Homework 03 – LED Lights & Servo Motor Controlled by LDR – Daniel Scott

31 Oct

For this assignment, I wanted to sync the Led lights with the Servo Motor with the LDR. Also, looking at what values the LDR was sensing, I changed the parameters to allow more movement.
The code is as follows:

#include

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

int potpin = 0; // analog pin used to connect the LDR
int val; // variable to read the value from the analog pin
int red = 6; //setting red LED variable
int blue = 5;//setting blue LED variable
int green = 2; //setting green LED variable

void setup()
{
Serial.begin(9600); // to get program to read values of PDL sensor
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(red, OUTPUT); //sets pin number as output
pinMode(blue, OUTPUT); //sets pin number as output
pinMode(green, OUTPUT);//sets pin number as output
}

void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 400, 810, 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 for the servo to get there
Serial.println(analogRead(potpin)); //used to determine values sensor is reading

digitalWrite(red, HIGH); //light on
delay(val); //light on, LDR controls light on time
digitalWrite(red, LOW); //light off
delay(val); //light off, LDR controls light off time
digitalWrite(blue, HIGH); //light on
delay(val); //light on, LDR controls light on time
digitalWrite(blue, LOW); //light off
delay(val); //light off, LDR controls light off time
digitalWrite(green, HIGH); //light on
delay(val); //light on, LDR controls light on time
digitalWrite(green, LOW); //light off
delay(val); //light off, LDR controls light off time
}

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