Homework 03 — Servo Motor Controlled by Variable Resistor — Alexa Zyllo

30 Oct

The lights flash one at a time in a loop, while the potentiometer controls the Servo motor. In class I was having difficulties with my motor, sometimes it worked and others it didn’t. This was also happening with this project. Sometimes when I ran the code it worked, others it didn’t. Here’s my code:

________________________________________________________

#include <Servo.h>
Servo myservo;

int red = 3; //setting red LED variable
int green = 5; //setting green LED variable
int blue = 6; // setting blue LED variable
int potpin = 0; // setting the potentiometer
int val; // setting value to the analog pin

void setup() {
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue,OUTPUT);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer
val = map(val, 0, 1023, 0, 179); // use with servo
myservo.write(val); // sets the servo position according to value
delay(15); // waits for servo

digitalWrite(red, HIGH); // turn the red light on
delay(250); // wait 0.25 seconds

digitalWrite(red, LOW); // turn the red light off
delay(250); // wait 0.25 seconds

digitalWrite(green, HIGH); // turn the green light on
delay(250); // wait 0.25 seconds

digitalWrite(green, LOW); // turn the red light off
delay(250); // wait 0.25 seconds

digitalWrite(blue, HIGH); // turn the blue light on
delay(250); // wait 0.25 seconds

digitalWrite(blue, LOW); // turn the blue light off
delay(250); // wait 0.25 seconds

}

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