a02yyuw waterproof ultrasonic sensor

Hey Narwoq here!

I'am relatively new to Arduino and Programming all together, i've worked myself into it for my Matura thesis (like A-Level thesis). Im working on a Robot, that searches a square space for a certain object (e.g. a tennis ball).

The idea: It follows an algorithm to move through the space (Image: Algorithm) with the help of three a02yyuw waterproof ultrasonic sensors (A02YYUW Waterproof Ultrasonic Sensor Wiki - DFRobot). As soon as the Pixy2 (an intelligent camera) sees the object its own code will take over. I HAVEN'T implementet that yet. I'm aware that this code isnt perfect but thats just for some background information.

The problem:If set up and used individually all three sensors work as expected. If i try to use them together they will return 0.00. I searched up this problem and found another post that proposed to put a delay(10) between the Serial.listen and the Serial.read functions. I did that and i worked... but only when i only run the "geradeausbis30()" function (its german and means straightforwarduntil30) as soon as i want to run the main function (haupt()), which starts with the geradeausbis30 it behaves completly different, alltough in my opinion it shouldnt have any impact on the result.

geradeausbis30():

void geradeausbis30() {
  forward();
  while (frontdistance() >= 300) {
    Serial.print("Weiter");
  }
  halt();
}

The Serial output with only the the geradeausbis30() function is like following:
Forward
WeiterWeiterWeiterWeiter (until i put an object in front)
Halt
(and it repeats that way)
Thats how its supposed to work.

When i use the main function though it gives me this:
Forward
Halt
TurnRight
meaning it doesnt even get to the

while (frontdistance() >= 300) {
    Serial.print("Weiter");
  }

Why does it change its behaviour?

The phot of the Excel-Document gives you information about the connections.

I had to split the post because its too long.

I hope thats enough information?

Thanks for every answer!

Narwoq

Photo Sensor.PNG

Photo Sensor.PNG

Code:

#include <SoftwareSerial.h>
#include <SPI.h>
//Sensor
SoftwareSerial front(11, 35); // RX(grün), TX(blau)
SoftwareSerial left (12, 37); // RX(grün), TX(blau)
SoftwareSerial right(13, 39); // RX(grün), TX(blau)
unsigned char dataF[4] = {};
float distancef;
unsigned char dataL[4] = {};
float distancel;
unsigned char dataR[4] = {};
float distancer;

//Drive
int rEnA = 2 ;
int rIn1 = 27  ;
int rIn2 = 29  ;
int rEnB = 3 ;
int rIn4 = 23  ;
int rIn3 = 25  ;
int lEnA  = 4 ;
int lIn1  = 22  ;
int lIn2  = 24  ;
int lEnB  = 5 ;
int lIn4  = 26  ;
int lIn3  = 28  ;
int m;
int t = 1; //turn count

//algorithmus
bool fertig = false;


void setup() {
  Serial.begin(9600);
  front.begin(9600);
  left.begin(9600);
  right.begin(9600);
  //Drive
  pinMode(rEnA, OUTPUT);
  pinMode(rIn1, OUTPUT);
  pinMode(rIn2, OUTPUT);
  pinMode(rEnB, OUTPUT);
  pinMode(rIn4, OUTPUT);
  pinMode(rIn3, OUTPUT);
  pinMode(lEnA, OUTPUT);
  pinMode(lIn1, OUTPUT);
  pinMode(lIn2, OUTPUT);
  pinMode(lEnB, OUTPUT);
  pinMode(lIn4, OUTPUT);
  pinMode(lIn3, OUTPUT);
}

void loop() {
  haupt();
  //geradeausbis30();
}




float  frontdistance() {
  front.listen();
  delay(10);
  do {
    for (int f = 0; f < 4; f++)
    {
      dataF[f] = front.read();
    }
  } while (front.read() == 0xff);

  front.flush();

  if (dataF[0] == 0xff)
  {
    int sum;
    sum = (dataF[0] + dataF[1] + dataF[2]) & 0x00FF;
    if (sum == dataF[3])
    {
      distancef = (dataF[1] << 8) + dataF[2];
      return distancef;
    }
  }
}

