Switching from a PING))) sensor to IR sensors

I tried switching code from using a PING))) to using IR sensors. I’m new to all of this, so I was just wondering if I have to instruct the program how to use the IR sensors, or if everything that’s been used for the PING))) will be the same.

Here is the code (with the right source) but with my edits.

/*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 irreceiverPin = 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 ir receiver pan to center

}

void loop()

{

int distanceFwd = irreceiver();

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 = irreceiver(); //scan to the right

delay(500);

panMotor.write(180);

delay(700);

leftDistance = irreceiver(); //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 irreceiver()

{

// Send out IR waves/ signal pulse

pinMode(irreceiverPin, OUTPUT);

digitalWrite(irreceiverPin, LOW);

delayMicroseconds(2);

digitalWrite(irreceiverPin, HIGH);

delayMicroseconds(5);

digitalWrite(irreceiverPin, LOW);

//Get duration it takes to receive echo

pinMode(irreceiverPin, INPUT);

duration = pulseIn(irreceiverPin, HIGH);

//Convert duration into distance

return duration / 29 / 2;

}

You certainly will have to use different code to ‘talk’ to the IR sensor and then to convert it’s data into some units useful to your code. After that I suspect the rest of your code would be the same. Almost as certain is that someone has written this interface code and published it as a library for use w/whatever sensor you’re using (that’s a hint).

You’ll find it better if you used the code button when posting code.

(click on to open)

It’s usually helpfull if you first explain exactly what the IR sensor is that you have. As I can think of a few different types of IR detectors. And not all can provide a distance value. I had to dig into your code to figure out from the comment what the device could be.

const int irPin = 0; //Sharp infrared sensor pin

If you read the datasheet/documentation of the specific sensor (also a hint :wink: )than you would have noticed that it is not really necessary to send out a pulse to get a range value, like you would for the ultrasonic range sensor. (Though being able to turn it of somehow would be beneficial for battery-life. But that is beside your question)