Go Down

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

Holm

Good to hear it's possible  ;)

I was thinking about a stepper motor becouse of the precise stop/turns.

I am building a garage for my robot lawn mower, and i need to control the garage door - which consists of an small aluminum plate. Lift it like an elevator with a wire or something.

When the robot is leaving the base - the door needs to lift up fast to a given hight.
When it returns and stay in the base for 10 sec the door needs to lower down slowly to the ground.

Sometimes it goes to the base and stops - and then drives out again immediately. In this case the door must stay in raised position.

Thats my idea  ;)

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Link?
I just ordered one from eBay ($1.35).  We'll see if it's really more accurate than the HC-SR04 module.  Also, it appears making it work with NewPing should be a simple task as the trigger/echo processes are almost identical.

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

LarsJ

Hi- maybe you can help. I have connected the HCSR04 Echo pin to TK2 and Trigger til TK1 using the following sketch. But noting happens - I get "=0 cm" as readout no matter what.


#include <ArduinoRobot.h>
#include <Wire.h>
#include <SPI.h>
#include <NewPing.h>

int TRIGGER_PIN = TK1;
int ECHO_PIN = TK2;
int MAX_DISTANCE = 200;
int distance = 0;

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
 Robot.begin();
 
}

void loop() {
 delay(50);
 unsigned int uS = sonar.ping();
 Serial.print("Ping: ");
 Serial.print(distance / US_ROUNDTRIP_CM);
 Serial.println("cm");

//Serial.print(distance / US_ROUNDTRIP_CM);
 
 }




Hi- maybe you can help. I have connected the HCSR04 Echo pin to TK2 and Trigger til TK1 using the following sketch. But noting happens - I get "=0 cm" as readout no matter what.


#include <ArduinoRobot.h>
#include <Wire.h>
#include <SPI.h>
#include <NewPing.h>

int TRIGGER_PIN = TK1;
int ECHO_PIN = TK2;
int MAX_DISTANCE = 200;
int distance = 0;

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
 Robot.begin();
 
}

void loop() {
 delay(50);
 unsigned int uS = sonar.ping();
 Serial.print("Ping: ");
 Serial.print(distance / US_ROUNDTRIP_CM);
 Serial.println("cm");

//Serial.print(distance / US_ROUNDTRIP_CM);
 
 }



Because zero divided by any number is always zero.  You're setting the variable "distance" to zero and then dividing it by US_ROUNDTRIP_CM.  You're never actually using the sonar distance variable, which is "uS".

Get rid of the "int distance = 0;" at the top of your sketch, then change the serial output of the distance to:

Serial.print(uS / US_ROUNDTRIP_CM);

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

Hi- maybe you can help. I have connected the HCSR04 Echo pin to TK2 and Trigger til TK1 using the following sketch. But noting happens - I get "=0 cm" as readout no matter what.


#include <ArduinoRobot.h>
#include <Wire.h>
#include <SPI.h>
#include <NewPing.h>

int TRIGGER_PIN = TK1;
int ECHO_PIN = TK2;
int MAX_DISTANCE = 200;
int distance = 0;

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
 Robot.begin();
  
}

void loop() {
 delay(50);
 unsigned int uS = sonar.ping();
 Serial.print("Ping: ");
 Serial.print(distance / US_ROUNDTRIP_CM);
 Serial.println("cm");

//Serial.print(distance / US_ROUNDTRIP_CM);
 
 }



Because zero divided by any number is always zero.  You're setting the variable "distance" to zero and then dividing it by US_ROUNDTRIP_CM.  You're never actually using the sonar distance variable, which is "uS".

Get rid of the "int distance = 0;" at the top of your sketch, then change the serial output of the distance to:

Serial.print(uS / US_ROUNDTRIP_CM);

After you have this working, I would then try the unified pin method in NewPing (both trigger and echo wired to the same pin).  I would suggest this as you have limited pins on the Arduino Robot.

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

LarsJ

HI - thanks.
And quite embarassing. However, I have corrected that - and it still doesn't work.

This code works on an Arduino Uno with an HC-SR04 sensor (even though, it's like there are way to many "0" results even if I hold up a book 10 cm in front of the sensor. It only reads the distance correctly like one of ten times. But it reacts to objects.

Code: [Select]
 
#include <NewPing.h>
#define TRIGGER_PIN  12
#define ECHO_PIN     11
#define MAX_DISTANCE 100
int distance = 0;


NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
 // put your setup code here, to run once:
 Serial.begin(115200);
}

void loop() {
delay(200);
int uS = sonar.ping();
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM);
Serial.println("cm");

}

 



Then I changed the code a little to adapt to the robot pin names etc.
Like this:

Code: [Select]
   

#include <ArduinoRobot.h>
#include <NewPing.h>
#include <SPI.h>
#include <Wire.h>


#define TRIGGER_PIN B_TK3
#define ECHO_PIN B_TK4
#define MAX_DISTANCE 100

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
Serial.begin(115200);
Robot.begin();
Robot.beginTFT();
Robot.beginSD();
}

