Homework 03 – Servo Motor + Dimming Lights – Olivia Coughlin

30 Oct

Synching the knob code/circuit with the light dimming code/circuit I created (hopefully) a spinning propeller with simultaneous dimming red light and blinking blue light.

 

const int red = 3; //red LED is on pin 3
int i = 0; //to count up and down
const int blue = 5; //blue LED is on pin 5
int potpin = 0; //analog pin is 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); //red LED is an output
pinMode(blue, OUTPUT); //blue LED is an output
}

void loop(){

for (i = 0; i <255; i++) { //loop from 0 to 255, fade in
analogWrite(LED, i); //set the LED brightness
delay(10); //wait 10ms
potpin = (analogRead(A0)); //potentiometer is attached to analog 0
digitalWrite(red, HIGH); //turns red LED on
delay(potpin); //keeps red LED on
digitalWrite(blue, HIGH); //turns blue LED on
delay(potpin); //keeps blue LED on
digitalWrite(blue, LOW); //turns blue LED off
delay(potpin); //keeps blue LED off
digitalWrite(red, LOW); //turns red LED off
delay(potpin); //keeps red LED off
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 1023, 0, 179); // 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
}
for i = 255; i > 0; i–) { //loops red LED from 255 to 1, fade out
analogWrite(LED, i); //set the LED brightness
delay(10); //wait 10ms
}

}

IMAG1388

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