Cant get my code to exit a loop.

Hey i have been trying this code to stop my Redbot after it travels forward for a second but the problem is that i have tried break; and tried putting in a variable to stop it but it keeps on repeating the same function over and over again untill i press the reset button. Any help is much appreciated..... :slight_smile:

// setting motor pins as variables
// Motor 1
int motorafb = 2; // Motor A - forward/backwards
int motorab = 4; // Motor A - brake
int motorahs = 5; // Motor A - Speed
int motorbfb = 7; // Motor B - forward/backwards
int motorbb = 8; // Motor B - brake
int motorbhs = 6; // Motor B - Speed
int state = 0;
int state2 = 0;
 
void setup() {
 
//Setup Motor A
pinMode(motorafb, OUTPUT); //Initiates Motor A pin
pinMode(motorab, OUTPUT); //Initiates Brake A pin
 
//Setup Motor B
pinMode(motorbfb, OUTPUT); //Initiates Motor B pin
pinMode(motorbb, OUTPUT); //Initiates Brake B pin
Serial.begin(9600);
}
 
// two functions which turn one motor on, the other off for 'spd' speed, and 'tme' Time
 
void TurnRight(int spd, int tme){
digitalWrite(motorafb, LOW);
digitalWrite(motorab, LOW);
analogWrite(motorahs, spd);
 
digitalWrite(motorbfb, 'HIGH');
digitalWrite(motorbb, LOW);
analogWrite(motorbhs, spd);
delay(tme);
}
 
void TurnLeft(int spd, int tme){
digitalWrite(motorafb, HIGH);
digitalWrite(motorab, LOW);
analogWrite(motorahs, spd);
 
digitalWrite(motorbfb, LOW);
digitalWrite(motorbb, LOW);
analogWrite(motorbhs, spd);
delay(tme);
}
 
// Function to go power both motors for 'spd' speed, and 'tme' time
void GoForward(int spd, int tme){
digitalWrite(motorafb, HIGH);
digitalWrite(motorab, LOW);
analogWrite(motorahs, spd);
 
digitalWrite(motorbfb, HIGH);
digitalWrite(motorbb, LOW);
analogWrite(motorbhs, spd);
delay(tme);
}
 
// Function to stop for a set amount of time - tme
void Stop(int tme){
digitalWrite(motorab, HIGH);
digitalWrite(motorbb, HIGH);
analogWrite(motorahs, 0);
analogWrite(motorbhs, 0);
delay(tme);
}
 
void loop(){
// Now we can use them like this
 if(Serial.available() > 0){
   
   if(Serial.read() == 'D'){
   state =1 - state;
   }
   
   if(state == 1){
GoForward(255, 500); // So this will go forward full speed (255) fpr 500ms
Stop(1000); // Sits Stopped for 1000ms
 
   }
   state = 0;
 }
 
 {
  if (Serial.read() == 'F'){  
 state2 =1 -state2;
}

if(state2 == 1){
TurnRight(255, 1000);// turns Right at full speed for 1000ms
Stop(1000);// Sits Stopped for 1000ms

}
state2 = 0 ;
 }
}

I tried
reading your code.

But it jumps
all over
the place,
and I

got dizzy.

Use Tools + Auto Format to fix the indentation of your code, and post it again.

tools : autoformat

makes code easier to read.

darn.... beat me by 12 seconds

Gogo_pan:
Hey i have been trying this code to stop my Redbot after it travels forward for a second but the problem is that i have tried break; and tried putting in a variable to stop it but it keeps on repeating the same function over and over again untill i press the reset button. Any help is much appreciated..... :slight_smile:

When you stop the motors:

{
  digitalWrite(motorab, HIGH);
  digitalWrite(motorbb, HIGH);
  analogWrite(motorahs, 0);
  analogWrite(motorbhs, 0);
  delay(tme);
}

Unless you need the one second pause, why do you need the delay? Unless the loop() function picks up another input, it will stay stopped for a very long time, no?

and here:

    if(Serial.read() == 'D')
    {
      state = 1 - state;
    }

    if(state == 1)
    {
      GoForward(255, 500); // So this will go forward full speed (255) fpr 500ms
      Stop(1000); // Sits Stopped for 1000ms
    }
    state = 0;
  }

Why are you bothering with the state? If you receive a 'D' then just GoForward() no?

  if(Serial.read() == 'D')
  {
    GoForward(255, 500); // So this will go forward full speed (255) fpr 500ms
    Stop(1000); // Sits Stopped for 1000ms
  }

Serial.read in this expression:

if Serial.read() = 'D'

will consume that input.

Consider something like this:

