Pages: [1]   Go Down
Author Topic: Ultrasonic Ping Sensor  (Read 198 times)
0 Members and 1 Guest are viewing this topic.
Massachusetts
Offline Offline
Full Member
***
Karma: 0
Posts: 105
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have the following code reading 2 ping sensors, where the transmitter on each has been removed leaving only the receivers. The transmitter that was removed from sensor 1 was soldered back to the board with a 10ft length of wire.  This will allow me to hold the transmitter 2ft from the 2 receivers on center.  The transmitter is triggered by sensor 1 and initializes both sensor 1 and sensor 2.  Each receiving sensor is spaced 18" apart and approx. 24" away from the transmitter on center.  I able to read the output in cm on sensor 1, however the output on sensor 2 seems to be approx. 10cm short of sensor 1.  I have tried changing the timing function and modifying the order of the code, but this does not change the outcome. 

Can anyone offer any advice or see anything that my help match the output of the sensors at equal distance?
Thank you
Dave

Please find the code below.

Code:
#include <NewPing.h>

#define TRIGGER_PIN_1  12  // trigger pin on ping sensor 1
#define ECHO_PIN_1     12  // echo pin on ping sensor1
#define MAX_DISTANCE_1 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

#define TRIGGER_PIN_2  8  // trigger pin on ping sensor 2
#define ECHO_PIN_2     8  // echo pin on ping sensor 2
#define MAX_DISTANCE_2 175 // Maximum distance to ping in centimeters

NewPing sonar1(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE_1);
NewPing sonar2(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE_2);

unsigned int pingSpeed_1 = 300; // Frequency of ping output (in milliseconds)
unsigned long pingTimer_1;     // Hold next ping time.
unsigned int pingSpeed_2 = 300; // Frequency of ping output (in milliseconds)
unsigned long pingTimer_2;     // Hold next ping time.


void setup() {
  Serial.begin(9600); // serial monitor
  pingTimer_1 = millis(); // Start
  pingTimer_2 = millis(); // Start
}

void loop() {

  if (millis() >= pingTimer_1) {   // pingSpeed milliseconds since last ping, do another ping.
    pingTimer_1 += pingSpeed_1;      // Set next ping time.
    sonar1.ping_timer(echoCheck1); // Send out ping, calls "echoCheck" function every 24uS to check ping status.
  }

  if (millis() >= pingTimer_2) {   // pingSpeed milliseconds since last ping, do another ping.
    pingTimer_2 += pingSpeed_2;      // Set next ping time.
    sonar2.ping_timer(echoCheck2); // Send out ping, calls "echoCheck" function every 24uS to check ping status.
  }
  // add other code with no delay
}

void echoCheck1() { // Timer2 interrupt calls function every 24uS to check the ping status.
  // Don't do anything here!
  if (sonar1.check_timer()) { // check to see if ping was received.
    // Here's where you can add code.
    Serial.print("Ping1: ");
    Serial.print((sonar1.ping_result / US_ROUNDTRIP_CM); // convert to cm with US_ROUNDTRIP_CM
    Serial.println("cm1");
  }
  // Don't do anything here!
}

void echoCheck2() { // Timer2 interrupt calls function every 24uS to check the ping status.
  // Don't do anything here! 
  if (sonar2.check_timer()) { // check to see if ping was received.
    // Here's where you can add code.
    Serial.print("Ping2: ");
    Serial.print(sonar2.ping_result / US_ROUNDTRIP_CM); // convert to cm with US_ROUNDTRIP_CM
    Serial.println("cm2");
  }
  // Don't do anything here!
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8074
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think your problem is that there appears to be only one pin per sensor that is shared between Trigger and Echo. Because Sensor 2 thinks it is reading its own echo it starts the timing based of when TRIGGER_PIN_2 is pulsed.  If you could change to a sensor that has separate Trigger and Echo pin, like the HC-SR04 (http://www.fasttech.com/product/1012007) you could rigger them both together and look at the Echo pins alternately.

If you are married to your existing hardware you can try using Direct Register Manipulation to trigger both at the same time instead of in sequence.  To do that the trigger pins would have to be connected to a single PORT.  Lucky for you, pins 8 and 12 are both in PORTB (bit 0 and bit 4).  To set them both ON at the same time:

Code:
PORTB |= 0b00010001;

To turn them off:

Code:
PORTB &= 0b11101110;
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: