Tank until Ultrasonic Range then Continue Program

Hi
This is my first time using coding and have no knowledge prior to writing the below code.

I’m trying to get my robot to tank until something is within a certain range using the ultrasonic sensor HC-SR04 on an UNO board.

I have made the program ignore all the areas that I’m having issues with and the rest of the program works is intended. I have only got this far through supplied samples and google but cannot work out the U/S sensor.

I need to be able to the have the robot tank to the left or right until an object is within a set range. I have included the ranges (in mm) in brackets in the program.

It would be great if the distance ranges could be adjusted in a similar way to the motor speeds as per drive forward.

// Global

// Button pin
const int buttonPin = A0;
// Right motor pins (Out 1 Out 2)
const int dir1PinRight = 0;
const int dir2PinRight = 1;
const int speedPinRight = 5; // PWM pin
// Left motor pins (Out 1 Out 2)
const int dir1PinLeft = 2;
const int dir2PinLeft = 4;
const int speedPinLeft = 6; // PWM pin
// Hall effect sensors
const int hallPinRight = 3;
// Variables for hall effect sensors on wheels using interrupts
volatile int rightCount;
const int bumpStop = 13;
const int ball4release = 10;
const int ball3release = 9;
const int ball2release = 8;
const int ball1release = 7;
// Ultrasonic sensor
//const int triggerPin = 11; // send pulse on this pin
//const int echoPin = 12;    // detect return pulse on this pin
//long distToObject = 0;     // distance between sensor and object

//-----------------------------------

void setup() {
  
  // Define button pin
  pinMode(buttonPin, INPUT_PULLUP);

  // Define hall effect sensor pins
  pinMode(hallPinRight, INPUT);

  // Attach interrupts to the ISR vectors
  attachInterrupt(digitalPinToInterrupt(hallPinRight), hallRight_ISR, RISING);

  // Right motor
  pinMode(dir1PinRight, OUTPUT);
  pinMode(dir2PinRight, OUTPUT);
  pinMode(speedPinRight, OUTPUT);
  //Left motor
  pinMode(dir1PinLeft, OUTPUT);
  pinMode(dir2PinLeft, OUTPUT);
  pinMode(speedPinLeft, OUTPUT);

  // Define hall effect sensor pins
  pinMode(hallPinRight, INPUT);

  // define Bump Stop
  pinMode(bumpStop, INPUT_PULLUP);

  // define ball release
  pinMode(ball4release, OUTPUT);
  pinMode(ball3release, OUTPUT);
  pinMode(ball2release, OUTPUT);
  pinMode(ball1release, OUTPUT);

  // Define ultrasonic sensor pins
  //pinMode(triggerPin, OUTPUT);
  //pinMode(echoPin, INPUT);

  // Serial monitor
  //Serial.begin (9600);

  // end setup()
}
//---------------------------------

void loop() {
  // put your main code here, to run repeatedly:
  while (digitalRead(buttonPin) == HIGH) {
    // Wait for button press
  };
  delay(200);
  while (digitalRead(buttonPin) == LOW) {
    // Wait for button release
  };
  delay(200);

  driveForward(190, 200, 9);
  delay(500);
  //tankRight(500-625);
  delay(500);
  driveToBumpSwitch(190, 200, bumpStop);
  ball4();
  driveBackward(200, 185, 16);
  delay(1000);
  //tankLeft(1000-1060);
  delay(500);
  driveToBumpSwitch(190, 200, bumpStop);
  ball3();
  driveBackward(200, 185, 4);
  delay(500);
  //tankRight(600-680);
  driveToBumpSwitch(190, 200, bumpStop);
  delay(500);
  ball2();
  driveBackward(200, 185, 9);
  //tankLeft(520-600);
  driveToBumpSwitch(180, 200, bumpStop);
  ball1();
  driveBackward(200, 185, 45);
  tankLeftCount(0);
  delay(500);
  driveBackward(200, 185, 12);

} // end void loop()
//--------------------------------

