Surprisingly my code doesn't work

Hello !

Fisrt thank you for reading this and I'm even more greatful if you can help me.

So, I'm currently building a robot which avoid walls and obtacles using infrared LED. At first I just want my robot to stop when any obtacle is detected, even if it's just by one LED.

But when I compile my code, there is just nothing going on and the robot dont move even if the way is clear.

My code :

#include "AlphaBot.h"

int LSensorPin = 7;
int RSensorPin = 8;

int LSensor;              //Left Infrared Proximity Sensor signal value
int RSensor;              //Right Infrared Proximity Sensor signal value

AlphaBot Car1 = AlphaBot();

void ProximityConfig()
{
  pinMode(RSensorPin, INPUT);   //Define the input pin of Right Infrared Proximity Sensor
  pinMode(LSensorPin, INPUT);   //Define the input pin of Left Infrared Proximity Sensor
}

void setup()
{
  ProximityConfig();
  Car1.SetSpeed(0);
//  Serial.begin(9600);
}

void loop()
{
  RSensor = digitalRead(RSensorPin);            
  LSensor = digitalRead(LSensorPin);

  
  if (LSensor == HIGH && RSensor == HIGH)   //If both sensor are clear, run forward
    Car1.Forward();
  else if (LSensor == LOW or RSensor == LOW)   //If the right or left sensor has a signal, stop the robot
    Car1.SetSpeed(0);
 
  
}

Can anyone figure out what's going on ?

I was studying state machines a few days ago, and found this article talking about doing almost exactly what you are trying to do. It doesn't fix your code, but it might be worth a read.

Swimingpoule:
if (LSensor == HIGH && RSensor == HIGH) //If both sensor are clear, run forward
Car1.Forward();
else if (LSensor == LOW or RSensor == LOW) //If the right or left sensor has a signal, stop the robot
Car1.SetSpeed(0);

In the else-if condition, that word "or" doesn't strike me as being correct. Try replacing the "or" with "||" and see what happens.

If the program still does not run as you intend, after reading in the two sensors, try to print them to the serial monitor and see what values it gives when nothing is in front and when you wave your hand in front of the sensor.

bos1714:
In the else-if condition, that word "or" doesn't strike me as being correct. Try replacing the "or" with "||" and see what happens.

If "or" isn't correct, the compiler would spit it out, and the code wouldn't run at all.

There is nothing wrong with "or".

bos1714:
In the else-if condition, that word "or" doesn't strike me as being correct. Try replacing the "or" with "||" and see what happens.

If the program still does not run as you intend, after reading in the two sensors, try to print them to the serial monitor and see what values it gives when nothing is in front and when you wave your hand in front of the sensor.

If the or was wrong nothing would work I think, but my program does compile, but do nothing

I tried to print on the serial monitor but nothing there either

#include "AlphaBot.h"

int LSensorPin = 7;
int RSensorPin = 8;

int LSensor;              //Left Infrared Proximity Sensor signal value
int RSensor;              //Right Infrared Proximity Sensor signal value

AlphaBot Car1 = AlphaBot();

void ProximityConfig()
{
  pinMode(RSensorPin, INPUT);   //Define the input pin of Right Infrared Proximity Sensor
  pinMode(LSensorPin, INPUT);   //Define the input pin of Left Infrared Proximity Sensor
}

void setup()
{
  ProximityConfig();
  Car1.SetSpeed(0);
//  Serial.begin(9600);
}

void loop()
{

  Serial.print("RSensor: ");
  Serial.println(RSensor);
  Serial.print("LSensor: ");
  Serial.println(LSensor);

  RSensor = digitalRead(RSensorPin);            
  LSensor = digitalRead(LSensorPin);

  
  if (LSensor == HIGH && RSensor == HIGH)   //If both sensor are clear, run forward
    Car1.Forward();
  else if (LSensor == LOW or RSensor == LOW)   //If the right or left sensor has a signal, stop the robot
    Car1.SetSpeed(0);
 
  
}

What kind if IR sensor are you using?

Mark

TolpuddleSartre:
If "or" isn't correct, the compiler would spit it out, and the code wouldn't run at all.

There is nothing wrong with "or".

"or" is not listed in Arduino Reference - Arduino Reference

so I am not sure why it works, but it does

It is valid in Visual Studio ( Logical OR operator: || | Microsoft Docs ) but I very rarely see it used in C++ only with Visual Basic.

Use || logical OR.

.

Swimingpoule:
If the or was wrong nothing would work I think, but my program does compile, but do nothing

