Let's Make Robots!

Problem with sensor readings and servo jitter.

Hi, i am new to LMR and i dont whether this is the right forum for my question. So if i am in the wrong place, plz tell me where i should post this. I am having problems with my ping ultrasonic sensor readings going into the arduino because of the servo on which it is mounted. I added all the required caps and reduced a lot of noise, but still the sensor gives wrong readings. How can i make it work properly. 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Thx for ur help guys, i was able to fix the problem and when i get the bot runnig i will post the videos.

Bajdi's picture

You have an awful lot of delays in your code, try to use the millis function instead. Don't know if it is of much of much help but there is an unltrasonic library for Arduino, I use it with my SRF04 and SR06 ultrasound sensors. I have never had any problems with them. I got the library from here: http://iteadstudio.com/store/index.php?main_page=product_info&cPath=4&products_id=52

ChuckCrunch's picture

ping();

scanahead = duration/29/2;

 

 

i think that's what's going on your not calling the ping void or putting the correct ver in scanhead, scanleft , scanright , get a second opinion 

BotFIN's picture

Can you post the code and some pictures how you wired all things up?

I can post the code and the way i added the caps but i cant take a picture of my robot cause it is all awfully connected and messy.

This is my code here-

/* Robot which can sense obstacles and choose a free path and can detect

   edges and decide whether to go or not.

 */ 

 

#include <Servo.h>       // include servo library in code

 

Servo servo1;

Servo servo2;

const int pingPin = 9;     // Obstacle sense pin

const int MBRRpin = 7;       // Motor back right red 

const int MBRBpin = 8;       // Motor back right black

const int MBLRpin = 12;       // Motor back left red

const int MBLBpin = 13;       // Motor back left black

const int danger = 15;        // thresh hold for obstacle dist. in cm 

const int danger2 = 15;

const int ground1sensepin = 11;

const int ground2sensepin = 10;

const int groundS = 10;

const int groundR = 11;

const int groundL = 11;

int incomingbyte = 0;

int scanleft;

int scanright;

int scanahead;

long duration;

 

void setup (){

  Serial.begin(9600);

  pinMode(MBRRpin, OUTPUT);      // set pin as output

  pinMode(MBRBpin, OUTPUT);      // set pin as output

  pinMode(MBLRpin, OUTPUT);      // set pin as output

  pinMode(MBLBpin, OUTPUT);      // set pin as output

  pinMode(ground1sensepin, INPUT);

  pinMode(ground2sensepin, INPUT);

  servo1.attach(6);               // servo is attached to this pin

  servo2.attach(5);

  servo1.write(90);

  servo2.write(180);

  delay(7000);

}

 

void loop(){

  int obstacle = ping();

  int ground1 = digitalRead(ground1sensepin);

  int ground2 = digitalRead(ground2sensepin);

 

  if(obstacle > danger && ground1 == LOW && ground2 == LOW){

    Serial.println(" No obstacle nor edge detected ");

    straight(); 

    sweep();              //servo sweep action

  }

  if(ground1 == HIGH || ground2 == HIGH){

    Serial.println("Edge detected");

    servo1.write(90);

    Serial.println("Scaning ahead");

    servo2.write(90);

    delay(500);

    scanahead = ping();

    delay(2000);

    servo1.write(110);

    delay(500);

    Serial.println("Scaning left");

    scanleft = ping();

    delay(2000);

    servo1.write(70);

    delay(500);

    Serial.println("Scaning right");

    scanright = ping();

    delay(2000);

    servo1.write(90);

    delay(500);

    compare2();

  }

  else if(obstacle <= danger){    // if obstacle is sensed

    delay(1000);

    Serial.println(" obstacle detected ");

    Serial.println(" Looking for path ");

    Serial.println(" Scaning Left ");

    servo1.write(180);

    delay(1000);

    scanleft = ping();

    delay(2000);

    Serial.println(" Scaning right ");

    servo1.write(0);

    delay(1000);

    scanright = ping();

    delay(2000);

    Serial.println(" Scaning ahead ");

    servo1.write(90);

    delay(1000);

    scanahead = ping();

    delay(1000);

    compare();

  }

}

 

void compare(){

  if(scanleft <= danger2 && scanright <= danger2 && scanahead <= danger2){

    Serial.println(" All sides are blocked, turning around ");

    back();

    delay(1000);

    left();

    delay(2500);

  }

  else if(scanleft < scanahead && scanright < scanahead){

    Serial.println(" Straight is less obstructed, procceding ");

    straight();

    delay(2500);

  }

  else if(scanleft < scanright){

    Serial.println(" Right is less obstructed, Moving right ");

    right();

    delay(2500);

  }

  else if(scanleft > scanright){

    Serial.println(" Left is less obstructed, Moving left ");

    left();

    delay(2500);

  }

}

 

void compare2(){

  if(scanahead <= groundS && scanleft <= groundL && scanright <= groundR){

    Serial.println("Surface detected, procceding");

    servo2.write(180);

    straight();

    delay(5000);

  }

  else{

    Serial.println("No reachable surface, returning");

    servo2.write(180);

    back();

    delay(1000);

    left();

    delay(2500);

  }

}

 

void sweep(){

  int pos = 20;

 

  for(pos = 20; pos < 160; pos++)

  {

    servo1.write(pos);

    delay(4);

  }

  for(pos = 160; pos > 20; pos--)

  {

    servo1.write(pos);

  }

}

 

void left(){

  digitalWrite(MBRRpin, HIGH);

  digitalWrite(MBRBpin, LOW);  

  digitalWrite(MBLRpin, LOW);  

  digitalWrite(MBLBpin, HIGH);

}

 

void right(){

  digitalWrite(MBRRpin, LOW);

  digitalWrite(MBRBpin, HIGH);

  digitalWrite(MBLRpin, HIGH);

  digitalWrite(MBLBpin, LOW);    

}

 

void straight(){

  digitalWrite(MBRRpin, HIGH);

  digitalWrite(MBRBpin, LOW);

  digitalWrite(MBLRpin, HIGH);

  digitalWrite(MBLBpin, LOW);

}

 

void back(){

  digitalWrite(MBRRpin, LOW);

  digitalWrite(MBRBpin, HIGH);

  digitalWrite(MBLRpin, LOW);

  digitalWrite(MBLBpin, HIGH);

}

 

long ping(){

  // establish variables for duration of the ping, 

  // and the distance result in inches and centimeters:

 

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.

  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

  pinMode(pingPin, OUTPUT);

  digitalWrite(pingPin, LOW);

  delayMicroseconds(2);

  digitalWrite(pingPin, HIGH);

  delayMicroseconds(5);

  digitalWrite(pingPin, LOW);

 

  // The same pin is used to read the signal from the PING))): a HIGH

  // pulse whose duration is the time (in microseconds) from the sending

  // of the ping to the reception of its echo off of an object.

  pinMode(pingPin, INPUT);

  duration = pulseIn(pingPin, HIGH);

  Serial.println(duration/29/2);

  return duration/29/2; 

}

I placed the caps as mentioned here- http://letsmakerobots.com/node/12679

and these are the pics.Sorry icoudnt get a better picture and i am not a very good photographer.