void loop()
{
  // Now we can use them like this
  if(Serial.available() > 0)
  {
    char newInput = Serial.read();
    if (newInput == 'D')
    {
      Serial.println("Got myself a D, and I'm Charging Forward!!!");
      GoForward(255, 500); // So this will go forward full speed (255) fpr 500ms
      Stop(1000); // Sits Stopped for 1000ms
    }
    else if (newInput == 'F')
    {
      TurnRight(255, 1000);// turns Right at full speed for 1000ms
      Stop(1000);// Sits Stopped for 1000ms
    }
   else
   {
   }
  }
}

Finally, don't be afraid to add some debug Serial.print() to see what's happening...

// setting motor pins as variables
// Motor 1
int motorafb = 2; // Motor A - forward/backwards
int motorab = 4; // Motor A - brake
int motorahs = 5; // Motor A - Speed
int motorbfb = 7; // Motor B - forward/backwards
int motorbb = 8; // Motor B - brake
int motorbhs = 6; // Motor B - Speed
//int state = 0;
//int state2 = 0;

void setup() 
{
  //Setup Motor A
  pinMode(motorafb, OUTPUT); //Initiates Motor A pin
  pinMode(motorab, OUTPUT); //Initiates Brake A pin
  //Setup Motor B
  pinMode(motorbfb, OUTPUT); //Initiates Motor B pin
  pinMode(motorbb, OUTPUT); //Initiates Brake B pin
  Serial.begin(9600);
}

// two functions which turn one motor on, the other off for 'spd' speed, and 'tme' Time

void TurnRight(int spd, int tme)
{
  digitalWrite(motorafb, LOW);
  digitalWrite(motorab, LOW);
  analogWrite(motorahs, spd);

  digitalWrite(motorbfb, 'HIGH');
  digitalWrite(motorbb, LOW);
  analogWrite(motorbhs, spd);
  delay(tme);
}

void TurnLeft(int spd, int tme)
{
  digitalWrite(motorafb, HIGH);
  digitalWrite(motorab, LOW);
  analogWrite(motorahs, spd);

  digitalWrite(motorbfb, LOW);
  digitalWrite(motorbb, LOW);
  analogWrite(motorbhs, spd);
  delay(tme);
}

// Function to go power both motors for 'spd' speed, and 'tme' time
void GoForward(int spd, int tme)
{
  digitalWrite(motorafb, HIGH);
  digitalWrite(motorab, LOW);
  analogWrite(motorahs, spd);

  digitalWrite(motorbfb, HIGH);
  digitalWrite(motorbb, LOW);
  analogWrite(motorbhs, spd);
  delay(tme);
}

// Function to stop for a set amount of time - tme
void Stop(int tme)
{
  digitalWrite(motorab, HIGH);
  digitalWrite(motorbb, HIGH);
  analogWrite(motorahs, 0);
  analogWrite(motorbhs, 0);
  delay(tme);
}
void loop()
{
  // Now we can use them like this
  if(Serial.available() > 0)
  {
    char newInput = Serial.read();
    if (newInput == 'D')
    {
      Serial.println("Got myself a D, and I'm Charging Forward!!!");
      GoForward(255, 500); // So this will go forward full speed (255) fpr 500ms
      Stop(1000); // Sits Stopped for 1000ms
    }
    else if (newInput == 'F')
    {
      Serial.println("Got myself an F, and I'm Turning Right!!!");
      TurnRight(255, 1000);// turns Right at full speed for 1000ms
      Stop(1000);// Sits Stopped for 1000ms
    }
   else if (newInput == 'S')
   {
     Serial.println("Got myself an S, and I'm Turning Left!!!");
     TurnLeft(255, 1000);// turns Right at full speed for 1000ms
     Stop(1000);// Sits Stopped for 1000ms
   }
   else
   {
     
   }
  }
}

Thank You soo much for the reply... Well Im sorry for being a noob but i cant find the auto format key :stuck_out_tongue: i forgot to mention this but im using a Xbee module so the leds were working fine but i modified that code for the RedBot Mainboard now when i press the Push Button it sends the Word D and F wireless to the Receiving Redbot Xbee and then The Loop forward just keeps getting executed and wont stop. I want the Redbot to stop once i stop pressing the Push Button but with my code the Redbot keeps travelling forward. Can i please alter the code in such a way that it goes forward when i press the push button and stops right when i release the push button. Bulldog Lowell i tried your code but the problem is still there I will post the sending bit code too just so you know whats happening...

#define Version "1.00a0"

int Button0 = 2;
int Button1 = 3;

void setup() {
  pinMode(Button0 , INPUT);
  pinMode(Button1, INPUT);
  Serial.begin(9600);
}

void loop(){
  
  if(digitalRead(Button0) == HIGH) {
    Serial.print('D');
    delay(10);
  }
  if(digitalRead(Button1) == HIGH){
    Serial.print('F');
    delay(10);
  }
}

Well Im sorry for being a noob but i cant find the auto format key

