#define servoPin 9
int goc=0;
int tong=0;
int thuan=1;
int nghich=0;
int sensor=2;
int motor=12;
int coi=13;
void setup ( ) {
pinMode (servoPin,OUTPUT);
pinMode (sensor,INPUT_PULLUP);
pinMode (coi,OUTPUT);
pinMode ( motor,OUTPUT);
}
void loop ( ) {
if(digitalRead(sensor)==LOW)
{
digitalWrite(coi, HIGH);
digitalWrite(motor,HIGH);
delay(5000);
}
digitalWrite(coi, LOW);
digitalWrite(motor,LOW);
digitalWrite(servoPin, HIGH);
delayMicroseconds ( goc ) ;
digitalWrite(servoPin, LOW);
delayMicroseconds ( tong ) ;
if (goc>=2300){thuan=0;nghich=1;}
if (goc<=600){thuan=1;nghich=0;}
if( thuan==1){goc++;}
if(nghich==1){goc--;}
tong=20000-goc;
} |