servo motor and ultrasonic sensor problem

after i changing the angle of the servo
my ultrasonic sensor is not returning any value .
can u help me?

#include <TimerOne.h>
#include <Servo.h>
Servo myservo;

int bottun=10;
int trigPin = 11;    //Trig - green Jumper
int echoPin = 12;    //Echo - yellow Jumper

int speedr=2;
int r1=3;
int r2=4;

int speedl=5;
int l1=6;
int l2=7;

long duration, cm, inches;
int pos=0;
int rdis,ldis;
   //=========================setup==========================
     void setup() 
      {myservo.attach(8);
       myservo.write(90);
      delay(200);
       myservo.detach(); 
        //motor
         pinMode(speedr,OUTPUT);
         pinMode(speedl,OUTPUT);
         pinMode(r1, OUTPUT);
         pinMode(r2, OUTPUT);
         pinMode(l1, OUTPUT);
         pinMode(l2, OUTPUT);
        // put your setup code here, to run once:
        //Serial Port begin
       Serial.begin (9600);
       //Define inputs and outputs
        pinMode(trigPin, OUTPUT);
        pinMode(echoPin, INPUT);

      
        pinMode(bottun,INPUT);
      Timer1.initialize(20000);
      Timer1.attachInterrupt(updateSensor);
   
     }
     void updateSensor()//background updating of sensors
     { 
 myservo.detach(); 
             int cm;
         cm=dis();  
        
         
    }
//================loop=========================     
void loop() 
{delay(200);

while(cm>20)  
  Go(255,255);
S_Stop();
 myservo.attach(8); 
delay(200);
myservo.write(0);
delay(400);
rdis=dis();
myservo.write(180);
delay(400);
ldis=dis();
myservo.write(90);
delay(400);
 myservo.detach(); 
 delay(50);
Serial.print(rdis+" "+ldis);
if(rdis>=ldis)
while(dis()<20)
Right(255,255);
else
while(dis()<20)
Left(255,255);

T_Stop();
 
}










//============funcions=========================
/*void wall()
{if(turn()==0)
while(cm<30)
Right(255,255);

if(turn()==1)
while(cm<30)
Left(255,255);



}

*/

int dis()//convert from sr-04 to cm

{
   
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  // convert the time into a distance
  cm = (duration/2) / 29.1;
  inches = (duration/2) / 74; 
 
  return cm;
}
  
  void S_Stop()
  {
    
   analogWrite(speedr,0);  
   analogWrite(speedl,0);
     
    digitalWrite(r1,HIGH); 
   digitalWrite(r2,HIGH);      
    digitalWrite(l1,HIGH); 
   digitalWrite(l2,HIGH); 
  }
    void T_Stop()
  {
   int x=1;
   
   analogWrite(speedr,0);  
   analogWrite(speedl,0);
     
    digitalWrite(r1,HIGH); 
   digitalWrite(r2,HIGH);      
    digitalWrite(l1,HIGH); 
   digitalWrite(l2,HIGH); 
  
while(x==1)
delay(10);
  }
  
  
  void Go(int l,int r)
  {
   analogWrite(speedr,255); 
     analogWrite(speedl,255);
     
    digitalWrite(r1,HIGH); 
   digitalWrite(r2,LOW); 
  
   
   digitalWrite(l1,HIGH); 
   digitalWrite(l2,LOW);  
  }
 
  void GoBack(int l,int r)
  {
   analogWrite(speedr,255); 
    digitalWrite(r2,HIGH); 
   digitalWrite(r1,LOW); 
  
   analogWrite(speedl,255);
   digitalWrite(l2,HIGH); 
   digitalWrite(l1,LOW);  
  }
  
  void Right(int l,int r)
  {
   analogWrite(speedr,255); 
    digitalWrite(r2,HIGH); 
   digitalWrite(r1,LOW); 
  
   analogWrite(speedl,255);
   digitalWrite(l1,HIGH); 
   digitalWrite(l2,LOW);  
  }
 
  void Left(int l,int r)
  {
   analogWrite(speedr,255); 
    digitalWrite(r2,HIGH); 
   digitalWrite(r1,LOW); 
  
   analogWrite(speedl,255);
   digitalWrite(l2,HIGH); 
   digitalWrite(l1,LOW);  
  }
/* int turn ()
 {
  int x=0;
h.write(55);
if(cm>40)
{  h.write(90);
  return 0;
}
h.write(115);
if(cm>40)
{  h.write(90);
  return 1;
}


 }

  
 
 void see()
  {while(cm<50){
   for (pos = 55; pos <= 115; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    h.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 115; pos >= 55; pos -= 1) { // goes from 180 degrees to 0 degrees
    h.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }}}
  */