float  leftdistance() {
  left.listen();
  delay(10);
  do {
    for (int l = 0; l < 4; l++)
    {
      dataL[l] = left.read();
    }
  } while (left.read() == 0xff);

  left.flush();

  if (dataL[0] == 0xff)
  {
    int sum;
    sum = (dataL[0] + dataL[1] + dataL[2]) & 0x00FF;
    if (sum == dataL[3])
    {
      distancel = (dataL[1] << 8) + dataL[2];
      return distancel;
    }
  }
}

float  rightdistance() {
  right.listen();
  delay(10);
  do {
    for (int r = 0; r < 4; r++)
    {
      dataR[r] = right.read();
    }
  } while (right.read() == 0xff);

  right.flush();

  if (dataR[0] == 0xff)
  {
    int sum;
    sum = (dataR[0] + dataR[1] + dataR[2]) & 0x00FF;
    if (sum == dataR[3])
    {
      distancer = (dataR[1] << 8) + dataR[2];
      return distancer;
    }
  }
}

void turnRight() { //4
  Serial.println("TurnRight");
  if (m != 4) {
    digitalWrite(rIn1, LOW);
    digitalWrite(rIn2, LOW);
    digitalWrite(rIn4, LOW);
    digitalWrite(rIn3, LOW);
    digitalWrite(lIn1, LOW);
    digitalWrite(lIn2, LOW);
    digitalWrite(lIn4, LOW);
    digitalWrite(lIn3, LOW);
    m = 4;
  }
  //rechts r
  analogWrite(rEnA, 250);
  digitalWrite(rIn1, HIGH);
  digitalWrite(rIn2, LOW);
  analogWrite(rEnB, 250);
  digitalWrite(rIn4, HIGH);
  digitalWrite(rIn3, LOW);
  //left v
  analogWrite(lEnA, 250);
  digitalWrite(lIn1, LOW);
  digitalWrite(lIn2, HIGH);
  analogWrite(lEnB, 250);
  digitalWrite(lIn4, LOW);
  digitalWrite(lIn3, HIGH);
}

void turnLeft() { //3
  Serial.println("TurnLeft");
  if (m != 3) {
    digitalWrite(rIn1, LOW);
    digitalWrite(rIn2, LOW);
    digitalWrite(rIn4, LOW);
    digitalWrite(rIn3, LOW);
    digitalWrite(lIn1, LOW);
    digitalWrite(lIn2, LOW);
    digitalWrite(lIn4, LOW);
    digitalWrite(lIn3, LOW);
    m = 3;
  }
  //links r
  analogWrite(lEnA, 250);
  digitalWrite(lIn1, HIGH);
  digitalWrite(lIn2, LOW);
  analogWrite(lEnB, 250);
  digitalWrite(lIn4, HIGH);
  digitalWrite(lIn3, LOW);
  //rechts v
  analogWrite(rEnA, 250);
  digitalWrite(rIn1, LOW);
  digitalWrite(rIn2, HIGH);
  analogWrite(rEnB, 250);
  digitalWrite(rIn4, LOW);
  digitalWrite(rIn3, HIGH);
}

void halt() {
  Serial.println("Halt");
  digitalWrite(rIn1, LOW);
  digitalWrite(rIn2, LOW);
  digitalWrite(rIn4, LOW);
  digitalWrite(rIn3, LOW);
  digitalWrite(lIn1, LOW);
  digitalWrite(lIn2, LOW);
  digitalWrite(lIn4, LOW);
  digitalWrite(lIn3, LOW);
}

void forward() {  //1
  Serial.println("Forward");
  if (m != 1) {
    digitalWrite(rIn1, LOW);
    digitalWrite(rIn2, LOW);
    digitalWrite(rIn4, LOW);
    digitalWrite(rIn3, LOW);
    digitalWrite(lIn1, LOW);
    digitalWrite(lIn2, LOW);
    digitalWrite(lIn4, LOW);
    digitalWrite(lIn3, LOW);
    m = 1;
  }
  //rechts v
  analogWrite(rEnA, 250);
  digitalWrite(rIn1, LOW);
  digitalWrite(rIn2, HIGH);
  analogWrite(rEnB, 250);
  digitalWrite(rIn4, LOW);
  digitalWrite(rIn3, HIGH);
  //links v
  analogWrite(lEnA, 250);
  digitalWrite(lIn1, LOW);
  digitalWrite(lIn2, HIGH);
  analogWrite(lEnB, 250);
  digitalWrite(lIn4, LOW);
  digitalWrite(lIn3, HIGH);

}