There is a menu at the top of the window. One of the menu items is Tools. On that menu is Auto Format. Find it. Use it.

:stuck_out_tongue_closed_eyes: Thank You soo much..... I got it.... Can You look at the modified code Bulldoglowell posted for me??? How can stop the loop??/

How can stop the loop??

You need to be clear(er) on what the problem is. If the problem is that loop() gets called over and over, there is no reasonable way to stop that. It is doing exactly what it is supposed to do.

#define Version ''1.00a0''

This is nonsense. Two single quotes are not the same as a double quote.

int Button = 2;
int Button1 = 3;

Do you count like this? Nothing, one,... Or, do you count one, two,... If you can't come up with meaningful names, and have to resort to numbering, number ALL the names in the set.

The sender code is crap:

void loop(){
  
  if(digitalRead(Button) == HIGH) {
    Serial.print('D');
    delay(10);
  }
  if(digitalRead(Button1) == HIGH){
    Serial.print('F');
    delay(10);
  }
}

Look at the state change detection example. You should be sending a letter when the switch BECOMES pressed, not every 10 milliseconds when the switch IS pressed.

Actually I'm studying electronics so everything starts from 0 that's why have a habit of not writing zero and starting off at 1.. Well the sending bit code i got from a book for xbee. As i mentioned earlier I'm not that good of a programmer.. In the book it says The Delay is used to prevent overwhelming the Serial port. So could you please tell me what can I change in the code so that when I press the push button the redbot moves and stops when the push button is released i.e. just as any other remote controlled car functions. I hope I was able to be more clear this time. Thank you guys. :slight_smile:

Actually im studying electronics so everything starts from 0 thats why have a habit of not writing zero and starting off at 1..

Describe something in electronics, not programming, that starts from 0 rather than 1.

Button0 and Button1 would be better than Button and Button1.

So please can you tell me what can i change in the code.

I told you where to look. Part of being a programmer is finding code that does something like what you want, and learning to adapt it.

What you want to do is send one value when the switch BECOMES pressed, and send another value when the switch BECOMES released. This way, the delay()s are not needed (except for debouncing) and the sender will send just one value for each state change.

Thank You Paul I totally agree....

So i have edited the code and this is how it turned out to be. Everyone please feel free to point out the errors as its still doing infinite loops of going forward , when i press the push button once instead of moving forward only until i keep the push button pressed and stop when the push button is released. AND now the second push button is not working. What i have done is that i have introduced 2 new variables so that if the push buttons are not pressed it could perform the functions that are not needed. PLEASE Help.....

#define Version "1.00a0"

int Button0= 2;
int Button1 = 3;
int Dummy1  = 4;
int Dummy2  = 5;

void setup() {
  pinMode(Button0 , INPUT);
  pinMode(Button1, INPUT);
  pinMode(Dummy1, OUTPUT);
  pinMode(Dummy2, OUTPUT);
  Serial.begin(9600);
}

void loop(){

  if(digitalRead(Button0) == HIGH) {

    Serial.print('D');
    //delay(10);
  }
  else if (digitalRead(Button0) == LOW){
    digitalWrite(Dummy1 ,LOW);

  }
  else if(digitalRead(Button1) == HIGH){

    Serial.print('F');
    //delay(10);
  }
  else if (digitalRead(Button1) == LOW){
    digitalWrite(Dummy2, LOW) ;
  }
  else 
  {
  }
}
  if(digitalRead(Button0) == HIGH) {

    Serial.print('D');
    //delay(10);
  }
  else if (digitalRead(Button0) == LOW){
    digitalWrite(Dummy1 ,LOW);

  }

This is still sending one value when the switch IS high, not when the switch BECOMES high.

It is sending nothing when the switch becomes/is low.

  else if(digitalRead(Button1) == HIGH){

Why is this an else if statement? The action based on switch two should not be dependent on the state of switch one. In fact, this statement will never even be evaluated, since one of the previous statements must be true (a switch can be either HIGH or LOW, nothing else).

PaulS:
Why is this an else if statement? The action based on switch two should not be dependent on the state of switch one. In fact, this statement will never even be evaluated, since one of the previous statements must be true (a switch can be either HIGH or LOW, nothing else).

perhaps take a step back and start with this and watch the behaviour of your serial monitor as you press the two buttons. Take a look to even notice what happens when you press both.

int Button0= 2;
int Button1 = 3;
int Dummy1  = 4;
int Dummy2  = 5;

void setup() 
{
  pinMode(Button0 , INPUT);
  pinMode(Button1, INPUT);
  pinMode(Dummy1, OUTPUT);
  pinMode(Dummy2, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  if (digitalRead(Button0) == HIGH) 
  {
    Serial.print("D");
  }
  else if(digitalRead(Button1) == HIGH)
  {
    Serial.print("F");
  }
}