How can I repeat a void function?

Hello. I have a void (move forward). I want to repeat it in void loop but it is executed just one time. How can I repeat it in void loop?

//ARDUINO OBSTACLE AVOIDING CAR//
// Before uploading the code you have to install the necessary library//
//AFMotor Library https://learn.adafruit.com/adafruit-motor-shield/library-install //
//NewPing Library https://github.com/livetronic/Arduino-NewPing// 
#include <NewPing.h>
#include <SoftwareSerial.h>
#include <Servo.h>
#include "HCPCA9685.h"

#define TRIG_PIN D3 
#define ECHO_PIN D4
#define MAX_DISTANCE 200 

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);   

boolean goesForward=false;
int distance = 100;

#define  I2CAdd_1 0x40
#define  I2CAdd_2 0x41

int pos1 = 200;
int pos2 = 220;
int pos3 = 180;
int pos4 = 0;
int pos5 = 130;
int pos6 = 100;
int pos7=50;
int pos8=260;
int pos9=140;
const float delay1 = 0.5;
int delay2 = 500;

/* Create an instance of the library */
HCPCA9685 HCPCA9685_1(I2CAdd_1);
HCPCA9685 HCPCA9685_2(I2CAdd_2);

void setup() {  
     // Initialise both modules
  HCPCA9685_1.Init(SERVO_MODE);
  HCPCA9685_2.Init(SERVO_MODE);
  // Wake both devices up
  HCPCA9685_1.Sleep(false);
  HCPCA9685_2.Sleep(false);
  unsigned int Pos;
}

void loop() {
 moveForward(); 
}

void moveStop() {

  } 
  
void moveForward() {

 if(!goesForward)
  {
    goesForward=true;
   for (int Pos = pos5 ; Pos >= pos7 ; Pos--) {
    HCPCA9685_1.Servo(1, Pos);
    HCPCA9685_1.Servo(7, Pos);
    HCPCA9685_1.Servo(13, Pos);
    delay(delay1);
  }
  }
}

void moveBackward() {
    goesForward=false;
  for (int Pos = pos7 ; Pos <= pos5 ; Pos++) {
    HCPCA9685_1.Servo(4, Pos);
    HCPCA9685_1.Servo(10, Pos);
    HCPCA9685_2.Servo(0, Pos);
    delay(delay1);
  }
}  

void turnRight() {
   for (int Pos = pos7 ; Pos <= pos5 ; Pos++) {
    HCPCA9685_1.Servo(4, Pos);
    HCPCA9685_1.Servo(10, Pos);
    HCPCA9685_2.Servo(0, Pos);
    delay(delay1);
  }    
} 
 
void turnLeft() {
  for (int Pos = pos5 ; Pos >= pos7 ; Pos--) {
    HCPCA9685_1.Servo(1, Pos);
    HCPCA9685_1.Servo(7, Pos);
    HCPCA9685_1.Servo(13, Pos);
    delay(delay1);
  }
}  

void loop() {} does exactly as it says, the code inside the {} repeats forever, so you are done.

The problem seems to be here. On the first pass, goesForward is assigned the value true. Since moveBackward is not getting executed yet and the value of goesForward does not ever get re-assigned to false, the if statement never gets called on subsequent passes so the loop just goes around ad infinitum. I am, of course, assuming that there are no additional statements in void loop().

1 Like

No I want to repeat another viod 'void moveForward ()' in void loop. But I can't.

I am not sure that I follow, but this would run it twice in succession and run through the if statement:

void loop() {
 moveForward();
 goesForward=false;
 moveForward(); 
}

Either that or have a counter and change goesForward to true only when the number of counts has been reached. Not quite sure why one would want to do that though? What are you trying to achieve exactly?

1 Like

The problem not in void loop(), but in the

itself.
Read the message #3

1 Like

@alija22
Do I understand right, that it is not your code? Do you copypasted it elsewhere and don't understand it?

Thank you. This is exactly what I need. My code is very busy. I want to simplify it.

Yes I copied it but I understand it

it is how your 'void moveForward ()' function written. It can't be run more than once if you not reset the goesForward flag.

If you need to repeat the moveForward () - change the logic of the code.

1 Like

it's seems not a case...

Difficult to advise really without having the full picture. What I provided is only an example. I have no way of knowing whether it actually fits your particular scenario or simplifies it...

I solved the problem with this solution:

void loop() {
 moveForward();
 goesForward=false;
 moveForward(); 
}

Be aware that post #5 is not a solution, it is a workaround only.
You need to understand "your" code and change the logic.

I tested it and it's ok

Of course your right my friend.

If you use this

try to think, why do you need a goesForward at all?
Remove it from the function and you would not need the workaround

To the future - do not copy the code, if you don't understand it.

@BitSeeker
it works somehow, but it was bad advice. The author understands the code very poorly and therefore does not see that he did not solve the problem.

And yes, if you wanted to set the goesForward flag every time before function call - it was enough to do it once, the second call is completely unnecessary here:

void loop() {
 goesForward=false;
 moveForward(); 
}
1 Like

Agreed. b707, your approach is neater and my reply was only meant to illustrate that the goesForward flag needed to be cleared somewhere, not propose a final solution. But point taken.

1 Like

Thanks. Good idea