/*

pin assignments:

10   - base of npn transistor #1 2n2222 (either one)

11  - base of npn transistor #2 2n2222 (the other one…)

*/


#include <Servo.h> 


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

// a maximum of eight servo objects can be created 


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



int t1 = 10;


int t2 = 11;


void setup(){

myservo.attach(9); // attaches the servo on pin 9 to the servo object 

pinMode(t1, OUTPUT);

pinMode(t2, OUTPUT);

off();

delay(1000);

}


void loop(){

forward();

delay(10000);

reverse();

delay(10000);

off();

delay(10000);

clockwise();

counterclockwise();

servo_90();

delay(5000);

servo_180();

delay(5000);

servo_0();

delay(5000);

}



void forward() {

digitalWrite(t1, HIGH);

digitalWrite(t2, LOW);

}


void reverse(){

digitalWrite(t1, LOW);

digitalWrite(t2, HIGH);

}



void off() {

analogWrite(t1, 0);

digitalWrite(t2, HIGH);

}


void clockwise() {

for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees 

{ // in steps of 1 degree 

myservo.write(pos); // tell servo to go to position in variable 'pos' 

delay(15); // waits 15ms for the servo to reach the position 

} 


}


void counterclockwise() {


for(pos = 180; 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 

}


}



void servo_90 () {


myservo.write(90); // tell servo where to go

delay(250); // waits 250ms for the servo to reach the position 


}


void servo_180() {


myservo.write(180); // tell servo where to go

delay(250); // waits 250ms for the servo to reach the position 


}


void servo_0 () {



myservo.write(0); // tell servo where to go

delay(250); // waits 250ms for the servo to reach the position 


}


 

/*

NOTICE THAT WE BRING THE SERVO BACK TO THE POSITION WHERE THE NEXT FOR LOOP STARTS TO

AVOID JERKY MOTION

*/