Combine a code for a different ping sensor with a pingbot code

Hello.
Im building a simple pingbot. But the tutorial i follow is made for a ping sensor with 3 pins. Mine is 4.
I do have the code for the 4-pin sensor but i having trouble combining it to the original pingbot code.
Therefore i would appreciate some help combining this two codes.
These two sensors looks the same, but have different connections.

Heres a manual on my sensor
http://babelduck.com/index.php/en/reference/user-manuals/1423-ultrasonic-module-user-manual

thanks in advance

Pingbot code

/*MAEP 2.0 Navigation
by Noah Moroze, aka GeneralGeek
This code has been released under a Attribution-NonCommercial-ShareAlike license, more info at http://creativecommons.org/licenses/
PING))) code by David A. Mellis and Tom Igoe http://www.arduino.cc/en/Tutorial/Ping
*/

#include <Servo.h> //include Servo library

const int RForward = 0; 
const int RBackward = 180; 
const int LForward = RBackward; 
const int LBackward = RForward; 
const int RNeutral = 90; 
const int LNeutral = 90; //constants for motor speed
const int pingPin = 7;
const int irPin = 0;  //Sharp infrared sensor pin
const int dangerThresh = 10; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;  
Servo leftMotor;
Servo rightMotor; //declare motors
long duration; //time it takes to recieve PING))) signal

void setup()
{
  rightMotor.attach(11);
  leftMotor.attach(10);
  panMotor.attach(6); //attach motors to proper pins
  panMotor.write(90); //set PING))) pan to center
}

void loop()
{
  int distanceFwd = ping();
  if (distanceFwd>dangerThresh) //if path is clear
  {
    leftMotor.write(LForward); 
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral);
    rightMotor.write(RNeutral); 
    panMotor.write(0); 
    delay(500);
    rightDistance = ping(); //scan to the right
    delay(500);
    panMotor.write(180);
    delay(700);
    leftDistance = ping(); //scan to the left
    delay(500);
    panMotor.write(90); //return to center
    delay(100);
    compareDistance();
  }
}
  
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    leftMotor.write(LBackward); 
    rightMotor.write(RForward); //turn left
    delay(500); 
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn right
    delay(500);
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward); 
    rightMotor.write(RBackward); //turn 180 degrees
    delay(1000);
  }
}

long ping()
{
  // Send out PING))) signal pulse
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  
  //Get duration it takes to receive echo
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  
  //Convert duration into distance
  return duration / 29 / 2;
}

4 pin sensor

// Arduino code for using our Ultrasonic Range Finder
// Please feel free to copy, edit, distribute, sell, plagiarise ... Whatever you want :)
// A mention of Babelduck Cybernetics would be nice but is not necessary
// Written December 2011
#define trigPin 13  // Connect Trig on the module to pin 13 on the Arduino
#define echoPin 12  // Connect Echo on the module to pin 12 on the Arduino
void setup()
{
   pinMode(trigPin, OUTPUT);    // Set the trigPin to output
   digitalWrite(trigPin, LOW);  // Set trigPin low to start
   pinMode(echoPin, INPUT);     // Set echoPin as an input
   Serial.begin(9600);          // Start a serial connection so you can view the output in the Arduino IDE
   delay(3000);                 // 3 second pause before we start
}
void loop()
{
  long duration;                      // Define the variable we will use to store the pulse duration
 
  digitalWrite(trigPin, HIGH);        // Set the trigPin high, start of pulse to get module working
  delayMicroseconds(10);              // Wait 10 mS
  digitalWrite(trigPin, LOW);         // Set the trigPin low, module will now send a pulse
  duration = pulseIn(echoPin, HIGH);  // Read the pulse length from the module
  duration = duration/53;             // Convert number to cm
 
  Serial.println(duration);           // Send the distance in cm to the serial port
  delay(1000);                        // Wait a second before the next reading
}

What kind of trouble are you having combining the two codes? You should try to work it out on your own, test it, and then ask for help. Hint: Look at what the differences are between the two ultrasonic sensors. Look at the pin assignments and the duration of the trigger pulse, etc. Post your combined code, and I'll be happy to help from there.

In the first code, you have a function called ping() that gets data from one kind of sensor.

