Servos not reacting to ultrasonic commands

Im making an automatic fuel selector system for three tanks.Am using ultrasonic sensors as level sensors and servos to control the taps(open and close).Left tank and Right tank tank should always maintain balance or equal levels of fuel while Front tank must only come in when Left and right tanks fuel levels are greator than 18 cm.
If left and right tank levels are >18 servo opens tap on the front tank.
If all tank levels are >18(fuel fully depleted) all servos write zero to close all taps.
If right tank level is greator than left tank level,servo on right tank opens tap.
If left tank level is greator than right tank level,servo on left tank opens tap.
If both left and right tank levels equal,both servos on both tanks write 90 to open taps.

So the servos are not reacting to commands.

below is the code

#include <Servo.h>
#define trigPin1 3
#define echoPin1 2
#define trigPin2 4
#define echoPin2 5
#define trigPin3 7
#define echoPin3 8

#define PinRR A0
#define PinRG A1
#define PinLR A2
#define PinLG A3
#define PinFR A4
#define PinFG A5

int buzzer = 1;

Servo myservo1;
Servo myservo2;
Servo myservo3;

long duration1,duration2,duration3, distance1,distance2,distance3, RightSensor, FrontSensor, LeftSensor;
int potValue;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  pinMode(PinRR, OUTPUT);
  pinMode(PinRG, OUTPUT);
  pinMode(PinLR, OUTPUT);
  pinMode(PinLG, OUTPUT);
  pinMode(PinFR, OUTPUT);
  pinMode(PinFG, OUTPUT);
  pinMode(buzzer, OUTPUT);
  myservo1.attach(6);
  myservo2.attach(10);
  myservo3.attach(11);

}

void loop() {
  digitalWrite(buzzer, HIGH);
  potValue = distance1, distance2, distance3;
  
  potValue = map(potValue, 0, 1023, 0, 179);


  SonarSensor(trigPin1, echoPin1);
  RightSensor = distance1;
  SonarSensor(trigPin2, echoPin2);
  LeftSensor = distance2;
  SonarSensor(trigPin3, echoPin3);
  FrontSensor = distance3;

  Serial.print(LeftSensor);
  Serial.print(" - ");
  Serial.print(FrontSensor);
  Serial.print(" - ");
  Serial.println(RightSensor);

  if (distance1 > distance2 )
  {
    myservo1.write(90);
    digitalWrite(PinRR, HIGH);

    digitalWrite(PinRG, LOW);
  }
    else if ( distance2 > distance1)
    {
      myservo2.write(90);
      digitalWrite(PinLR, HIGH);

      digitalWrite(PinLG, LOW);
    }
    else if (distance1 = distance2)
    {
      myservo1.write(90);
      myservo2.write(90);
      digitalWrite(PinFR, HIGH);

      digitalWrite(PinFG, LOW);
    }
    else if ((distance1>=18 && distance2>=18)) {

      myservo1.write(0);
      myservo2.write(0);
      myservo3.write(90);
      digitalWrite(PinRR, LOW);

      digitalWrite(PinRG, HIGH);
    }
    else if (  distance3 >=18) {
      myservo3.write(0);
      digitalWrite(PinLR, LOW);

      digitalWrite(PinLG, HIGH);
    }
    else if ( (distance1>=18 && distance2>=18 && distance3>=18)) {
       myservo1.write(0);
      myservo2.write(0);
      myservo3.write(0);
      digitalWrite(buzzer, HIGH);
      digitalWrite(PinFR, LOW);

      digitalWrite(PinFG, HIGH);
    }

  }
  void SonarSensor(int trigPin, int echoPin)
  {
    digitalWrite(trigPin1, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin1, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin1, LOW);
    duration1= pulseIn(echoPin1, HIGH);
    distance1 = (duration1 / 2) / 29.1;
    digitalWrite(trigPin2, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin2, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin2, LOW);
    duration2= pulseIn(echoPin2, HIGH);
    distance2 = (duration2 / 2) / 29.1;
    digitalWrite(trigPin3, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin3, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin3, LOW);
    duration3= pulseIn(echoPin3, HIGH);
    distance3 = (duration3 / 2) / 29.1;


  }

potValue = distance1, distance2, distance3; What the Hell is that?

You pass the SonarSensor function a trigger and echo pin, then ignore those values, and use the globals instead..

SonarSensor should read and return the range for ONE sensor at a time.

Time for a rewrite

TheMemberFormerlyKnownAsAWOL:
potValue = distance1, distance2, distance3; What the Hell is that?

You pass the SonarSensor function a trigger and echo pin, then ignore those values, and use the globals instead..

SonarSensor should read and return the range for ONE sensor at a time.

Time for a rewrite

I'm using three diffferent ultrasonic sensors not single as u mean.

The SonarSensor function reads one sensor at a time. And you ignore the result indeed. And you repeat that three times, for the three sensors.

Also, the moment you fee the need to use numbers in variable names you're due to learn about arrays.

Ok, what about the comma operator question?

I'm using three diffferent ultrasonic sensors not single as u mean.

So why aren't you passing three sets of pins into the function?
And returning three values?
Oh. Wait.

else if (distance1 = distance2)Oops

You do know that a good way to find out what your code is doing, is to get it to tell you?
Edit: except you used the valuable serial port to drive a buzzer. Not a good use of resources.

Hi,
What model Arduino are you using?

Pins 0 and 1 are used for programming the Arduino, so please use other pins so that your code will run consistently.

And what are you trying to do here?

void SonarSensor(int trigPin, int echoPin)
  {
    digitalWrite(trigPin1, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin1, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin1, LOW);
    duration1= pulseIn(echoPin1, HIGH);
    distance1 = (duration1 / 2) / 29.1;
    digitalWrite(trigPin2, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin2, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin2, LOW);
    duration2= pulseIn(echoPin2, HIGH);
    distance2 = (duration2 / 2) / 29.1;
    digitalWrite(trigPin3, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin3, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin3, LOW);
    duration3= pulseIn(echoPin3, HIGH);
    distance3 = (duration3 / 2) / 29.1;
  }

Tom.... :slight_smile: