Want to run my code once. while(true) {} - this doesn't work - Please can anyone help me understand why?

int PWMA = 5; //PIN Motor A = Speed
int PWMB = 6; //PIN Motor B = Speed
int BIN_1 = 7; //PIN Motor B = Spin
int AIN_1 = 8; //PIN Motor A = Spin
int STBY = 3; //Motor Standby
int Speed = 100;

void setup(){
// put your setup code here, to run once:
pinMode(PWMA, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(AIN_1, OUTPUT);
pinMode(BIN_1, OUTPUT);
pinMode(STBY, OUTPUT);
digitalWrite(STBY, HIGH);
}

void loop(){

forward();
backward();
while (true){};

}
//while (true){} //goes backwards for ever
//while(true){digitalWrite(STBY, LOW);} //Only way to get the car to stop
// seems if there isn't an argument within in the curly brackets it just fills it with what ran before it

void forward(){
analogWrite(PWMA, Speed);
digitalWrite(PWMB, Speed);
digitalWrite(AIN_1, HIGH);
digitalWrite(BIN_1, HIGH);
delay(1000);
}

void backward(){
analogWrite(PWMA, Speed);
analogWrite(PWMB, Speed);
digitalWrite(AIN_1, LOW);
digitalWrite(BIN_1, LOW);
delay(1000);
}

void left(){
analogWrite(PWMA, Speed);
analogWrite(PWMB, Speed);
digitalWrite(AIN_1, LOW);
digitalWrite(BIN_1, HIGH);
delay(250);
}

void right(){
analogWrite(PWMA, Speed);
analogWrite(PWMB, Speed);
digitalWrite(AIN_1, HIGH);
digitalWrite(BIN_1, LOW);
delay(250);
}
void stopCar(){
analogWrite(PWMA, 0);
analogWrite(PWMB, 0);
digitalWrite(AIN_1, LOW);
digitalWrite(BIN_1, LOW);
}

I am very new to programming. I only got this Elegoo robot car version 4, so I can practice my coding. I wanted to know if anyone can help me with this problem I am having.
I want to know why the while loop replaces its empty curly brackets with the code on top if no condition was written inside it. From what I have read about the while loop should not work like that, it should run what's in the curly brackets. So if it has nothing it shouldn't constantly run nothing.

Not possible. That is an infinite loop that is doing nothing. I suppose you commanded the motors to go backwards and never stopped them.

I actually just wanted the code to be stuck in the infinite loop, so it only runs once. For example in this case. Go forward for 1 second then backward 1 second and stop. Instead it just goes backwards forever. What am I doing wrong here?

I don't know. I could guess but I assure you it is not the while/do loop. I suspect if you remove the backward() call it will go forward forever. Can you write 0 to PWMA and PWMB to stop the motor? What does STBY signal do?

Hello
Your sketch haven´t a function to stopp.

Yeah exactly, if I take out backwards, it'll just run forward forever haha...so weird. Yeah, I can just write a function to kill the PWMA, but as it is in the void loop function, it'll just start again going backwards and forwards. STBY is the standby signal, the only way I have actually managed to stop it is using this code: while(true){digitalWrite(STBY, LOW);}

I am actually more curious than anything of why the, while (true) { //empty code}, runs the code on top of it? Instead of what is actually in the curly brackets which is nothing.
But when I run a code like this, while(true){digitalWrite(STBY, LOW);}, it runs what's inside the curly bracket not what's on top. Feel like I am going mad haha.

No it hasn't yet. Currently this is what I have used to stop the code from running in the void loop: while(true){digitalWrite(STBY, LOW);}

Hello
you have to insert a stop function behind the call of backward(); to stopp.

I have tried this but then it starts from the beginning again as its all within the void loop().

Post the code for that attempt.

I see nothing weird here at all. The behaviour you described matches your code exactly.

This is what your code says should be done:

  1. Set the motors to run forward
  2. Do nothing for 1 second
  3. Set the motors to run backward
  4. Do nothing for 1 second
  5. Do nothing forever

And that is exactly what you describe happening.

Telling the Arduino to "do nothing" does not stop the motors. Stopping the motors is an action. Doing nothing is not an action.

If you want it to stop, you have to tell it to stop. But you are telling it to do nothing, to take no action at all. So nothing changes, everything continues as before.

By the way, this looks like an error:

digitalWrite(PWMA, Speed);

The digitalWrite() function expects only the values HIGH or LOW. It does not expect a value like 100. I suspect it is treating 100 to mean the same as HIGH. I think you meant to use analogWrite().

1 Like

I love it. I’ve wanted to have a way to run things backwards.

Maybe it’s the secret behind “undo”, that late 20th century innovation.

a7

1 Like

Which motor driver? See what this does:

int PWMA = 5; //PIN Motor A = Speed
int PWMB = 6; //PIN Motor B = Speed
int BIN_1 = 7; //PIN Motor B = Spin
int AIN_1 = 8; //PIN Motor A = Spin
int STBY = 3; //Motor Standby
int Speed = 100;

void setup() {
  // put your setup code here, to run once:
  pinMode(PWMA, OUTPUT);
  pinMode(PWMB, OUTPUT);
  pinMode(AIN_1, OUTPUT);
  pinMode(BIN_1, OUTPUT);
  pinMode(STBY, OUTPUT);
  digitalWrite(STBY, HIGH);
}

void loop() {

  forward();
  backward();
  stop();
  while (true);
}
void forward() {
  analogWrite(PWMA, Speed);
  analogWrite(PWMB, Speed);
  digitalWrite(AIN_1, HIGH);
  digitalWrite(BIN_1, HIGH);
  delay(1000);
}
void backward() {
  analogWrite(PWMA, Speed);
  analogWrite(PWMB, Speed);
  digitalWrite(AIN_1, LOW);
  digitalWrite(BIN_1, LOW);
  delay(1000);
}
void stop () {
  analogWrite(PWMA, 0);
  analogWrite(PWMB, 0);
  digitalWrite(AIN_1, LOW);
  digitalWrite(BIN_1, LOW);
}
1 Like

See edited version above.

The forward and backwards routines only have a 1 second delay. Is that enough time for the motor to respond?

I have tried using a while or for function but all it does is run the code on top of it instead of just running nothing. Hope that makes sense?

From what I see, the motor is going to run backwards forever.

Instead of using the do - while you could just put your code in the setup section.

1 Like

forward();
backward();
stop();
while (true);

This helped thank you. May I ask why the while loop reads what's above it? So through your suggestion, it runs stop function continuously.

Shouldn't the following work as well? As you are saying while true play whatever is in the curly bracket? (which is nothing). Why is the empty curly bracket ignored and filled in with the code above it instead?
The following code makes it go backwards forever.
forward();
backward();
while (true) {};

Thank you for spotting my error I have changed it now. I knew something was wrong with the speed.

if you look what this guy does in this videio. Please jump to 20:45. He seems to run it once successfully. why does the while empty loop work for him? It doesn't replace the empty code with whats on top and run backwards forever.

Look you are making a false assumption, it does not run the code above it. It simply stops, as the code above it was never stopped then it keeps on running the motor after the delay. If after the delay in the backwards function you stopped the motor the the origional code would work like you thought it should.

That is not what is happening

Once you have told the motors to run backward that is what they will do until you tell them to do something different. So, your code says "run forwards, run backwards then go round in an infinite loop with nothing changing, so the motors continue to run backwards

The effect is to leave the motors running backwards

1 Like

Thank you all for your help. It all makes sense now, the problem was in my understanding, not the code itself. As @Grumpy_Mike and @UKHeliBob pointed out my understanding of how the while loop worked was flawed, I understand what I was doing wrong now and fixed the code, by adding a stop function suggested by JCA34F (can't tag more than 2 users sorry) in the backward function. Thank you all for your help. Learning something every day. This matter is now closed.