#include <Servo.h>
Servo servoBin;
Servo servoToll;
int angle = 10;
// defines pins numbers
const int trigBinPin = 12;
const int echoBinPin = 11;
const int trigTollPin = 10;
const int echoTollPin = 13;
// defines variables
long durationServoBin;
long durationServoToll;
int distanceBin;
int distanceToll;
void setup() {
servoBin.attach(8);
servoBin.write(angle);
pinMode(trigBinPin, OUTPUT); // Sets the trigBinPin as an Output
pinMode(echoBinPin, INPUT); // Sets the echoBinPin as an Input
servoToll.attach(9);
servoToll.write(angle);
pinMode(trigTollPin, OUTPUT); // Sets the trigTollPin as an Output
pinMode(echoTollPin, INPUT); // Sets the echoTollPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigBinPin
digitalWrite(trigBinPin, LOW);
delayMicroseconds(2);
// Sets the trigBinPin on HIGH state for 10 micro seconds
digitalWrite(trigBinPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigBinPin, LOW);
// Reads the echoBinPin, returns the sound wave travel time in microseconds
durationServoBin = pulseIn(echoBinPin, HIGH);
// Calculating the distanceBin
distanceBin= durationServoBin*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance Bin: ");
Serial.println(distanceBin);
delay(100);
if(distanceBin<20)
{
servoBin.write(180);
}
else
{
servoBin.write(70);
}
// Clears the trigTollPin
digitalWrite(trigTollPin, LOW);
delayMicroseconds(2);
// Sets the trigTollPin on HIGH state for 10 micro seconds
digitalWrite(trigTollPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigTollPin, LOW);
// Reads the echoTollPin, returns the sound wave travel time in microseconds
durationServoToll = pulseIn(echoTollPin, HIGH);
// Calculating the distanceToll
distanceToll= durationServoToll*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance Toll: ");
Serial.println(distanceToll);
delay(100);
if(distanceToll<20)
{
servoToll.write(180);
}
else
{
servoToll.write(70);
}
}