I tried to print on the serial monitor but nothing there either

void setup()

{
  ProximityConfig();
  Car1.SetSpeed(0);
//  Serial.begin(9600);
}

It seems that you have your serial communication start command commented out. Delete the two slashes to enable serial printing.

Swimingpoule:

  Serial.print("RSensor: ");

Serial.println(RSensor);
  Serial.print("LSensor: ");
  Serial.println(LSensor);

RSensor = digitalRead(RSensorPin);           
  LSensor = digitalRead(LSensorPin);

It also might benefit to place the serial printing after the data has been read, just so that the first printing is valid data.

So I modify according to all the informations you gave me and in the monitor I have this :

LSensor: 0
RSensor: 0
LSensor: 0
RSensor: 0
LSensor: 0
RSensor: 0
LSensor: 0
RSensor: 0
LSensor: 0
RSensor: 0
LSensor: 0
RSensor: 0
LSensor: 0
RSensor: 0
LSensor: 0
RSensor: 0

repeatedly even when i wave my hand in front of the sensors

int LSensorPin = 7;
int RSensorPin = 8;

int LSensor;              //Left Infrared Proximity Sensor signal value
int RSensor;              //Right Infrared Proximity Sensor signal value

AlphaBot Car1 = AlphaBot();

void ProximityConfig()
{
  pinMode(RSensorPin, INPUT);   //Define the input pin of Right Infrared Proximity Sensor
  pinMode(LSensorPin, INPUT);   //Define the input pin of Left Infrared Proximity Sensor
}

void setup()
{
  ProximityConfig();
  Car1.SetSpeed(0);
  Serial.begin(9600);
}

void loop()
{
  RSensor = digitalRead(RSensorPin);            
  LSensor = digitalRead(LSensorPin);

  Serial.print("RSensor: ");
  Serial.println(RSensor);
  Serial.print("LSensor: ");
  Serial.println(LSensor);

  
  if (LSensor == HIGH && RSensor == HIGH)   //If the right sensor has a signal,turn left
    Car1.Forward();
  else if (LSensor == LOW || RSensor == LOW)   //If the right sensor has a signal,turn left
    Car1.SetSpeed(0);
 
  
}

holmes4:
What kind if IR sensor are you using?

Mark

Got a picture, but not sure how useful it is

Image1.png

Is there a model number somewhere on the little board, maybe I can find a user manual for you.

In my experience however, IR sensor outputs usually go to an analog pin, which allows one to determine distance based on how much of the transmitted light returns to the sensor. It is possible that the potentiometer on the board (the little blue box with the white thing) determines a threshold, and the output will give a 1 or 0 depending on whether the reflectivity is higher or lower than the threshold.

That unit is NOT a distance sensor and will not be of use!

Mark

I'm using this robot : https://www.waveshare.com/w/upload/c/c7/AlphaBot-User-Manual.pdf

holmes4:
That unit is NOT a distance sensor and will not be of use!

Mark

My goal is not to know the distance, I just want my device to stop whenever there is an obtacle, and that's what those sensors are made for.

OP's picture:
Image1.png

Swimingpoule:
My goal is not to know the distance, I just want my device to stop whenever there is an obtacle, and that's what those sensors are made for.

And yet:

LSensor: 0
RSensor: 0
LSensor: 0
RSensor: 0
LSensor: 0
RSensor: 0
LSensor: 0
RSensor: 0
LSensor: 0
RSensor: 0
LSensor: 0
RSensor: 0
LSensor: 0
RSensor: 0
LSensor: 0
RSensor: 0

You might consider adding 'pinMode()' and perhaps PULLUP to your 'setup()'.

Btw, can someone actually help me find out why my code isn't working and how to fixe it ? Cause I've tried everything I could and still doesn't work

Can you look on the little blue IR sensor board for any model numbers or identification of any kind? That way I can look up some user guides or similar references.

Let's skip the code for a minute.

Since the connector is shrouded I cannot tell, but I would think it is a three pin connector, power, ground, and output.

The board appears to have two leds on it, power and out(put). Do they change or work? Can you get the OUT led to light?

Once you can get the out led to light, check with a voltmeter and report the voltages output by the board with the led on and off?

Have you calibrated your sensor? It has a tiny pot on it. You use that to set the sensitivity, or range at which it should report an object.

Power the sensor.
Place an object near the sensor.
Turn the pot carefully to increase or decrease the sensitivity.
The DAT led should light up.

Experiment
Move the object closer and further away.
Try different values of the pot.
Make sure that the sensor triggers at an acceptable range for your application.