2 servo scann

Hi.
I m using this to pan a servo.

void scanObstacle ()
    {
      myservo.write(pos);                    // sets the servo position
      delay(aDelay);                         // waits for the servo to get there
      readDistance = analogRead(sensorPin);  // Take a reading from sensor
      delay(aDelay);                         // Wait to get the reading SOSOSOSOS !!!! We need this
      if (readDistance > minDistanceLimit)   // If we can scan an obstacle
      {
        avoidObstacle(pos);                  // Then do avoiding actions
      }

      if (pos == 180)                        // If we reach end of servo
       {
         servoStep= -sStep;                  // Move it back
       }
      if (pos == 0)                          // If we are at the start
      {
        servoStep = sStep;                   // Move it right
      }
     
      pos += servoStep;
    }

What i m trying to do is. to use a 2nd servo and
when the pan servo value is between (0 and 80 ) and ( 100 to 180 ) to myservo_vertical.write(90); <<---- the 2nd servo
and when is between 80 and 100 to
myservo_vertical.write(100);

How can i do this ??? What ever i try is not working :-[
Thnx in advance :wink:

Add this:

if(pos >= 0 && pos <= 80)
{
   myservo_vertical.write(90);
}
else if(pos > 80 && pos < 100)
{
   myservo_vertical.write(100);
}
else if(pos >= 100 && pos <= 180)
{
   myservo_vertical.write(90);
}

wherever in that function you want the vertical servo to move.

What do you want the vertical servo to do when pos > 80 and < 100?

Thnx PaulS for your reply.
..but i v added the your code and is NOR working :frowning:
whatever i v try .. the same results .. not working.

when the pan servo value is between (0 and 80 ) and ( 100 to 180 ) to myservo_vertical.write(90); <<---- the 2nd servo
and when is between 80 and 100 to
myservo_vertical.write(100);

I assume you mean "OR"?

if((pos >= 0 && pos <= 80) || 
            (pos >= 100 && pos <= 180)) {
   myservo_vertical.write(90);
}
else if(pos > 80 && pos < 100)
{
   myservo_vertical.write(100);
}

I assume you mean "OR"?

Yes ...
Same results as PaulS code..
The 1st servo is panning correctly but the 2nd is not working at all :frowning:
I thing that the part of code for the 2nd servo must be INSIDE the existent code something like
if (pos == 180) // If we reach end of servo
{
servoStep= -sStep; // Move it back
<<<<---Here to add somehow the code for the 2nd servo
}

Thnx for your reply

What i want to do is... the paning servo (1st) to scan for obstacles in front of my robot and the 2nd (vertical) to scan in two potitions . 180 degrees for Gap in front of my robot and 90 degrees for an obstacle in the front of my robor. The final goal is to compare the 2 readings from the 2 front results in order to descide if the obstacle is tall enough (both sensors detect obstacle) and in this case to turn my robot OR if the lower (2nd) sensor detects an obstacle but the 1st is not , then the obstacle is "small" and my robot is able to climbe it.

We're just guessing here without seeing where in the function you added the new code. Time to post the code again.

  void scanObstacle ()
    {
      myservo_pan.write(pos);                           // sets the servo position
      myservo_vertical.write(90);                       // sets the servo position @ 90 degree
      delay(aDelay);                                    // waits for the servo to get there
      
      readDistance = analogRead(sensorPin);             // Take a reading from sensor
      readDistance90 = analogRead(Vertical_sensorPin);  // Take a reading from Vertical sensor @90 degree and store it      
      delay(aDelay);                                    // Wait to get the reading SOSOSOSOS !!!! We need this     
      
      if (readDistance > minDistanceLimit)              // If we can scan an obstacle
      {
        avoidObstacle(pos);                  // Then do avoiding actions
      }

<<<<--------------------------------------------------------------------------- i v test to paste the code here also .Same results
      if (pos == 180)                        // If we reach end of servo
       {         
         servoStep= -sStep;                  // Move it back
       }
      if (pos == 0)                          // If we are at the start
      {        
        servoStep = sStep;                   // Move it right
      }
      
       if((pos >= 0 && pos <= 80) || (pos >= 100 && pos <= 180)) 
      {
          myservo_vertical.write(90);
      }
      else if(pos > 80 && pos < 100)
      {
          myservo_vertical.write(180);
      } 
     
      pos += servoStep;
      
     <<<<--------------------------------------------------------------------------- i v test to paste the code here also .Same results
     
    }

Right after the curly brace at the start of the function, add this:

Serial.println("scanObstacle ==>");

Just before this line:

       if((pos >= 0 && pos <= 80) || (pos >= 100 && pos <= 180))

Add this:

Serial.print("pos = ");
Serial.println(pos, DEC);

Right before the curly brace at the end of the function, add this:

Serial.println("scanObstacle <==");

Let us know what gets shown in the serial monitor window.

scanObstacle ==>
pos = 0
scanObstacle <==
scanObstacle ==>
pos = 10
scanObstacle <==
scanObstacle ==>
pos = 20
scanObstacle <==
scanObstacle ==>
pos = 30
scanObstacle <==
scanObstacle ==>
pos = 40
scanObstacle <==
scanObstacle ==>
pos = 50
scanObstacle <==
scanObstacle ==>
pos = 60
scanObstacle <==
scanObstacle ==>
pos = 70
scanObstacle <==
scanObstacle ==>
pos = 80
scanObstacle <==
scanObstacle ==>
pos = 90
scanObstacle <==
scanObstacle ==>
pos = 100
scanObstacle <==
scanObstacle ==>
pos = 110
scanObstacle <==
scanObstacle ==>
pos = 120
scanObstacle <==
scanObstacle ==>
pos = 130
scanObstacle <==
scanObstacle ==>
pos = 140
scanObstacle <==
scanObstacle ==>
pos = 150
scanObstacle <==
scanObstacle ==>
pos = 160
scanObstacle <==
scanObstacle ==>
pos = 170
scanObstacle <==
scanObstacle ==>
pos = 180
scanObstacle <==
scanObstacle ==>
pos = 170
scanObstacle <==
scanObstacle ==>
pos = 160
scanObstacle <==
scanObstacle ==>
pos = 150
scanObstacle <==
scanObstacle ==>
pos = 140
scanObstacle <==
scanObstacle ==>
pos = 130
scanObstacle <==
scanObstacle ==>
pos = 120
scanObstacle <==
scanObstacle ==>
pos = 110
scanObstacle <==
scanObstacle ==>
pos = 100
scanObstacle <==
scanObstacle ==>
pos = 90
scanObstacle <==
scanObstacle ==>
pos = 80
scanObstacle <==
scanObstacle ==>
pos = 70
scanObstacle <==
scanObstacle ==>
pos = 60
scanObstacle <==
scanObstacle ==>
pos = 50
scanObstacle <==
scanObstacle ==>
pos = 40
scanObstacle <==
scanObstacle ==>
pos = 30
scanObstacle <==
scanObstacle ==>
pos = 20
scanObstacle <==
scanObstacle ==>
pos = 10
scanObstacle <==
scanObstacle ==>
pos = 0

.......... and again from the begining...
Iv added also in setup Serial.begin(19200);

And, the vertical servo doesn't move?

Add two more print statements:

      if((pos >= 0 && pos <= 80) || (pos >= 100 && pos <= 180))
      {
[glow]          Serial.println("Move vertical servo to 90");
[/glow]          myservo_vertical.write(90);
      }
      else if(pos > 80 && pos < 100)
      {
[glow]          Serial.println("Move vertical servo to 1800");
[/glow]          myservo_vertical.write(180);
      }

One thing I noticed is that you are stepping by 10. So, the only value of pos that should result in the vertical servo moving is 90. Try changing the vertical servo code to this:

      if((pos >= 0 && pos [glow]<[/glow] 80) || (pos [glow]>[/glow] 100 && pos <= 180))
      {
[glow]          Serial.println("Move vertical servo to 90");
[/glow]          myservo_vertical.write(90);
      }
      else if(pos >[glow]=[/glow] 80 && pos <[glow]=[/glow] 100)
      {
[glow]          Serial.println("Move vertical servo to 1800");
[/glow]          myservo_vertical.write(180);
      }

Hmmmmm..... seems that is working . im taking this

scanObstacle ==>
pos = 0
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 10
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 20
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 30
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 40
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 50
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 60
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 70
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 80
Move vertical servo to 180
scanObstacle <==
scanObstacle ==>
pos = 90
Move vertical servo to 180
scanObstacle <==
scanObstacle ==>
pos = 100
Move vertical servo to 180
scanObstacle <==
scanObstacle ==>
pos = 110
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 120
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 130
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 140
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 150
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 160
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 170
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 180
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 170
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 160
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 150
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 140
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 130
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 120
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 110
Move vertical servo to 90
scanObstacle <==
scanObstacle ==>
pos = 100
Move vertical servo to 180
scanObstacle <==
scanObstacle ==>
pos = 90
Move vertical servo to 180
scanObstacle <==

and i v modify

if((pos >= 0 && pos < 80) || (pos > 100 && pos <= 180))
      {
          Serial.println("Move vertical servo to 90");
          myservo_vertical.write(90);
      }
      else if(pos >= 89 && pos <= 91) <<<------------- only one step for 180 degree
      {
          Serial.println("Move vertical servo to 180");
          myservo_vertical.write(180);
            delay(500);  <<<<<<<---------------------------added this delay
      }

and now WORKING just fine.. as expected :slight_smile:

Seems that was working from the first time , both 2 modifications , but due to small delay
int aDelay = 30; // Delay for the servo and let the IR to return results
the 2nd servo was not moving at all
I m so sorry.. its my fault :-[
From now , I must use the print statements that helps a lot to see what goes wrong.

Thnx for your help :slight_smile:

The important thing is that you learned something.

You need to take a look at the if/else block that moves the vertical servo, though. There is no action defined when pos = 80 to 89 or when pos = 92 to 100. Even if the step size remains 10, pos = 80 and pos = 100 do not have defined actions.