Pages: [1]   Go Down
Author Topic: How to avoid obstacle with 2x IR  (Read 1790 times)
0 Members and 1 Guest are viewing this topic.
Netherlands
Online Online
Jr. Member
**
Karma: 0
Posts: 80
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//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.
« Last Edit: November 09, 2011, 03:21:20 pm by Stealth0113 » Logged

Watch my stealth-bot @ let's make robots

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Netherlands
Online Online
Jr. Member
**
Karma: 0
Posts: 80
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//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);
}
Logged

Watch my stealth-bot @ let's make robots

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26632
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could get rid of one of those IR read functions.
That would shorten it
Logged

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

Netherlands
Online Online
Jr. Member
**
Karma: 0
Posts: 80
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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?
Logged

Watch my stealth-bot @ let's make robots

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26632
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What do the two lines after the declaration of pulseleft and pulseright do?
Logged

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

New Jersey
Offline Offline
Faraday Member
**
Karma: 72
Posts: 3763
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 int (irReadLeft(irSensorPinLeft, irLedPinLeft));
  int (irReadRight(irSensorPinRight, irLedPinRight));

with this:
Code:
 int irReadLeft = irRead(irSensorPinLeft, irLedPinLeft);
  int irReadRight = irRead(irSensorPinRight, irLedPinRight);
Logged

Netherlands
Online Online
Jr. Member
**
Karma: 0
Posts: 80
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Watch my stealth-bot @ let's make robots

Pages: [1]   Go Up
Jump to: