Shop OBEX P1 Docs P2 Docs Learn Events
pulseIn code for ping? — Parallax Forums

pulseIn code for ping?

Jamesonh20Jamesonh20 Posts: 2
edited 2009-11-09 14:02 in Accessories
I am using a Parallax ping sensor on a servo connected to a Arduino 328. I am trying to get the ping sensor to ping as close to continious as possible while the servo sweeps. But the servo stops everytime the Parallax sensor pings. So to do a 180* sweep it takes roughly a minute and a half. I am being told that the pulseIn (return info) is a disruptive declaration, IS THERE ANY WAY AROUND THIS???

#include <Servo.h>
const int pingPin = 7;
const int servoPin = 9;
const int LED = 13;
Servo myservo; // create servo object to control a servo
int pos = 0;            //  current servo position
int dir = 1;            //  current servo direction
int ping (void) {
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);  // hello, ping)))
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  return pulseIn(pingPin, HIGH) / 148;  // wait for echo and return inches
}
void setup() { 
  myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
  Serial.begin(9600);
}
void loop (void) {
  int inches;
  if (pos == 0) dir = 1;
  else if (pos == 180) dir = -1;
  myservo.write(pos);      // tell servo to go to position in variable 'pos'
  delay(14);               // waits 14ms for the servo to reach the position
  inches = ping ();        // anybody there?
  Serial.print (inches);
  Serial.println (" inches.");
  if (inches < 11)         // if anything's within 10 inches
    digitalWrite (LED, HIGH);  // turn led on
  else                     // otherwise
    digitalWrite (LED, LOW);  //turn led off
  pos += dir;              // advance servo position
}


Thanks 4 any insight!!

jamesonh20

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-08 20:41
    We can't really help you with the Arduino code. It may be that the Arduino is somehow missing the PING echo pulse and maybe the pulseIn is timing out. Try a short delay between the last "digitalWrite(pingPin,LOW)" and the "pinMode(pingPin,INPUT)".
  • FranklinFranklin Posts: 4,747
    edited 2009-11-08 20:57
    Take out the serial commands and shorten or remove the delay in moving the servo.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • Jamesonh20Jamesonh20 Posts: 2
    edited 2009-11-09 03:44
    Well I tried both of the suggestions from each of you and the problem still exists. I think that there is some sort of signal jam between the servo running and the ping sensor running at the same time. Everytime the parallax pings the servo stops then starts again. I think I need a seperate processor to run the servo independently.
    Take out the serial commands and shorten or remove the delay in moving the servo. said...
    (replace this text with what was said)
    Taking out the serial commands didn't seem to help/hurt anything, The delay time also doesnt speed anything up. The ping and servo still --> ping/move...ping/move...etc. I need a smooth transition. Thanks for the help though!

    Jamesonh20
  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-09 03:59
    The Basic Stamps do quite well interleaving the PING sensing with the generation of the servo control pulses, all with a relatively slow single processor. There must be something strange going on with the Arduino implementation of either the pulseIn or the servo control object.
  • tronsnavytronsnavy Posts: 70
    edited 2009-11-09 14:02
    Jamesonh20,
    Go to "Stamps In Class", Ping)))Dar - a Radar Style Display.· It contains code for the BS2 and Propeller (spin).· I got ping to scan (using Propeller) from 0-180 in about 4 seconds at about ~ 1.4 degrees of resolution.·· That's about 128 "PINGS" in 180 degrees.· If you change it to 2.8 degrees of resolution, Ping would scan 0-180 degrees in about 2 seconds.· Of course, this was using spin and no Floating Point (FP) Math.· I could speed this up by using PASM, but I have no time.· You might be able to decipher the code and implement to Arduino.· Good luck.
    Bob
Sign In or Register to comment.