Image

Homework 03 -Button-controlled Servo- Dennis MacAvaney

31 Oct

Homework 3 -Button-controlled Servo- Dennis MacAvaney

#include

Servo myservo;

int button1=5;
int button2=6;
int val=0;
int valB1=0;
int valB2=0;
int valNet=0;

void setup()
{
myservo.attach(9);
pinMode(button2, INPUT);
pinMode(button1, INPUT);
}

void loop()
{
if(digitalRead(button1)==HIGH){//incriment valB1 whenever Arduino reads B1 is pushed
valB1++;
}else{valB1–;}

if(digitalRead(button2)==HIGH){//reverse whenever Arduino reads B1 is pushed
valB2++;
}else{valB2–;}

valNet=(valB1-valB2);//determines the net movement of the servo
if(valNet>179){//caps net value at 179 and 0
valNet=179;}
if(valNet<0){
valNet=0;}
valNet = map(valNet, 0, 179, 0, 179);
myservo.write(valNet);//sends servo resulting valNet
delay(10);

}

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