Homework 03 – Servo Motor alarm lights – Sarah Wolf

31 Oct

I decided to create a motion-sensitive Servo motor reaction that is caused by movement as the hand goes to press a button to turn a red LED light on. Unfortunately, the proximity of the button was a bit far from the motion sensor so I had to kind of wave my hand in front of the motion sensor as I pressed the button, but the code worked!

Here’s the code:

int redLED = 13; // setting the red LED to pin 13
int Button = 7; // set button to pin 7

int value = 0; // setting the value to 0


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

int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin

void setup()
pinMode (redLED, OUTPUT); // set pin 13 as the output variable
pinMode (Button, INPUT); // Button on pin 7 is the input variable
myservo.attach(9); // attaches the servo on pin 13 to the servo object

void loop()
val = digitalRead(Button);// setting Button on pin 7

if(val == HIGH) { //if pin 7/Button high, then do something
digitalWrite(redLED, HIGH); //if statement is met then send high to pin 13
} else {
digitalWrite(redLED, LOW); //send low signal

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


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