#include <Servo.h>
#define buttonc 11
#define buttona 2
#define buttonb 1
#define PIN_TRIG 13
#define PIN_ECHO 12
Servo myservo;
int pos = 0;
int statusx;
int statusy;
int statusz;
int leda = 4;
int ledb = 7;
int ledc = 10;
int start = 0;
void setup()
{
Serial.begin(115200);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
myservo.attach(9);
pinMode(buttona, INPUT_PULLUP);
myservo.attach(9);
pinMode(buttonb, INPUT_PULLUP);
pinMode(buttonc, INPUT_PULLUP);
pinMode(leda, OUTPUT);
pinMode(ledb, OUTPUT);
myservo.write(0);
}
void loop()
{
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
int duration = pulseIn(PIN_ECHO, HIGH);
Serial.print("Distance in CM: ");
Serial.println(duration / 58);
duration = duration / 58;
delay(10);
statusx = digitalRead(buttona);
if(statusx == 0)
{
digitalWrite(leda, 1);
myservo.write(90);
delay (1000);
digitalWrite(leda, 0);
myservo.write(0);
delay (10);
}
statusy = digitalRead(buttonb);
if(statusy == 0)
{
digitalWrite(ledb, 1);
myservo.write(180);
delay (1000);
digitalWrite(ledb, 0);
myservo.write(0);
delay (10);
}
if( start == 0 && duration <= 100)
{
digitalWrite(ledc, 1);
myservo.write(180);
delay (1000);
digitalWrite(ledc, 0);
myservo.write(0);
delay (statusz);
start = 1;
}
if(duration >=100)
{
myservo.write(0);
digitalWrite(ledc, 0);
}
statusz = digitalRead(buttonc);
if(statusz == 0)
{
start = 0;
}
}