Image

Homework 03 – Servo Motor Controlled by Variable Resistor – Arthur Dunne

30 Oct

Homework 03 - Servo Motor Controlled by Variable Resistor - Arthur Dunne

I wired the potentiometer to control the servo motor as well as the three LEDs at the same time. When I turned it on, the lights were flashing in their pattern and the servo motor started turning back and forth with similar delays to the flashing LEDs.

// Controlling a servo position and flashing LEDs using a potentiometer (variable resistor)
#include

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

int red = 3; // setting red led variable
int blue = 5; // setting blue led variable
int green = 6; // setting green led variable
int pot = 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
pinMode(red, OUTPUT); //connects the red LED to digital pin 3
pinMode(blue, OUTPUT); //connects the blue LED to digital pin 5
pinMode(green, OUTPUT); //connects the green LED to digital pin 6
}

void loop()
{
val = analogRead(pot); // reads the value of the potentiometer (value between 0 and 200)
val = map(val, 0, 200, 0, 500); // scale it to use it with the servo (value between 0 and 500)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there

pot = (analogRead (A0));
digitalWrite(red, HIGH); //turn red light on
delay(pot); //holds red light on for a certain amount of time based on what position the potentiometer is at
digitalWrite(red, LOW); //turns red light off
delay(pot); //holds red light off for a certain amount of time based on what position the potentiometer is at
digitalWrite(blue, HIGH); //turns blue light on
delay(pot); //holds blue light on for a certain amount of time based on what position the potentiometer is at
digitalWrite(blue, LOW); //turns blue light off
delay(pot);//holds blue light off for a certain amount of time based on what position the potentiometer is at
digitalWrite(blue, HIGH);//turns blue light on
delay(pot); //holds blue light on for a certain amount of time based on what position the potentiometer is at
digitalWrite(blue, LOW); //turns blue light off
delay(pot); //holds blue light off for a certain amount of time based on what position the potentiometer is at
digitalWrite(green, HIGH); //turns green light on
delay(pot); //holds green light on for a certain amount of time based on what position the potentiometer is at
digitalWrite(green, LOW); //turns green light off
delay(pot); //holds green light off for a certain amount of time based on what position the potentiometer is at
}

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