Code for obstacle avoidance with TSOP sensors

Hey people,

I have written a code for an autonomous obstacle avoiding robot. It uses two TSOP general purpose proximity sensors with digital output. Each sensor module consists of a IR emitter and TSOP receiver pair. One for obstacles on left, and the other for right.
The TSOP receiver detects a signal of fixed frequency and therefore it works very efficiently in ambient lighting also.
It also uses a H-Bridge L293D Motor Controller to control the left and right motors which use 6v each.

I still haven't bought the sensor modules for I am not sure whether it will work or not.

Could you guys please verify the code for me once and tell me will it work or not? Is there anything I would have to change?

// Left Motor Controls
# define L1  4    // Left Positive
# define L2  5    // Left Negetive
// Right Motor Controls
# define R1  6    // Right Positive
# define R2  7   // Right Negetive
// TSOP Sensors
# define Ls   A0
# define Rs   A1
// Variables to store sensor data
int ls_val;
int rs_val;

// Declaring INPUT and OUTPUT pins
void setup() {
  Serial.begin(9600);
  pinMode (L1, OUTPUT);
  pinMode (L2, OUTPUT);
  pinMode (R1, OUTPUT);
  pinMode (R2, OUTPUT);
  
  pinMode (Ls, INPUT);
  pinMode (Rs, INPUT);
}

void loop() {
  ls_val = digitalRead(Ls);        // Read data from Left Sensor
  rs_val = digitalRead(Rs);        // Read data from Right Sensor
  
  if (ls_val != rs_val && ls_val == HIGH) {            // If the Left Sensor detects an obstacle go Right 
    digitalWrite (L1, LOW);
    digitalWrite (L2, HIGH);
    digitalWrite (R1, HIGH);
    digitalWrite (R2, LOW);
    Serial.println ("RIGHT");
  }
  
  if (ls_val != rs_val && rs_val == HIGH) {           // If the Right Sensor detects an obstacle go Left 
    digitalWrite (L1, LOW);
    digitalWrite (L2, HIGH);
    digitalWrite (R1, HIGH);
    digitalWrite (R2, LOW);
    Serial.println ("LEFT");
  }
  
  else if (ls_val == LOW && rs_val == LOW) {           // If both the sensors dont detect any obstacle go Forward  
    digitalWrite (L1, HIGH);
    digitalWrite (L2, LOW);
    digitalWrite (R1, HIGH);
    digitalWrite (R2, LOW);
    Serial.println ("FORWARD");
  }
  
  else {                                              // If both the sensors detect obstacles turn around
    digitalWrite (L1, LOW);
    digitalWrite (L2, HIGH);
    digitalWrite (R1, HIGH);
    digitalWrite (R2, LOW);
    delay(3000);
    Serial.println ("DEAD END");
  }
}

Also can I use this (ls_val == HIGH && rs_val == LOW) instead of (ls_val != rs_val && ls_val == HIGH)

Thanks In Advance,
SolidSnake31295

The TSOP receiver detects a signal of fixed frequency and therefore it works very efficiently in ambient lighting also.

I still haven't bought the sensor modules for I am not sure whether it will work or not.

If you haven't yet bought them, how do you know their performance?

Could you guys please verify the code for me once and tell me will it work or not?

The IDE's compiler will verify the code for you - you don't need hardware, but to find out if it will work, then you need the hardware.

Why are you so fixated on the sensor's package?

I know its performance because: http://www.robosoftsystems.co.in/roboshop/index.php/sensors/single-tsop-sensor-module.html

Why I am fixated on it:

  1. It is cheap.
  2. Doesn't get affected by ambient lighting.

Doesn't get affected by ambient lighting.

No the link says:-

Due to this, errors due to false detection of ambient light are significantly reduced.

Normally TSOP is the name of a particular package shape however here TSOP might mean it uses a Vishy TSOP21xx sensor. If so then those things don't cope too well with a continuous modulated beam. That is it will not give a continuous indication of an object just the first time it sees it.

But the question is will it work with the code?

Reply #1 says

but to find out if it will work, then you need the hardware.

We have not got the hardware.
Just a point, you are using the analogue inputs as digital ones, but you define the pin numbers as if they are analogue so no it will not work.

but you define the pin numbers as if they are analogue so no it will not work.

No, that's fine.

Just a point, you are using the analogue inputs as digital ones, but you define the pin numbers as if they are analogue so no it will not work.

Yes, they will...

OK it's just that when I tried compiling the code I got the error message
A0 was not declared in this scope
When setting the pin mode to input
I am using Arduino 0022
What are you using?

Edit.
But then on the other hand if you define your arduino board as a 168 then it will compile. Just looked at Wprogram.h and A0 does resolve to 14 so it should be alright. Sorry

Grumpy_Mike:
Sorry

No Prob. :slight_smile: