Shop OBEX P1 Docs P2 Docs Learn Events
Ping repeat calls — Parallax Forums

Ping repeat calls

CRChisholmCRChisholm Posts: 5
edited 2012-02-20 06:06 in Accessories
I have several STAMPS and Arduino, and I sometimes use Parallax sensors on the Arduino. In this case the Ping sensor on the arduino mega. I am running into a problem with the ping mounted to a servo at 90 degrees and ping, turning it to 0 and ping, then 180 and ping again. Works perfectly on the first and second pings, but I can't seem to get it to ping on the third call, even if I change the sequence to 90, 0, and 180. Still won't ping on the third try. It doesn't seem to have anything to do with the servo, because I get the same results if the servo is disconnected. Before each call to the ping, I set the signal low for 2 microseconds and then high for 5 microseconds before I make the pin input and read it. Is there something about the timing on the ping that I should understand? I will be glad to post the code, but since it is arduino mega code, I figured I'd get stoned. Sorry if this is too vague. .

Comments

  • FranklinFranklin Posts: 4,747
    edited 2012-02-18 20:53
    How long do you wait between pings? You might not be giving enough time for echos to die down.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-02-19 07:13
    CRChisholm wrote: »
    I figured I'd get stoned.

    I knew it! Arduino users are stoners.:smile:

    I noticed in the Propeller Ping object the pin is pulled low before making it an input. I'd be surprised if this matters, but I noticed in the part of your post I didn't intentionally misread, that you don't say that you pull the line low prior to making it an input. Again, I'd be surprised if this matters.

    I say go ahead an post your code. Blame me if people complain. If they do, I'll get stoned with you.:zombie:

    When you post code, it will help if you follow these directions.

    attachment.php?attachmentid=78421&d=1297987572


    BTW, welcome to the forum . . . dude.
    (My attempts at humor are going to get me in trouble someday.)
  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2012-02-19 07:18
    There's working code for the Arduino and Ping here:

    https://sites.google.com/site/parallaxinretailstores/

    Just move the servo, wait (delay) for the servo to transit to its position, then call the pinging code. Note the sequence of setting the pin direction as either an INPUT or an OUTPUT, and when an OUTPUT changing it from LOW to HIGH.

    The code was developed for an Uno, but in this regard the Mega isn't any different.

    -- Gordon
  • CRChisholmCRChisholm Posts: 5
    edited 2012-02-19 15:52
    Thanks for the reply, Franklin. I have tried waiting quite a while before pinging. Right now the code is set for 5 sec. I also put the code in a do loop so that if I try unsuccessfully, it should keep trying . Unfortunately it just keeps trying. About arduino users being....I have tried to work with Arduino, Basic Stamp, OOPIc, and Picax and have shown equal incompetence in all. Maybe I SHOULD be stoned.

    The ping works perfectly even with the servo for the first two tries (straight ahead (90) and at zero ), but not the third try at 180 (I know I have 170...trying anything). I tried switching the two loops and doing 180 first, and then zero, but it made no difference.

    BTW, I discovered that the code works perfectly if the servo is not connected, so I think you're on the right track, but I just can't seem to get around it.

    Thank you again for responding.
    #include <Servo.h> 
    Servo myservo; 
    const int pingPin = 10;
    long duration, newPath, lastDur;
    void setup() {
      Serial.begin(9600);
      myservo.attach(9);
     
    }
    void loop()
    {
      myservo.write(90);
      duration = getPing();
      // 740 microseconds translates to about five inches 
      if (duration < 740 && lastDur - duration < 740) {
          Serial.println("object found");
          newPath=lookForPath();
          }
      lastDur=duration;
      delay(100);
    }
    int lookForPath() {
      long rightDur = 0;
      long leftDur = 0;
      
      Serial.println("Looking for Path");
      myservo.write(0);
      delay(5000);
      Serial.println("Looking Left ");
      do {
          leftDur = getPing();
          Serial.print(leftDur);
          } while (leftDur == 0);
      Serial. println();
      
      myservo.write(170);
      delay(5000);
      Serial.println("Looking Right ");  
      
      // Here is where the problem occures....If the servo is connected, the loop below will go on forever
      // because a value is never returned to rightDur and the ping LED never flashes in this loop.  
      // But if the servo is NOT connected, the ping LED flashes and rightDur is loaded  with a value 
      // on the first try and the code continues on as it should.
      
      do {
          rightDur = getPing();
          Serial.print(rightDur);
          } while (rightDur == 0);
      Serial. println();
      myservo.write(90);
      delay(5000);
      if (rightDur > leftDur) {
          Serial.print("turn right!!  ");
          return 0;
          }
      else {
          Serial.print("turn left!!  ");
          return 180;
          }
      }
      
    long getPing() {
      pinMode(pingPin, OUTPUT);
      digitalWrite(pingPin, LOW);
      delayMicroseconds(2);
      digitalWrite(pingPin, HIGH);
      delayMicroseconds(5);
      digitalWrite(pingPin, LOW);
      pinMode(pingPin, INPUT);
      return pulseIn(pingPin, HIGH);
      };
    
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-02-19 16:04
    This really sounds like a power supply issue. What are your using for power?

    Servos pull a lot of current when they first start moving. I've had a bunch of projects that kept resetting when a servo started moving. Sometimes a 1000uF capacitor on the Vin line will provide enough juice to keep the uC happy while the servo starts to move. Other times a separate battery for the uC is required.
  • CRChisholmCRChisholm Posts: 5
    edited 2012-02-19 16:27
    That sounds very possible. Since I was testing the program, I was drawing my power from the Arduino (oops forgot where I was for a second) ardrino. Let me try a seperate power source for the servo.

    Thanks and I will get back to you as soon as I try it.
  • CRChisholmCRChisholm Posts: 5
    edited 2012-02-19 17:23
    So, how's it feel to always be right?? You nailed it, dude! I guess that should have been something I thought of right away (and probably will from now on), but it just seemed that the servo worked fine when connected.

    I left the ping on the arduino and put the servo on another power souce and it worked like a charm.

    Thank you very much.

    Well, on to the the next stupid mistake....

    Charlie
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-02-19 20:36
    Charlie,

    I'm glad I could give some useful advice after razzing you about the arduino.
    CRChisholm wrote: »
    Well, on to the the next stupid mistake....

    My current favorite is to forget to have my separate power supplies share a common ground connection. The servos sometimes, sort of, work without a shared ground with the uC.
  • CRChisholmCRChisholm Posts: 5
    edited 2012-02-20 06:06
    Yep, that was good for another "Oh, now what?" moment for me this time as well. Next one after that was forgetting to actually PLUG-IN my alternate power source. The good news is I ended without any calderas in my IC's. That's a plus.
Sign In or Register to comment.