Homework 03-Server Motor Controlled by Variable Resistor-Elena Brown

31 Oct

image

I programmed the LEDs to blink in a sequence. The server motor rotated back and forth. The speed at which the blinking and rotating occurred was determined by the potentiometer.

Here is my code for this assignment

#include<Servo.h>

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

int red = 3; // setting red LED to pin 3

int blue = 5; // setting blue LED to pin 5  

int green = 6; // setting green LED to pin 6  

int pot = 0; // analog pin connected to A0 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);

pinMode(blue, OUTPUT);

pinMode(green, OUTPUT); 

}

void loop()  {  val = analogRead(pot); // reads the value from the potentiometer  

val = map(val, 0, 200, 0, 500); // maps the servo range of motion (value between 0 and 500)  

myservo.write(val); // moves the servo according to the scaled value from the potentiometer 

delay(15); // waits for the servo to move to new position

pot = (analogRead (A0));  

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

delay(pot); //holds red light on for period determined by potentiometer  

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

delay(pot); //holds red light off for period determined by potentiometer  

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

delay(pot); //holds blue light on for period determined by potentiometer  

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

delay(pot);//holds blue light off for period determined by potentiometer  

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

delay(pot); //holds blue light on for period determined by potentiometer  

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

delay(pot); //holds blue light off for period determined by potentiometer  

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

delay(pot); //holds green light on for period determined by potentiometer  

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

delay(pot); //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