Ultrasonic sensors affected by sunlight ?

(deleted)

kevin1951:
I've built a three ultrasonic sensor robot lawnmower, it works perfectly indoors, but goes erratically as soon as I take it into sunlight.
I've tried swapping the sensors with new, but the problem still exists.
I'm running the three sensors from an Arduino Uno , could it possibly be a power supply problem.
I haven't added the code, but if needed I certainly shall.
Many thanks in advance for any help.

What is different about mowing an indoor lawn and mowing an outdoor lawn. Oh, you did not ACTUALLY test it indoor. Tell us about the difference between indoor and outdoor.
Paul

Are the ultrasonic sensors subject to any vibration?
Paul

Further reading: IF the motors spin left or right, but you are getting NOTHING from the ultrasonic sensors, what is controlling the motors?
Paul

How are you handling the "time out" on your sensors. Do you report it or what?
Paul

No, just tell me in words how you are handling the ultra sonic time-out.
Paul

What do you expect those ultrasonic sensors to see, exactly?

If there is nothing detected in front of the sensors, what are the motors supposed to do, and how is that different from what they actually do?

During the ping to read cycle is the ultrasonic sensor being moved? Moving the SR04 during a reading will cause erroneous readings.

You'll get better imaging/object detection by using a LIDAR, such as a LIDAR-Lite-v3.

sigh Code in code tags looks like this:

#include <NewPing.h>

int Echo1 = A3; //LEFT_SENSOR ECHO
int Trig1 = A2; //LEFT_SENSOR TRIG
int Echo2 = A5; //MID_SENSOR ECHO
int Trig2 = A4; //MID_SENSOR TRIG
int Echo3 = A1; //RIGHT_SENSOR ECHO
int Trig3 = A0; //RIGHT_SENSOR TRIG

int in1 = 5;
int in2 = 6;
int in3 = 9;
int in4 = 10;
int ENA = 3;
int ENB = 11;
int ABS = 100;
int Left_Distance = 0,Right_Distance = 0,Middle_Distance = 0 ;
void _mForward()
{
  digitalWrite(in1,HIGH);
  digitalWrite(in2,LOW);
  digitalWrite(in3,HIGH);
  digitalWrite(in4,LOW);
  analogWrite(ENA,ABS);
  analogWrite(ENB,ABS);
  Serial.println("ROBOT_MOVING_FORWARD");
}
void _mBack()
{
  digitalWrite(in1,LOW);
  digitalWrite(in2,HIGH);
  digitalWrite(in3,LOW);
  digitalWrite(in4,HIGH);
  analogWrite(ENA,ABS);
  analogWrite(ENB,ABS);
  Serial.println("ROBOT_MOVING_BACKWARD");
}
void _mleft()
{
  digitalWrite(in1,HIGH);
  digitalWrite(in2,LOW);
  digitalWrite(in3,LOW);
  digitalWrite(in4,LOW);
  analogWrite(ENA,ABS);
  analogWrite(ENB,ABS);
  Serial.println("ROBOT_MOVING_LEFT");
}
void _mright()
{
  digitalWrite(in1,LOW);
  digitalWrite(in2,LOW);
  digitalWrite(in3,HIGH);
  digitalWrite(in4,LOW);
  analogWrite(ENA,ABS);
  analogWrite(ENB,ABS);
  Serial.println("ROBOT_MOVING_RIGHT");
}
void _mStop()
{
  digitalWrite(in1,LOW);
  digitalWrite(in2,LOW);
  digitalWrite(in3,LOW);
  digitalWrite(in4,LOW);
  digitalWrite(ENA,LOW);
  digitalWrite(ENB,LOW);
  Serial.println("ROBOT_STOP");
}
/*Ultrasonic distance measurement Sub function*/
/* ref https://www.youtube.com/watch?v=9nrU2klJ59U */
/*
1. el delay luego del HIGH debería ser de 10ms
2. para tener la distancia en cm dividir entre 58 directamente (por las dudas)
3. al devolver la distancia como int se pierde precisión que tenía el float
*/
int Left_Distance_test()
{
  digitalWrite(Trig1, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig1, HIGH);
  delayMicroseconds(20);
  digitalWrite(Trig1, LOW);
  float Fdistance = pulseIn(Echo1, HIGH);
  delay(10);
  Fdistance = Fdistance/ 29 / 2;
  return (int)Fdistance;
}
int Middle_Distance_test()
{
  digitalWrite(Trig2, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig2, HIGH);
  delayMicroseconds(20);
  digitalWrite(Trig2, LOW);
  float Fdistance = pulseIn(Echo2, HIGH);
  delay(10);
  Fdistance= Fdistance/ 29 / 2;
  return (int)Fdistance;
}
int Right_Distance_test()
{
  digitalWrite(Trig3, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig3, HIGH);
  delayMicroseconds(20);
  digitalWrite(Trig3, LOW);
  float Fdistance = pulseIn(Echo3, HIGH);
  delay(10);
  Fdistance= Fdistance/ 29 / 2;
  return (int)Fdistance;
}