void backwards() { //2
  Serial.println("Backwards");
  if (m != 2) {
    digitalWrite(rIn1, LOW);
    digitalWrite(rIn2, LOW);
    digitalWrite(rIn4, LOW);
    digitalWrite(rIn3, LOW);
    digitalWrite(lIn1, LOW);
    digitalWrite(lIn2, LOW);
    digitalWrite(lIn4, LOW);
    digitalWrite(lIn3, LOW);
    m = 2;
  }
  //rechts r
  analogWrite(rEnA, 250);
  digitalWrite(rIn1, HIGH);
  digitalWrite(rIn2, LOW);
  analogWrite(rEnB, 250);
  digitalWrite(rIn4, HIGH);
  digitalWrite(rIn3, LOW);
  //links r
  analogWrite(lEnA, 250);
  digitalWrite(lIn1, HIGH);
  digitalWrite(lIn2, LOW);
  analogWrite(lEnB, 20);
  digitalWrite(lIn4, HIGH);
  digitalWrite(lIn3, LOW);

}

void kurveRsensorR () {
  float prev = rightdistance();
  float next = prev;


  bool kleiner = false;
  bool groessernachkl = false;

  turnRight();
  while ((kleiner && groessernachkl) == false) {
    prev = next;
    delay(10);
    next = rightdistance();
    kleiner = prev > next;
    groessernachkl = kleiner && (prev <= next);
  }
  halt();
}


void kurveRsensorL () {
  float prev = leftdistance();
  float next = prev;

  bool unter50 = false;
  bool kleiner = false;
  bool groessernachkl = false;
  turnRight();
  while ((unter50 && kleiner && groessernachkl) == false) {
    prev = next;
    delay(10);
    next = leftdistance();
    kleiner = prev > next;
    groessernachkl = kleiner && (prev <= next);
    unter50 = next < 500;
  }
  halt();
}

void kurveLsensorR () {
  float prev = rightdistance();
  float next = prev;

  bool unter50 = false;
  bool kleiner = false;
  bool groessernachkl = false;
  turnLeft();
  while ((unter50 && kleiner && groessernachkl) == false) {
    prev = next;
    delay(10);
    next = rightdistance();
    kleiner = prev > next;
    groessernachkl = kleiner && (prev <= next);
    unter50 = next < 500;
  }
  halt();
}

void kurveLsensorL () {
  float prev = leftdistance();
  float next = prev;


  bool kleiner = false;
  bool groessernachkl = false;

  turnLeft();
  while ((kleiner && groessernachkl) == false) {
    prev = next;
    delay(10);
    next = leftdistance();
    kleiner = prev > next;
    groessernachkl = kleiner && (prev <= next);
  }
  halt();
}

void SeiteL() {
  kurveLsensorR();
  if (fertig == true) {
    halt();
    while (true) {

    }
  }
  cm30geradeaus();
  kurveLsensorL();
}
void cm30geradeaus() {
  int merken = frontdistance() - 300;
  fertig = merken <= 300 || fertig;
  forward();
  while (frontdistance > 300 && frontdistance > merken) {

  }
  halt();
}
void SeiteR() {
  kurveRsensorL();
  if (fertig == true) {
    halt();
    while (true) {

    }
  }
  cm30geradeaus();
  kurveRsensorR();
}



void geradeausbis30() {
  forward();
  while (frontdistance() >= 300) {
    Serial.print("Weiter");
  }
  halt();
}

void haupt() {
  geradeausbis30();
  kurveRsensorL();
  geradeausbis30();
  while (true) {
    SeiteR();
    geradeausbis30();
    SeiteL();
    geradeausbis30();
  }
}