I've got a Arduino Uno with a Ping sensor and 2 InfraRed sensors on the left and right front of my robot.
I want it to drive autonomous and avoiding objects with Ping (works already), but i want IR as backup system.
i've already the code for detecting objects with IR but i can't get them work together, so i can't detect left and right.
This is what i already got.
//define pins.
#define irLedPinLeft 30 // IR Led on this pin
#define irSensorPinLeft 29 // IR sensor on this pin
#define irLedPinRight 4 // IR Led on this pin
#define irSensorPinRight 3 // IR Sensor on this pin
int irRead(int readPin, int triggerPin); //function prototype
void setup()
{
pinMode(irSensorPinLeft, INPUT);
pinMode(irLedPinLeft, OUTPUT);
pinMode(irSensorPinRight, INPUT);
pinMode(irLedPinRight, OUTPUT);
Serial.begin(9600);
// prints title with ending line break
Serial.println("Program Starting");
// wait for the long string to be sent
delay(100);
}
void loop()
{
Serial.println(irRead(irSensorPinLeft, irLedPinLeft)); //display the results
Serial.println(irRead(irSensorPinRight, irLedPinRight)); //display the results
delay(10); //wait for the string to be sent
}
/******************************************************************************
* This function can be used with a panasonic pna4602m ir sensor
* it returns a zero if something is detected by the sensor, and a 1 otherwise
* The function bit bangs a 38.5khZ waveform to an IR led connected to the
* triggerPin for 1 millisecond, and then reads the IR sensor pin to see if
* the reflected IR has been detected
******************************************************************************/
int irRead(int readPin, int triggerPin)
{
int halfPeriod = 13; //one period at 38.5khZ is aproximately 26 microseconds
int cycles = 38; //26 microseconds * 38 is more or less 1 millisecond
int i;
for (i=0; i <=cycles; i++)
{
digitalWrite(triggerPin, HIGH);
delayMicroseconds(halfPeriod);
digitalWrite(triggerPin, LOW);
delayMicroseconds(halfPeriod - 1); // - 1 to make up for digitaWrite overhead
}
return digitalRead(readPin);
}
What i want is a "irDetectLeft" and a "irDetectRight" so i can use a if..else loop to turn left on a irDetectRight, and turn right on a irDetectLeft.