void setup()
{
  Serial.begin(9600);
  pinMode(Echo1, INPUT);
  pinMode(Trig1, OUTPUT);
  pinMode(Echo2, INPUT);
  pinMode(Trig2, OUTPUT);
  pinMode(Echo3, INPUT);
  pinMode(Trig3, OUTPUT);
  pinMode(in1,OUTPUT);
  pinMode(in2,OUTPUT);
  pinMode(in3,OUTPUT);
  pinMode(in4,OUTPUT);
  pinMode(ENA,OUTPUT);
  pinMode(ENB,OUTPUT);

  _mStop();
}
void loop()
{
  // no es necesario esperar entre las medidas, son independientes
  Left_Distance = Left_Distance_test();
  delay(10);
  Middle_Distance = Middle_Distance_test();
  delay(10);
  Right_Distance = Right_Distance_test();
  delay(10);
  Serial.println("Left_Distance\tMiddle_Distance\tRight_Distance\tStatus\n");
  Serial.print(Left_Distance);
  Serial.print("cm\t\t");
  Serial.print(Middle_Distance);
  Serial.print("cm\t\t");
  Serial.print(Right_Distance);
  Serial.print("cm\t\t");
  if(Middle_Distance<=30)
  {
    /* _mStop();
    delay(1000);*/
    if(Right_Distance>Left_Distance)
    {
      if((Right_Distance<=20)&&(Left_Distance<=20))
      {
        _mStop();
        delay(100);
        _mBack();
        delay(100);
      }
      else
      {
        // _mBack();
        // delay(3000);
        _mright();
        delay(100);}
      }
      else if(Right_Distance<Left_Distance)
      {
        if((Right_Distance<=30)&&(Left_Distance<=20))
        {
          _mStop();
          delay(100);
          _mBack();
          delay(100);
        }
        else
        {
          // _mBack();
          // delay(100);
          _mleft();
          delay(100);
        }
      } // RD < LD
    } // RD > LD
    else if(Right_Distance<=20)
    {
      _mleft();
      delay(100);
    }
    else if(Left_Distance<=20)
    {
      _mright();
      delay(100);
    }
    else // creo que este else es del MD <= 30 pero el codigo no estaba indentado
    {
      _mForward();
    }
    //delay(100);
  } // MD <= 30
 // loop

Now see how much easier it is to copy the code into a sketch and check it out for issues?

What part of the code handles a no echo return reading?

READ THIS BEFORE YOU ASK A QUESTION

This is what happens when you do not

The best analogy I can make for this forum is, it's like a magic shop. you don't just walk in, flash the platinum card, and walk out with "Saw the lady in half". you show the magic you have, we guide you further down the path.

an essential lesson of that magic is reading 7. If you are posting code or error messages, use "code" tags

Perhaps I am blind. You include the newPing library, but never implement it or use it's functions. Also you use the Pulseln() function without ever setting the time-out value. This is why I asked you to describe the time-out you were using. The Pulseln() function, as I recall, will always block your code until the time-out occurs, which is 1 second.
Paul

  Left_Distance = Left_Distance_test();
  delay(10);
  Middle_Distance = Middle_Distance_test();
  delay(10);
  Right_Distance = Right_Distance_test();
  delay(10);

Try increasing the delays to around 30 to 40 milliseconds.

Try decreasing the size of your code.

Here is a link to the pulsln() documentation: http://www.arduino.cc/reference/en/language/functions/advanced-io/pulsein/?setlang=it.
Please change you usage of each pulseln() to include the timeout value you need. It is microseconds and relates directly to the speed of sound.
The program also seems to be wanting a float form pulseln() and documentation tells us it returns a long integer.
The documentation also tells us ZERO is returned if the function times out. I do not see any code in your program that is looking for a ZERO to be returned.
So many problems!
Paul

Did you add the timeout to the pulseIn() as well?
There's no need to wait a full second for an echo. A timeout of 10000-20000 (10-20 ms) is enough - if no echo received in that period, it won't ever arrive.

TheMemberFormerlyKnownAsAWOL:
Try increasing the delays to around 30 to 40 milliseconds.

kevin1951:
It worked, thank you so much, this has meant the success or failure of months of effort in my project.

It puts an end to literally months of frustration

But....do you know why it worked?

You should also study Paul_KD7HB and wvmarle's posts and try to figure out why they said what they did, assuming you are serious about learning and not ok with just trying random things without understanding what you are doing. That may also help you avoid additional frustration on this project.

And, just doing something as simple as googling "how does the hc-sr04 work" could have given you a huge step up (and may help you answer the question: why did increasing the delay between pings seem to solve my problem?)

I don't understand much about coding at all

To avoid months of frustration on future projects associated with this lack of knowledge, do some self-education. Read and attempt to comprehend all of the "stickies" on this forum (those are the posts at the top of every section of this forum with the bold titles, including "How to get the best out of this forum," "BEGINNERS and LEARNERS read this...," and "Read this before posting a programming question..." Spend some quality time with the reference section, too, looking at all of the available functions, values, and structure items: Arduino Reference - Arduino Reference And, go through the example sketches and attempt to understand them.

But to know how to impart that knowledge, we have to know where we’re starting from.

If you don’t understand the simple concepts behind an ultrasound ranger (they are not typically affected by sunshine, in my experience), we may have to take a further step back.

Just how many steps back we need to take to find common ground, we simply don’t know at this stage.

Just throwing up your hands, and saying “I don’t know” is almost as bad as saying “it doesn’t work” - if you gave us at least a idea of the extent of your knowledge, beyond Old Testament commentary, it makes it easier to tailor responses.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.