void driveForward(int driveSpeedLeft, int driveSpeedRight, int count) {

  rightCount = 0;

  // Set speed
  analogWrite(speedPinLeft, driveSpeedLeft);
  analogWrite(speedPinRight, driveSpeedRight);

  digitalWrite(dir1PinRight, HIGH);
  digitalWrite(dir2PinRight, LOW);
  digitalWrite(dir1PinLeft, LOW);
  digitalWrite(dir2PinLeft, HIGH);

  while (rightCount <= count) {
    // Wait for hall effect to register required counts
  }

  // Stop both motors
  digitalWrite(dir1PinLeft, LOW);
  digitalWrite(dir2PinLeft, LOW);
  digitalWrite(dir1PinRight, LOW);
  digitalWrite(dir2PinRight, LOW);

} // end driveforward()
//--------------------------------
void driveBackward(int driveSpeedLeft, int driveSpeedRight, int count) {
  rightCount = 0;

  // Set speed
  analogWrite(speedPinLeft, driveSpeedLeft);
  analogWrite(speedPinRight, driveSpeedRight);

  digitalWrite(dir1PinRight, LOW);
  digitalWrite(dir2PinRight, HIGH);
  digitalWrite(dir1PinLeft, HIGH);
  digitalWrite(dir2PinLeft, LOW);

  while (rightCount <= count) {
    // Wait for hall effect to register required counts
  }

  // Stop both motors
  digitalWrite(dir1PinLeft, LOW);
  digitalWrite(dir2PinLeft, LOW);
  digitalWrite(dir1PinRight, LOW);
  digitalWrite(dir2PinRight, LOW);
} // end driveBackward()

//------------------------------
void tankRight(int count) {
  rightCount = 0;

  digitalWrite(dir1PinRight, HIGH);
  digitalWrite(dir2PinRight, LOW);
  digitalWrite(dir1PinLeft, HIGH);
  digitalWrite(dir2PinLeft, LOW);

  while (rightCount <= count) {
    // Wait for hall effect to register required counts
  }

  // Stop both motors
  digitalWrite(dir1PinLeft, LOW);
  digitalWrite(dir2PinLeft, LOW);
  digitalWrite(dir1PinRight, LOW);
  digitalWrite(dir2PinRight, LOW);
} // end tankRight()

//-----------------------------
void tankLeft(int count) {
  rightCount = 0;

  digitalWrite(dir1PinRight, LOW);
  digitalWrite(dir2PinRight, HIGH);
  digitalWrite(dir1PinLeft, LOW);
  digitalWrite(dir2PinLeft, HIGH);

  while (rightCount <= count) {
    // Wait for hall effect to register required counts
  }

  // Stop both motors
  digitalWrite(dir1PinLeft, LOW);
  digitalWrite(dir2PinLeft, LOW);
  digitalWrite(dir1PinRight, LOW);
  digitalWrite(dir2PinRight, LOW);
} // end tankLeft()


//---------------------------
void driveToBumpSwitch(int driveSpeedLeft, int driveSpeedRight, int bumpStop) {
  // Note: Assumes bumpSwitchPin is initially HIGH

  // Both motors forward
  digitalWrite(dir1PinLeft, LOW);
  digitalWrite(dir2PinLeft, HIGH);
  digitalWrite(dir1PinRight, HIGH);
  digitalWrite(dir2PinRight, LOW);

  while (digitalRead(bumpStop) == HIGH) {

    delay(50);
  };

  delay(200);

  // Stop both motors
  digitalWrite(dir1PinLeft, LOW);
  digitalWrite(dir2PinLeft, LOW);
  digitalWrite(dir1PinRight, LOW);
  digitalWrite(dir2PinRight, LOW);
} // end driveToBumpStop()

//---------------------------
void tankLeftCount(int count) {
  rightCount = 0;

  digitalWrite(dir1PinRight, LOW);
  digitalWrite(dir2PinRight, HIGH);
  digitalWrite(dir1PinLeft, LOW);
  digitalWrite(dir2PinLeft, LOW);

  while (rightCount <= count) {
    // Wait for hall effect to register required counts
  }

  // Stop both motors
  digitalWrite(dir1PinLeft, LOW);
  digitalWrite(dir2PinLeft, LOW);
  digitalWrite(dir1PinRight, LOW);
  digitalWrite(dir2PinRight, LOW);
} // end tankLeftCount()

