Homework 04- Multiple Functions -Sarah Wolf

4 Nov

This circuit uses the fade LED code (for the blue light), a push button controlling the green LED light, and a potentiometer controlling a Servo motor.

Here’s the code:
//Assignment 4 – having an LED fade in and out, a servo motor
//controlled by a potentiometer, and a motion-sensitive buzzer

const int blueLED = 12; //pin LED is set to
int i = 0; // used to count up and down
int BUTTON = 7; //button is set to pin 7
int redLED = 13; //red LED to go with button set to pin 3
int val = 0;// setting the value to 0
int quail = 0; // setting button value to 0

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

int potpin = 0; // analog pin used to connect the potentiometer

void setup() {

pinMode(blueLED, OUTPUT); // tell Arduino LED is an output
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(redLED, OUTPUT);//set LED pin 3 to be used as output
pinMode(BUTTON, INPUT);//set button on pin 7 to be input

void loop(){

for (i = 0; i 0; i–) {

analogWrite(blueLED,i); // set the LED brightness
delay(10); //wait 10ms

val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 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

quail =digitalRead(BUTTON); //read input on button pin 7 and store it

if(quail == HIGH) {
digitalWrite(redLED, HIGH);
else {
digitalWrite(redLED, LOW);

Originally, I tried to use the motion sensor and buzzer to be activated by the Servo motor’s motion, but this didn’t work very well and the motion sensor didn’t seem to register the Servo motor as a source of motion.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s