In the second code, the code that gets the data from the sensor is in loop().

  long duration;                      // Define the variable we will use to store the pulse duration

  digitalWrite(trigPin, HIGH);        // Set the trigPin high, start of pulse to get module working
  delayMicroseconds(10);              // Wait 10 mS
  digitalWrite(trigPin, LOW);         // Set the trigPin low, module will now send a pulse
  duration = pulseIn(echoPin, HIGH);  // Read the pulse length from the module
  duration = duration/53;             // Convert number to cm

Move that code from loop() to ping().

After staring at the codes for several hours, this is what i came up with.
I dont have the robot at home so i cant test it out yet.
But does it look like something that could work?
Is this what you ment PaulS?

/*MAEP 2.0 Navigation
by Noah Moroze, aka GeneralGeek
This code has been released under a Attribution-NonCommercial-ShareAlike license, more info at http://creativecommons.org/licenses/
PING))) code by David A. Mellis and Tom Igoe http://www.arduino.cc/en/Tutorial/Ping
*/
#define trigPin 13  // Connect Trig on the module to pin 13 on the Arduino
#define echoPin 12  // Connect Echo on the module to pin 12 on the Arduino
#include <Servo.h> //include Servo library

const int RForward = 0; 
const int RBackward = 180; 
const int LForward = RBackward; 
const int LBackward = RForward; 
const int RNeutral = 90; 
const int LNeutral = 90; //constants for motor speed
const int dangerThresh = 10; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;  
Servo leftMotor;
Servo rightMotor; //declare motors
long duration; //time it takes to recieve PING))) signal

void setup()
{
  pinMode(trigPin, OUTPUT);    // Set the trigPin to output
  digitalWrite(trigPin, LOW);  // Set trigPin low to start
  pinMode(echoPin, INPUT);     // Set echoPin as an input
  rightMotor.attach(11);
  leftMotor.attach(10);
  panMotor.attach(9); //attach motors to proper pins
  panMotor.write(0); //set PING))) pan to center
}

void loop()
{
  int distanceFwd = ping();
  if (distanceFwd>dangerThresh) //if path is clear
  {
    leftMotor.write(LForward); 
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral);
    rightMotor.write(RNeutral); 
    panMotor.write(20); 
    delay(500);
    rightDistance = ping(); //scan to the right
    delay(500);
    panMotor.write(160);
    delay(700);
    leftDistance = ping(); //scan to the left
    delay(500);
    panMotor.write(90); //return to center
    delay(100);
    compareDistance();
  }
}
  
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    leftMotor.write(LBackward); 
    rightMotor.write(RForward); //turn left
    delay(500); 
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn right
    delay(500);
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward); 
    rightMotor.write(RBackward); //turn 180 degrees
    delay(1000);
  }
}

long ping()
{
  // Send out PING))) signal pulse
  long duration;                      // Define the variable we will use to store the pulse duration
  digitalWrite(trigPin, HIGH);        // Set the trigPin high, start of pulse to get module working
  delayMicroseconds(10);              // Wait 10 mS
  digitalWrite(trigPin, LOW);         // Set the trigPin low, module will now send a pulse
  duration = pulseIn(echoPin, HIGH);  // Read the pulse length from the module
  duration = duration/53;             // Convert number to cm
  
  //Convert duration into distance
  return duration / 29 / 2;
}

Is this what you ment PaulS?

Close.

long ping()
{
  long duration;                      // Define the variable we will use to store the pulse duration
<snip>
  return duration / 29 / 2;
}

So, you have a function that returns a long,

  int distanceFwd = ping();

which you store in an int.

One of them is wrong. Either the function should return an int (carefully) or the returned value should be stored in the appropriate type variable.

I have to say that im totaly stuck :P

PaulS, Do you know the answer to this? "One of them is wrong. Either the function should return an int (carefully) or the returned value should be stored in the appropriate type variable." and are just trying to make me figure it out for myself?^^ Because i dont get it :P

I will try to upload this to my bot tomorow and see whats happens :P

fridh:
I have to say that im totaly stuck :stuck_out_tongue:

PaulS, Do you know the answer to this? “One of them is wrong. Either the function should return an int (carefully) or the returned value should be stored in the appropriate type variable.” and are just trying to make me figure it out for myself?^^ Because i dont get it :stuck_out_tongue:

If you assign the return value from a function call into a variable, the type of the variable should usually match the type of the variable. You should only rarely need to assign a value of one type to a variable of a different type.

Aaah :) I start to get i now! I will dig in to this tomorow! Maybe there is a little bit of hope in this project.