ultrasonic sensor with servo motor using fuzzy method

hi guys,i’n having problem with my project.basically i want to make servo turn 180degree if obstacle close to ultrasonic, about 90 degree when it is medium close and do nothing when obstacle is far…
but i’m having problem when in medium close condition, the servo wont react to the ultrasonic measurement
here’s my program code

/* 
  Ultrasonic program with 2 ping sensor and one hc-sr04
  Output on servo motor Hitec HC-422
  */
  
 #define trigPinB 13
 #define echoPinB 12
 const int pingLeftB = 11;
 const int pingRightB = 10;
 int servo = 5;
 long durbackleft, durbackright, durbackmid, disbackleft, disbackright, disbackmid;
 long closebl,closebm,closebr,mediumbl,mediumbm,mediumbr,farbl,farbm,farbr;

 
 void setup()
 {
   pinMode (trigPinB, OUTPUT);
   pinMode (echoPinB, INPUT);
   pinMode (servo, OUTPUT);
 }
 
  void loop()
 {
    
 // Back Left Sensor (Ping)
 
   pinMode(pingLeftB, OUTPUT);
   digitalWrite(pingLeftB, LOW);
   delayMicroseconds(2);
   digitalWrite(pingLeftB, HIGH);
   delayMicroseconds(5);
   digitalWrite(pingLeftB, LOW);
   pinMode(pingLeftB, INPUT);
   durbackleft = pulseIn(pingLeftB, HIGH);
   disbackleft = (durbackleft/2) /29.1;
   
 // Back Right Sensor (Ping)
 
   pinMode(pingRightB, OUTPUT);
   digitalWrite(pingRightB, LOW);
   delayMicroseconds(2);
   digitalWrite(pingRightB, HIGH);
   delayMicroseconds(5);
   digitalWrite(pingRightB, LOW);
   pinMode(pingRightB, INPUT);
   durbackright = pulseIn(pingRightB, HIGH);
   disbackright = (durbackright/2) /29.1;
 
 // Back Middle Sensor (HC-SR04)
 
   digitalWrite (trigPinB, HIGH);
   delayMicroseconds(100);
   digitalWrite (trigPinB, LOW);
   durbackmid = pulseIn(echoPinB, HIGH);
   disbackmid = (durbackmid/2) / 29.1;
   
   closebl  = disbackleft < 3;
   mediumbl  = disbackleft >= 3 && disbackleft < 10;
   farbl  = disbackleft > 10;
   
   closebm  = disbackmid < 3;
   mediumbm  = disbackmid >= 3 && disbackleft < 10;
   farbm  = disbackmid > 10;
   
   closebr  = disbackright < 3;
   mediumbr  = disbackright >= 3 && disbackleft < 10;
   farbr  = disbackright > 10;
   
   
   if (disbackleft == closebl || disbackmid == closebm || disbackright == closebr)
   {
     digitalWrite (servo, HIGH);
     delayMicroseconds (2500);
     digitalWrite (servo, LOW);
     delay(20);
   }
   
   else if (disbackleft == mediumbl || disbackmid == mediumbm || disbackright == mediumbr)
   {
     digitalWrite (servo, HIGH);
     delayMicroseconds (1300);
     digitalWrite (servo, LOW);
     delay(20);
   }
   
   else if (disbackleft == mediumbl && disbackmid == mediumbm && disbackright == farbr)
   {
     digitalWrite (servo, HIGH);
     delayMicroseconds (1300);
     digitalWrite (servo, LOW);
     delay(20);
   }
   
   else if (disbackleft == closebl && disbackmid == closebm && disbackright == farbr)
   {
     digitalWrite (servo, HIGH);
     delayMicroseconds (2500);
     digitalWrite (servo, LOW);
     delay(20);
   }
   
    else if (disbackleft == farbl && disbackmid == mediumbm && disbackright == mediumbr)
   {
     digitalWrite (servo, HIGH);
     delayMicroseconds (1300);
     digitalWrite (servo, LOW);
     delay(20);
   }
   
    else if (disbackleft == farbl && disbackmid == closebm && disbackright == closebr)
   {
     digitalWrite (servo, HIGH);
     delayMicroseconds (2500);
     digitalWrite (servo, LOW);
     delay(20);
   }
   
   else
   {
     digitalWrite (servo, HIGH);
     delayMicroseconds (800);
     digitalWrite (servo, LOW);
     delay(20);
   }
   delay(10);
 }

please help me on this problem…i think my program is already correct but did i miss something or anything?
thx btw

i think my program is already correct

Correct, maybe, but too long.

