Image

Homework 04 – Combine Circuits – Arthur Dunne

4 Nov

Homework 04 - Combine Circuits - Arthur Dunne

I integrated three components into this circuit. The first one is a push button that toggles a red LED on and off. The second component uses the same push button, which momentarily turns on the green LED for as long as the button is held down. The third component uses the push button to activate the servo motor, which turns it forward 180 degrees and backward 180 degrees and then stops.

#include “Servo.h”

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

int pos = 0; // variable to store the servo position

int LED = 13; //set LED to pin 13
int Button = 7; //set button to pin 7
int previous_value = 0; // previous button state
int state = 0; // determines on and off
int value = 0; // current button state
int doServoOnce = 0; // activates servo motor

void setup() {
pinMode(LED, OUTPUT); // set pin 13 to output
pinMode(Button, INPUT); // set pin7 to input
myservo.attach(9); // attaches the servo on pin 9 to the servo object

}
void loop() { //begin loop

// 1) get the button state

value = digitalRead(Button); //set val to reading at pin7
if ((value == HIGH) && (previous_value == LOW)) { //only meets argument if previous readings
state = 1 – state; //switches state between 0 and 1 if above argument is met
delay(20); //delay to avoid signal bounce
if (state == 1) {
doServoOnce = 1;
}
}

// 2) activates red LED

previous_value = value; //set previous value to value
if (state == 1) {// do something if state is equal to 1
digitalWrite(LED, HIGH);
}
else {
digitalWrite(LED, LOW);
}

// 3) activates the servo motor once

if (state == 1 && doServoOnce == 1) { // moves the servo motor if the state is equal to 1

for(pos = 0; pos =1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
doServoOnce = 0; // turns servo motor off
}
}

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