Homework 03 – Server Motor Controlled by Variable Resistor – Garrett Jensen

31 Oct

Like other posts I too used the photoresistor (LDR) to control the speed of the LED lights that had been set in a sequence as well as the movement of the servo motor.

My Code:

#include <Servo.h>

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

int blue = 5; // setting blue LED to pin 5
int green = 6; // setting green LED to pin 6
int potpin = 0; // analog pin used to connect the photoresistor
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
pinMode(blue, OUTPUT); // blue light output
pinMode(green, OUTPUT); // green light output
}

void loop()
{
val = analogRead(potpin); // reads the value of the photoresistor (value between 0 and 1023)
val = map(val, 0, 200, 0, 500); // 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(15); // waits for the servo to get there

potpin = (analogRead (A0));

digitalWrite(blue, HIGH); //turns blue light on

delay(potpin); //holds blue light on for period determined by photoresistor

digitalWrite(blue, LOW); //turns blue light off

delay(potpin);//holds blue light off for period determined by photoresistor

digitalWrite(blue, HIGH);//turns blue light on

delay(potpin); //holds blue light on for period determined by photoresistor

digitalWrite(blue, LOW); //turns blue light off

delay(potpin); //holds blue light off for period determined by photoresistor

digitalWrite(green, HIGH); //turns green light on

delay(potpin); //holds green light on for period determined by photoresistor

digitalWrite(green, LOW); //turns green light off

delay(potpin); //holds green light off for period determined by potentiometer }

}

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