Go Down

Topic: NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.5 (Read 128 times) previous topic - next topic

cyclegadget


@hbaxton

Take a look at this sketch. I reworked your sketch and put the motor running part into a function then, made it work with blink without delay style. It is untested but, I think it will work.

Code: [Select]
#include <NewPing.h>
#include <AFMotor.h>


#define TRIGGER_PIN  7  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     7  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

AF_Stepper motor(200, 1);
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

unsigned int pingSpeed = 1000;  // How frequently are we going to send out a ping (in milliseconds). 50ms would be 20 times a second.
unsigned long pingTimer;        // Holds the next ping time.
unsigned long motorTimer;       // Holds the place for motor speed adjusts
unsigned int adjustTime = 1000; // how often to adjust the speed of the motor
int rpm =10;

void setup(){
  Serial.begin(115200);
  pingTimer = millis(); // Start now.

  Serial.println("Stepper test!");
  motor.setSpeed(100);  // 100 rpm   
  motor.step(100, FORWARD, SINGLE);
  Serial.println("step done");
  motor.release();
  delay(1000);
}

void loop(){
  // Notice how there's no delays in this sketch to allow you to do other processing in-line while doing distance pings.
  if (millis() >= pingTimer) // pingSpeed milliseconds since last ping, do another ping.
  {   
    pingTimer = millis() + pingSpeed;      // Set the next ping time.
    sonar.ping_timer(echoCheck); // Send out the ping, calls "echoCheck" function every 24uS where you can check the ping status.
  }

////////////////////////////////////////Here is the new part that will run your motor on a timer
  if (millis() >= motorTimer)
  {
    motorrun();
    motorTimer = millis() + adjustTime;
  }
}

void motorrun()
{ // this is the function that will run your motor when called
  Serial.print("setting RMP = ");
  Serial.println(rpm);
  motor.setSpeed(rpm);
  Serial.println();
  Serial.println();
  motor.step(100, FORWARD, SINGLE);
  rpm = rpm +10;
}


void echoCheck() { // Timer2 interrupt calls this function every 24uS where you can check the ping status.
  // Don't do anything here!
  if (sonar.check_timer()) { // This is how you check to see if the ping was received.
    // Here's where you can add code.
    Serial.print("Ping: ");
    Serial.print(sonar.ping_result / US_ROUNDTRIP_CM); // Ping returned, uS result in ping_result, convert to cm with US_ROUNDTRIP_CM.
    Serial.println("cm");
  } 
}

teckel


hi teckel & cyclegadget,

thank you for your response.  i am new to events.  so i tried taking out all the delays and also tried calling the ping function directly but i had the same results.  i'm going to look into the adafruit motor library to see if it is using the timer as the new ping library.

thanks again!  i'll report back.


You can't just remove the delays and expect it to work as then you'll have other problems.  Instead, try using the replacement sketch I provided in a previous post that includes delays, but does so correctly.  Your sketch was a hybrid, but used bad logic.  To simplify things for you, my sketch does it the standard way, with the ping() method and delays.  This is how you should use NewPing unless you know what you're doing and know how to write event-driven sketches.  I do highly suggest that you learn this programming paradigm as you'll have a hard time graduating beyond "hello world" tests otherwise.  But, maybe try something simple at first.

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

xvjeko

Hello.. i wrote a code that uses this library, and isn't 100% reliable when debugging...
I'd be grateful if you comment it.
Code: [Select]

#include <NewPing.h>

#define BAUD 9600
#define MIN_DISTANCE 6      //Minimum distance to ping (cm)
#define MAX_DISTANCE 60     //Maximum distance to ping (cm)
#define THRESHOLD 10        //Treshold (cm)
#define HOLD_TIME 15        //Delay between each step (ms)
#define STEP_TIME 33        //Delay between ultrasonic's

//Creating ultrasonic objects
NewPing SONAR_L(TRIG_PIN_L, ECHO_PIN_L, 2000);
NewPing SONAR_R(TRIG_PIN_R, ECHO_PIN_R, 2000);

//Creating debugg function
void debugg(int val_l, int val_r)
{
 Serial.print(val_l);
 Serial.print(" ");
 Serial.print(val_r);
 Serial.println();
}

//Creating setup function
void setup()
{
 Serial.begin(BAUD);
 
 pinMode(ECHO_PIN_L, INPUT);
 pinMode(ECHO_PIN_R, INPUT);
 pinMode(TRIG_PIN_L, OUTPUT);
 pinMode(TRIG_PIN_R, OUTPUT);
}