//-----------------------


void hallRight_ISR() {
  // Right wheel sensor has triggered
  rightCount++;
}
//----------------------

void ball4() {
  digitalWrite(ball4release, HIGH);
  delay(5000);
  digitalWrite(ball4release, LOW);
}
//----------------------

void ball3() {
  digitalWrite(ball3release, HIGH);
  delay(5000);
  digitalWrite(ball3release, LOW);
}
//----------------------

void ball2() {
  digitalWrite(ball2release, HIGH);
  delay(3000);
  digitalWrite(ball2release, LOW);
}
//----------------------

void ball1() {
  digitalWrite(ball1release, HIGH);
  delay(2000);
  digitalWrite(ball1release, LOW);
}
//------------------------

Many Thanks

I'm trying to get my robot to tank until something is within a certain range using the ultrasonic sensor HC-SR04 on an UNO board.

What do you mean by tank? Is it possible that you're German speaking and translated "tanken" incorrectly? In English "to tank" means to fall or to sink.
So are you filling some liquid into your robot? Are you measuring the liquid level with the ultrasonic sensor?

I need to be able to the have the robot tank to the left or right until an object is within a set range. I have included the ranges (in mm) in brackets in the program.

Can you describe in other words what you're trying to achieve? I think you used the work tank again in a wrong way. I'm not a native speaker but I don't understand that sentence.

Post a wiring diagram of your setup so we know what hardware is connected and how.

Do you know the Ping library?

Hi
By tank I mean rotate on the spot (drive left side forward and right side backwards) until the ultrasonic detects an object at a distance range then go to the next line of the code.

Thanks

I have no clue what your problem is. You might have to start over and describe your project in more details, providing links to the used hardware as well as a wiring diagram and a description of what you're trying to achieve.

I think by tank he means rotate on the spot until a target within range is detected.

The problem as I see it is if the rotation speed is too fast you will miss any object, and if it is too slow then it will take too long to scan the 360o

The big problem with your code is that it is too big.
Write a smaller sketch that illustrates just the problem you are trying to solve. Rotate the tank looking for an echo return and stop and blink an LED when you see an object.

Unless you take measures to change it the default time before pulseIn function takes a second before it gives up looking and you can fire the ultrasonic beam again.

But you code never attempts to make any measurements, even the pin definitions are commented out.

See attached. Compiles but is untested.

EDIT: I realized after adding this that the do/while in the “SitandSpin” function has the incorrect while condition. Please replace with:

    do
    {
        // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
        digitalWrite(trigPin, HIGH);
        delayMicroseconds(10);
        digitalWrite(trigPin, LOW);  
        uint16_t duration = pulseIn(echoPin, HIGH, 7300ul );     //timeout of 7300uS for <2.5mm total travel time (1.25m distance)
        fDistMM = 10.0 * (float)duration * 0.034 / 2.0;     //10x because measurements are in mm       
            
    }while( fDistMM < (float)minRange || fDistMM > (float)maxRange );

tank_sitandspin.ino (9.1 KB)

Thanks Blackfin,
Worked a treat. Just what I was looking for. :smiley: ;D
For you information to make it work correctly I had to take out the stop and return lines as this caused my bot to not drive forward in the next step of the program. I also had issues with the timeout being to short and the ultrasonic not working for to detect over 700mm so I changed it 10000ul.

suter83:
Thanks Blackfin,
Worked a treat. Just what I was looking for. :smiley: ;D
For you information to make it work correctly I had to take out the stop and return lines as this caused my bot to not drive forward in the next step of the program. I also had issues with the timeout being to short and the ultrasonic not working for to detect over 700mm so I changed it 10000ul.

I expected you'd need to fine tune for your application. Glad it was helpful.