The layout of your code is just too difficult to read. Nothing goes after the { on the same line.

Use Tools + Auto Format to fix you horrid indenting.

Why are you detaching the servo?

  cm = (duration/2) / 29.1;
  inches = (duration/2) / 74;

Can't make up your mind which system of units to use?

  return cm;

Why do you need to return a global variable? In other words, why is cm global? It should NOT be.

cm is int .
changed to this ,still not working

#include <TimerOne.h>
#include <Servo.h>
Servo myservo;

int bottun = 10;
int trigPin = 11;    //Trig - green Jumper
int echoPin = 12;    //Echo - yellow Jumper

int speedr = 2;
int r1 = 3;
int r2 = 4;

int speedl = 5;
int l1 = 6;
int l2 = 7;

long duration, cm, inches;
int pos = 0;
int rdis, ldis;
//=========================setup==========================
void setup()
{ myservo.attach(8);
  myservo.write(90);
  delay(200);
 // myservo.detach();
  //motor
  pinMode(speedr, OUTPUT);
  pinMode(speedl, OUTPUT);
  pinMode(r1, OUTPUT);
  pinMode(r2, OUTPUT);
  pinMode(l1, OUTPUT);
  pinMode(l2, OUTPUT);
  // put your setup code here, to run once:
  //Serial Port begin
  Serial.begin (9600);
  //Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);


  pinMode(bottun, INPUT);
  Timer1.initialize(20000);
  Timer1.attachInterrupt(updateSensor);

}
void updateSensor()//background updating of sensors
{
  myservo.detach();
  int cm;
  cm = dis();


}
//================loop=========================
void loop()
{ delay(200);

  while (cm > 20)
    Go(255, 255);
  S_Stop();
  myservo.attach(8);
  delay(200);
  myservo.write(0);
  delay(400);
  rdis = dis();
  myservo.write(180);
  delay(400);
  ldis = dis();
  myservo.write(90);
  delay(400);
  //myservo.detach();
  delay(50);
  Serial.print(rdis + " " + ldis);
  if (rdis >= ldis)
    while (dis() < 20)
      Right(255, 255);
  else
    while (dis() < 20)
      Left(255, 255);

  T_Stop();

}










//============funcions=========================
/*void wall()
  {if(turn()==0)
  while(cm<30)
  Right(255,255);

  if(turn()==1)
  while(cm<30)
  Left(255,255);



  }

*/

int dis()//convert from sr-04 to cm

{

  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  // convert the time into a distance
  cm = (duration / 2) / 29.1;
  inches = (duration / 2) / 74;

  return cm;
}

void S_Stop()
{

  analogWrite(speedr, 0);
  analogWrite(speedl, 0);

  digitalWrite(r1, HIGH);
  digitalWrite(r2, HIGH);
  digitalWrite(l1, HIGH);
  digitalWrite(l2, HIGH);
}
void T_Stop()
{
  int x = 1;

  analogWrite(speedr, 0);
  analogWrite(speedl, 0);

  digitalWrite(r1, HIGH);
  digitalWrite(r2, HIGH);
  digitalWrite(l1, HIGH);
  digitalWrite(l2, HIGH);

  while (x == 1)
    delay(10);
}


void Go(int l, int r)
{
  analogWrite(speedr, 255);
  analogWrite(speedl, 255);

  digitalWrite(r1, HIGH);
  digitalWrite(r2, LOW);


  digitalWrite(l1, HIGH);
  digitalWrite(l2, LOW);
}

void GoBack(int l, int r)
{
  analogWrite(speedr, 255);
  digitalWrite(r2, HIGH);
  digitalWrite(r1, LOW);

  analogWrite(speedl, 255);
  digitalWrite(l2, HIGH);
  digitalWrite(l1, LOW);
}

void Right(int l, int r)
{
  analogWrite(speedr, 255);
  digitalWrite(r2, HIGH);
  digitalWrite(r1, LOW);

  analogWrite(speedl, 255);
  digitalWrite(l1, HIGH);
  digitalWrite(l2, LOW);
}

void Left(int l, int r)
{
  analogWrite(speedr, 255);
  digitalWrite(r2, HIGH);
  digitalWrite(r1, LOW);

  analogWrite(speedl, 255);
  digitalWrite(l2, HIGH);
  digitalWrite(l1, LOW);
}
/* int turn ()
  {
  int x=0;
  h.write(55);
  if(cm>40)
  {  h.write(90);
  return 0;
  }
  h.write(115);
  if(cm>40)
  {  h.write(90);
  return 1;
  }


  }



  void see()
  {while(cm<50){
   for (pos = 55; pos <= 115; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    h.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 115; pos >= 55; pos -= 1) { // goes from 180 degrees to 0 degrees
    h.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }}}
*/
void updateSensor()//background updating of sensors
{
  myservo.detach();
  int cm;
  cm = dis();


}

Why do you bother reading a value into “cm” and then discarding it?
Why not use the global “cm” ?

Edit: Which is the value you returned from dis() anyway :frowning:

BTW

not working

is about the single most hated phrase here.

{ myservo.attach(8);

Apparently, you seem to think that "NOTHING follows the {" doesn't apply to you. IT DOES!

If you don't detach the servo, you don't need to keep attaching it.

The code does something. You have some expectations about what it should do. "It doesn't work" doesn't tell us squat.

dis() not returning value after the move of the servo

#include <TimerOne.h>
#include <Servo.h>
Servo myservo;

int bottun = 10;
int trigPin = 11;    //Trig - green Jumper
int echoPin = 12;    //Echo - yellow Jumper

int speedr = 2;
int r1 = 3;
int r2 = 4;

int speedl = 5;
int l1 = 6;
int l2 = 7;

long duration, cm, inches;
int pos = 0;
int rdis, ldis;
//=========================setup==========================
void setup()
{ myservo.attach(8);
  myservo.write(90);
  delay(200);
 // myservo.detach();
  //motor
  pinMode(speedr, OUTPUT);
  pinMode(speedl, OUTPUT);
  pinMode(r1, OUTPUT);
  pinMode(r2, OUTPUT);
  pinMode(l1, OUTPUT);
  pinMode(l2, OUTPUT);
  // put your setup code here, to run once:
  //Serial Port begin
  Serial.begin (9600);
  //Define inputs and outputs
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);


  pinMode(bottun, INPUT);
 // Timer1.initialize(20000);
//  Timer1.attachInterrupt(updateSensor);

}
void updateSensor()//background updating of sensors
{
  int cm;
  cm = dis();


}
//================loop=========================
void loop()
{ delay(200);

  while (dis() > 20)
    Go(255, 255);
  S_Stop();
  myservo.attach(8);
  delay(200);
  myservo.write(0);
  delay(400);
  rdis = dis();
  myservo.write(180);
  delay(400);
  ldis = dis();
  myservo.write(90);
  delay(400);
  myservo.detach();
  delay(50);
  Serial.print(rdis + " " + ldis);
  if (rdis >= ldis)
    while (dis() < 20)
      Right(255, 255);
  else
    while (dis() < 20)
      Left(255, 255);
  T_Stop();

}










//============funcions=========================
/*void wall()
  {if(turn()==0)
  while(cm<30)
  Right(255,255);

  if(turn()==1)
  while(cm<30)
  Left(255,255);



  }

*/

int dis()//convert from sr-04 to cm

{

  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  // convert the time into a distance
  cm = (duration / 2) / 29.1;
  inches = (duration / 2) / 74;

  return cm;
}

void S_Stop()
{

  analogWrite(speedr, 0);
  analogWrite(speedl, 0);

  digitalWrite(r1, HIGH);
  digitalWrite(r2, HIGH);
  digitalWrite(l1, HIGH);
  digitalWrite(l2, HIGH);
}
void T_Stop()
{
  int x = 1;

  analogWrite(speedr, 0);
  analogWrite(speedl, 0);

  digitalWrite(r1, HIGH);
  digitalWrite(r2, HIGH);
  digitalWrite(l1, HIGH);
  digitalWrite(l2, HIGH);

  while (x == 1)
    delay(10);
}


void Go(int l, int r)
{
  analogWrite(speedr, 255);
  analogWrite(speedl, 255);

  digitalWrite(r1, HIGH);
  digitalWrite(r2, LOW);


  digitalWrite(l1, HIGH);
  digitalWrite(l2, LOW);
}

void GoBack(int l, int r)
{
  analogWrite(speedr, 255);
  digitalWrite(r2, HIGH);
  digitalWrite(r1, LOW);

  analogWrite(speedl, 255);
  digitalWrite(l2, HIGH);
  digitalWrite(l1, LOW);
}

void Right(int l, int r)
{
  analogWrite(speedr, 255);
  digitalWrite(r2, HIGH);
  digitalWrite(r1, LOW);

  analogWrite(speedl, 255);
  digitalWrite(l1, HIGH);
  digitalWrite(l2, LOW);
}

void Left(int l, int r)
{
  analogWrite(speedr, 255);
  digitalWrite(r2, HIGH);
  digitalWrite(r1, LOW);

  analogWrite(speedl, 255);
  digitalWrite(l2, HIGH);
  digitalWrite(l1, LOW);
}
/* int turn ()
  {
  int x=0;
  h.write(55);
  if(cm>40)
  {  h.write(90);
  return 0;
  }
  h.write(115);
  if(cm>40)
  {  h.write(90);
  return 1;
  }


  }



  void see()
  {while(cm<50){
   for (pos = 55; pos <= 115; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    h.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 115; pos >= 55; pos -= 1) { // goes from 180 degrees to 0 degrees
    h.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }}}
*/
void updateSensor()//background updating of sensors
{
  (void)dis();


}

And now?

the problem is that when i using the timer1 library my servo just stop working, and when i using the servo- the sensor not working. how can i use they both ?

how can i use they both ?

You can't. There is a ServoTimer2 library...