//Program
void loop()
{
 //Declaring local static variables
 static bool hold = false;
 static int val_l, val_r;
 static int per_l, per_r;
 static int prev_per_l = MIN_DISTANCE, prev_per_r = MIN_DISTANCE;

 //Reading ultrasonic values
 delay(STEP_TIME);
 unsigned int uS1 = SONAR_L.ping();
 per_l = uS1 / US_ROUNDTRIP_CM;
 
 delay(STEP_TIME);
 unsigned int uS2 = SONAR_R.ping();
 per_r = uS2 / US_ROUNDTRIP_CM;
 
 //Limiting the effective range of ultrasonics
 per_l = constrain(per_l, MIN_DISTANCE, MAX_DISTANCE);
 per_r = constrain(per_r, MIN_DISTANCE, MAX_DISTANCE);
 
 //This part of code is used to save the distance of ultrasonic sensors
 //when the hand is moved away from the beam ) ) ) of the sensor
 if( (per_l - prev_per_l) > THRESHOLD )
 {
   per_l = prev_per_l;
   hold=false;
 }

 if( (per_r - prev_per_r) > THRESHOLD )
 {
   per_r = prev_per_r;
   hold=false;
 }
 
 prev_per_l = per_l;
 prev_per_r = per_r;

 //Modifying the data using linear math (used for servo motors)
 per_l = map(per_l, MIN_DISTANCE, MAX_DISTANCE, 100, 0);

 val_l = map(per_r, MIN_DISTANCE, MAX_DISTANCE, 90, 0);
 val_r = map(per_r, MIN_DISTANCE, MAX_DISTANCE, 0, 90);
 
 val_l = val_l * (per_l / 100.0);
 val_r = val_r * (per_l / 100.0);

 //Writing final data to servo motors
 servo_l.write(val_l);
 servo_r.write(val_r);
 
 //Debugging the final data
 debugg(val_l, val_r);

 //Applying the final delay (due to servo speed characteristics)
 hold=true;
 if(hold==true) delay(HOLD_TIME);
}

teckel


Hello.. i wrote a code that uses this library, and isn't 100% reliable when debugging...
I'd be grateful if you comment it.


1) TRIG_PIN_L, TRIG_PIN_R, ECHO_PIN_L and ECHO_PIN_R are never defined.
2) No reason to set the pin modes, NewPing does that for you.
3) You shouldn't set the maximum distance to 2000cm on the "NewPing SONAR" lines.  The sensor won't work beyond 500cm and setting it to 2000 could make the time between pings from 33ms to 115ms or just cause problems in general.  Just remove the ,2000 and it will default to the sensor's maximum distance, which is 500cm and will allow for 33ms between pings as you have it set.
4) The "constrain" commands will give you problems as out of range results (which are zero) would change to 6.  Out of range would typically be beyond 500cm, making it 6cm will probably not give what you're looking for.  Instead, do something like "if (per_l > 0) per_l = constrain(per_l, MIN_DISTANCE, MAX_DISTANCE);" so there can still be a zero result (out of range).
5) The rest of your code then needs to accommodate what to do with out of range results.  It doesn't appear that you have considered that.  If anything, a zero result should be changed to MAX_DISTANCE + 1, as that's what it means.  Without considering a zero result (out of range), your sketch will probably never work.

That should give you a good start to diagnose what's going on.  1, 3, 4 & 5 are real problems that will seriously cause problems for your sketch.

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

xvjeko



Hello.. i wrote a code that uses this library, and isn't 100% reliable when debugging...
I'd be grateful if you comment it.


1) TRIG_PIN_L, TRIG_PIN_R, ECHO_PIN_L and ECHO_PIN_R are never defined.
2) No reason to set the pin modes, NewPing does that for you.
3) You shouldn't set the maximum distance to 2000cm on the "NewPing SONAR" lines.  The sensor won't work beyond 500cm and setting it to 2000 could make the time between pings from 33ms to 115ms or just cause problems in general.  Just remove the ,2000 and it will default to the sensor's maximum distance, which is 500cm and will allow for 33ms between pings as you have it set.
4) The "constrain" commands will give you problems as out of range results (which are zero) would change to 6.  Out of range would typically be beyond 500cm, making it 6cm will probably not give you want you're looking for.  Instead, do something like "if (per_l > 0) per_l = constrain(per_l, MIN_DISTANCE, MAX_DISTANCE);" so there can still be a zero result (out of range).
5) The rest of your code then needs to accommodate what to do with out of range results.  It doesn't appear that you have considered that.  If anything, a zero result should be changed to MAX_DISTANCE + 1, as that's what it means.  Without considering a zero result (out of range), your sketch will probably never work.

That should give you a good start to diagnose what's going on.  1, 3, 4 & 5 are real problems that will seriously cause problems for your sketch.

Tim


thank you sir

EDIT: I got everything to work now... I hope it will run flawlessly in the real life... Thanks again

Go Up