Go Down

Topic: How to avoid obstacle with 2x IR (Read 1 time) previous topic - next topic

Stealth0113

Nov 09, 2011, 09:19 pm Last Edit: Nov 09, 2011, 09:21 pm by Stealth0113 Reason: 1
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.
Code: [Select]

//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.
Watch my stealth-bot @ let's make robots

jraskell

Quote
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.


So instead of just printing out the values returned from your calls to irRead, store them in two variables you define as irDetectLeft and irDetectRight.  Then you can use those two variables as conditions for your turn left and turn right actions.  IE... (pseudocode)

if irDetectLeft and not irDetectRight
    turn Right

if irDetectRight and not irDetectLeft
    turn Left

if irDetectLeft and irDetectRight
    Stop.

Stealth0113

I've changed the code so i get "irReadLeft" and "irReadRight". But my sketch is very long to do only this job.

Is there any way to shorten this??

Code: [Select]

//define pins. I used pins 4 and 5
#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(irReadLeft(irSensorPinLeft, irLedPinLeft)); //display the results
  Serial.println(irReadRight(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 irReadLeft(int readPinLeft, int triggerPinLeft)
{
  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(triggerPinLeft, HIGH);
    delayMicroseconds(halfPeriod);
    digitalWrite(triggerPinLeft, LOW);
    delayMicroseconds(halfPeriod - 1);     // - 1 to make up for digitaWrite overhead   
  }
  return digitalRead(readPinLeft);
}

int irReadRight(int readPinRight, int triggerPinRight)
{
  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(triggerPinRight, HIGH);
    delayMicroseconds(halfPeriod);
    digitalWrite(triggerPinRight, LOW);
    delayMicroseconds(halfPeriod - 1);     // - 1 to make up for digitaWrite overhead   
  }
  return digitalRead(readPinRight);
}

Watch my stealth-bot @ let's make robots

AWOL

You could get rid of one of those IR read functions.
That would shorten it
"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.

Stealth0113

This is wat i've already got for now...

Code: [Select]

#include <Servo.h>

Servo leftServo;
Servo rightServo;

//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


void setup()
{
  tone(5, 440, 1000);
  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(irReadLeft(irSensorPinLeft, irLedPinLeft)); //display the results
  Serial.println(irReadRight(irSensorPinRight, irLedPinRight)); //display the results
  delay(10); //wait for the string to be sent*/
 
  int pulseleft;
  int pulseright;
  int (irReadLeft(irSensorPinLeft, irLedPinLeft));
  int (irReadRight(irSensorPinRight, irLedPinRight));
 
  if (irReadLeft == 0 and irReadRight == 0)
  { //Backup
   pulseleft = 1400;
   pulseright = 1600;   
  }
  else if (irReadLeft == 0)
  { //turn right
   pulseleft = 1400;
   pulseright = 1400;
  }
  else if (irReadRight == 0)
  { //turn left
   pulseleft = 1600;
   pulseright = 1600;
  }
  else
  {
    pulseleft = 1600;
    pulseleft = 1400;
  }
 
{
  leftServo.attach(9);
  leftServo.writeMicroseconds(pulseleft);
  rightServo.attach(8);
  rightServo.writeMicroseconds(pulseright);
}
}


int irReadLeft(int readPinLeft, int triggerPinLeft)
{
  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(triggerPinLeft, HIGH);
    delayMicroseconds(halfPeriod);
    digitalWrite(triggerPinLeft, LOW);
    delayMicroseconds(halfPeriod - 1);     // - 1 to make up for digitaWrite overhead   
  }
  return digitalRead(readPinLeft);
}

int irReadRight(int readPinRight, int triggerPinRight)
{
  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(triggerPinRight, HIGH);
    delayMicroseconds(halfPeriod);
    digitalWrite(triggerPinRight, LOW);
    delayMicroseconds(halfPeriod - 1);     // - 1 to make up for digitaWrite overhead   
  }
  return digitalRead(readPinRight);
}


But de if...else string doesn't get any data. The serial.printin shows the data on the serial monitor and the ir sensors seem to respond well. But it wont reach the if...else string. My robot turns only left for some reason...

Any help?
Watch my stealth-bot @ let's make robots

AWOL

What do the two lines after the declaration of pulseleft and pulseright do?
"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.

wildbill

You are using the irRead functions addresses, not what they return - that's the reason your ifs are misbehaving

First, follow AWOL's suggestion - your IRread routines are identical. Drop one of them and rename the other irRead

Then replace:
Code: [Select]

 int (irReadLeft(irSensorPinLeft, irLedPinLeft));
 int (irReadRight(irSensorPinRight, irLedPinRight));


with this:
Code: [Select]

 int irReadLeft = irRead(irSensorPinLeft, irLedPinLeft);
 int irReadRight = irRead(irSensorPinRight, irLedPinRight);

Stealth0113

Thanks for the help!

It runs a bit buggy... I think the problem is the IR Sensor or Detector...

Anyway this problem is solved so the topic can be closed!
Watch my stealth-bot @ let's make robots

Go Up