Can you see that

pinMode(pingLeftB, OUTPUT);
   digitalWrite(pingLeftB, LOW);
   delayMicroseconds(2);
   digitalWrite(pingLeftB, HIGH);
   delayMicroseconds(5);
   digitalWrite(pingLeftB, LOW);
   pinMode(pingLeftB, INPUT);
   durbackleft = pulseIn(pingLeftB, HIGH);
   disbackleft = (durbackleft/2) /29.1;

looks an awful lot like

   pinMode(pingRightB, OUTPUT);
   digitalWrite(pingRightB, LOW);
   delayMicroseconds(2);
   digitalWrite(pingRightB, HIGH);
   delayMicroseconds(5);
   digitalWrite(pingRightB, LOW);
   pinMode(pingRightB, INPUT);
   durbackright = pulseIn(pingRightB, HIGH);
   disbackright = (durbackright/2) /29.1;

looks an awful lot like "mid"? Reduce the size of your code, and give the bugs fewer dusty corners to hide in.

It may be that at short range, you're receiving echoes from the previous output pulses. Try reducing the ping rate.

   if (disbackleft == closebl || disbackmid == closebm || disbackright == closebr)

closebl was set to disbackleft < 3, which is either true or false. disbackledt is a distance. It is unlikely that they will ever be equal.

The other close values are also 0 or 1, so it is unlikely that any of the equality tests will ever be true.

   else if (disbackleft == mediumbl || disbackmid == mediumbm || disbackright == mediumbr)

Ditto for the medium and far values.

What are you trying to do?

AWOL:

i think my program is already correct

Correct, maybe, but too long.

Can you see that

pinMode(pingLeftB, OUTPUT);
   digitalWrite(pingLeftB, LOW);
   delayMicroseconds(2);
   digitalWrite(pingLeftB, HIGH);
   delayMicroseconds(5);
   digitalWrite(pingLeftB, LOW);
   pinMode(pingLeftB, INPUT);
   durbackleft = pulseIn(pingLeftB, HIGH);
   disbackleft = (durbackleft/2) /29.1;

looks an awful lot like

   pinMode(pingRightB, OUTPUT);
   digitalWrite(pingRightB, LOW);
   delayMicroseconds(2);
   digitalWrite(pingRightB, HIGH);
   delayMicroseconds(5);
   digitalWrite(pingRightB, LOW);
   pinMode(pingRightB, INPUT);
   durbackright = pulseIn(pingRightB, HIGH);
   disbackright = (durbackright/2) /29.1;

looks an awful lot like "mid"? Reduce the size of your code, and give the bugs fewer dusty corners to hide in.

It may be that at short range, you're receiving echoes from the previous output pulses. Try reducing the ping rate.

thx 4 ur reply..but from what i see, the code that ur said need to be simplified is a control code right? i can't see anything that can be reduced..about the ping rate i will try to reduce it

PaulS:

   if (disbackleft == closebl || disbackmid == closebm || disbackright == closebr)

closebl was set to disbackleft < 3, which is either true or false. disbackledt is a distance. It is unlikely that they will ever be equal.

The other close values are also 0 or 1, so it is unlikely that any of the equality tests will ever be true.

   else if (disbackleft == mediumbl || disbackmid == mediumbm || disbackright == mediumbr)

Ditto for the medium and far values.

What are you trying to do?

hi,thx 4 ur reply.from my point of view, i tried to give a range and not the exact value.but, when i measure the value when it in <3, it indeed less than 3 cm.

thx 4 ur reply…but from what i see, the code that ur said need to be simplified is a control code right?
i can’t see anything that can be reduced

You abbreviate “thanks” and “your”, but can’t see that that your code could be factored-down further?

See where this

long pingRange (byte pingPin)
{
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(5);
   digitalWrite(pingPin, LOW);
   pinMode(pingPin, INPUT);
   return (pulseIn(pingPin, HIGH) / 2) 29.1;
}

takes you

closebl  = disbackleft < 3;

Using 32 bits to store one bit of information isn’t going to get you far in a memory-constrained architecture.

Fuzzy logic is for fuzzy thinkers. The Arduino doesn't need/use/understand fuzzy logic.

i tried to give a range and not the exact value.but, when i measure the value when it in <3, it indeed less than 3 cm.

I think you misunderstood PaulS’s remark.

if (disbackleft == closebl

“disbackleft” is a long and can have any value, but “closebl” is (should be) a boolean, and can only hold the value one or zero.
There are therefore only two values of “disbackleft” for which the test is true, both of which are less than 3.