Homework 03 – Servo Motor Controlled by Variable Resistor – Josiah Song

31 Oct

Image

For this assignment, I chose to use the potentiometer to control both the servo motor and the three LEDs. The LEDs flash in a row in a basic pattern and the speed is determined by the position of the potentiometer. The servo motor position also depends on the position of the potentiometer.

#include <Servo.h>
 
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 potpin = 0;//analog pin used to connect the potentiometer
int value;//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);//sets pin 3 to output
  pinMode(blue, OUTPUT);//sets pin 5 to output
  pinMode(green, OUTPUT);//sets pin 6 to output
}
 
void loop()
{
  value = analogRead(potpin);// reads the value of the potentiometer (value between 0 and 400)
  value = map(value, 0, 400, 0, 1000);// scale it to use it with the servo (value between 0 and 1000)
  myservo.write(value);// sets the servo position according to the scaled value
  delay(15);// waits for the servo to get there
  potpin = (analogRead(A0));//sets variable to register potentiometer
  digitalWrite(red, HIGH);//turns red led on
  delay(potpin);//delays how long the light stays on based on potentiometer position
  digitalWrite(red, LOW);//turns red led off
  delay(potpin);//delays how long the light stays off based on potentiometer position
  digitalWrite(blue, HIGH);//turns blue led on
  delay(potpin);//delays how long the light stays on based on potentiometer position
  digitalWrite(blue, LOW);//turns blue led off
  delay(potpin);//delays how long the light stays off based on potentiometer position
  digitalWrite(green, HIGH);//turns green led on
  delay(potpin);//delays how long the light stays on based on potentiometer position
  digitalWrite(green, LOW);//turns green led off
  delay(potpin);//delays how long the light stays off based on potentiometer position
}

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