void loop() {
delay(200);
int uS = sonar.ping();
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM);
Serial.println("cm");

}



That doesn't Work. I never get anything but "0 cm".

I'm wondering if it could be:
- power from the robot being slightly lower than from the Uno
- pins on the robot being handled in another way (these pins, TKD3 & 4 should be digital)
- that the Measurement somehow gets stuck at zero, and that this is even more so non the robot, so maybe cutting of power to the sensor after each measuring could help (suggested in another forum post)

Lars


HI - thanks.
And quite embarassing. However, I have corrected that - and it still doesn't work.

This code works on an Arduino Uno with an HC-SR04 sensor (even though, it's like there are way to many "0" results even if I hold up a book 10 cm in front of the sensor. It only reads the distance correctly like one of ten times. But it reacts to objects.

Code: [Select]
 
#include <NewPing.h>
#define TRIGGER_PIN  12
#define ECHO_PIN     11
#define MAX_DISTANCE 100
int distance = 0;


NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
 // put your setup code here, to run once:
 Serial.begin(115200);
}

void loop() {
delay(200);
int uS = sonar.ping();
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM);
Serial.println("cm");

}

 



Then I changed the code a little to adapt to the robot pin names etc.
Like this:

Code: [Select]
   

#include <ArduinoRobot.h>
#include <NewPing.h>
#include <SPI.h>
#include <Wire.h>


#define TRIGGER_PIN B_TK3
#define ECHO_PIN B_TK4
#define MAX_DISTANCE 100

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
Serial.begin(115200);
Robot.begin();
Robot.beginTFT();
Robot.beginSD();
}

void loop() {
delay(200);
int uS = sonar.ping();
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM);
Serial.println("cm");

}



That doesn't Work. I never get anything but "0 cm".

I'm wondering if it could be:
- power from the robot being slightly lower than from the Uno
- pins on the robot being handled in another way (these pins, TKD3 & 4 should be digital)
- that the Measurement somehow gets stuck at zero, and that this is even more so non the robot, so maybe cutting of power to the sensor after each measuring could help (suggested in another forum post)

Lars


NewPing uses port register calls to set the port state and set or read the port status.  I've never used an Arduino Robot nor know much about it.  But, the odd pin names makes me think that maybe that's the issue.

Looking into the Arduino Robot's specs, TK0 to TK7 can't be used for sure.  They're multiplexed from a single Arduino pin.  It appears the Arduino Robot has two processors, which is why special functions are used.  This would also mean probably all pins on the motor board would not work either.

Basically, it appears that the Arduino Robot is very unique.  It can't even use digitalRead and digitalWrite commands.  Therefore, I would guess many libraries would not be compatible with the Arduino Robot.

It's possible that you could get NewPing working by using the correct pins.  It all depends on how they setup the wire library.  By looking at the specs, it would appear the best bet would be to try and use TKD0-TKD3, as those appear to be the only non-corrupted ports on the primary processor.  You may need to access them as TKD0-TKD3, or maybe their actual names which is A1-A4.  It's very possible that the TKD0-TKD3 names only work with the Robot.digitalRead() and Robot.digitalWrite() commands.

It's also possible that you just can't do it.  You'll probably have many other problems with other libraries as the hardware is quite different than other Arduino platforms.

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

LarsJ

Hi Tim

Thanks a lot.

You're completely right.

I changed the code to Trigger pin = A4 (which is D3 on the board) and Echo pin = A2 (which is D1) - and then I get signals from the ultrasonic sensor.

Great insight.
And yes - the robot hardware is quite different - and quite a challenge to decode due to name confusion.

regards
Lars, Denmark



Hi Tim

Thanks a lot.

You're completely right.

I changed the code to Trigger pin = A4 (which is D3 on the board) and Echo pin = A2 (which is D1) - and then I get signals from the ultrasonic sensor.

Great insight.
And yes - the robot hardware is quite different - and quite a challenge to decode due to name confusion.

regards
Lars, Denmark
That confirms that the "TK" pins can only use the special Robot.digitalRead() and Robot.digitalWrite() commands.  When not using code specifically for the Arduino Robot, you should use the actual port names, not the robot port names.

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

Hi!

Has anyone used this library to attempt to measure distance after first going through a barrier/wall/aberrating layer?  The specific application I want to test is liquid level measurement by mounting an ultrasonic sensor outside of a metal tank.  Will *any* signal come back from the internal liquid is my first question.  Wondering if anyone has tried this.

thanks.

Justin

Hi!

Has anyone used this library to attempt to measure distance after first going through a barrier/wall/aberrating layer?  The specific application I want to test is liquid level measurement by mounting an ultrasonic sensor outside of a metal tank.  Will *any* signal come back from the internal liquid is my first question.  Wondering if anyone has tried this.

thanks.

Justin
This would be a sensor question, not a library question.  In any case, the sensor cannot go through a solid object.  It sends a ping and listens for the echo.  The only way to measure a fluid level is to have the sensor inside the container looking down on the surface level.  But then you need to be concerned with moisture issues.

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy