How to stop loop?

Hey there Arduino forum!

I have a problem with stopping a loop in my code.
My question is how can i stop the loop of my LED roulette.

I am using a HC-05 bluetooth module.
So when i push the input value 2 on the android app.
The roulette starts.

But when i pushed it again it doesnt stop the sequence.
Somebody said that i needed to use serial intertupts.

But i can’t understand how to.
Can you guys help me out and create or give a tip how to stop the LED roulette after i give it a input by the android app.

If you scroll down all the way to the end. There is my code of the roulette.
While(1) makes it loop.

#define START_CMD_CHAR '*'
#define END_CMD_CHAR '#'
#define DIV_CMD_CHAR '|'
#define CMD_DIGITALWRITE 10
#define CMD_ANALOGWRITE 11
#define CMD_TEXT 12
#define CMD_READ_ARDUDROID 13
#define MAX_COMMAND 20  // max command number code. used for error checking.
#define MIN_COMMAND 10  // minimum command number code. used for error checking. 
#define IN_STRING_LENGHT 40
#define MAX_ANALOGWRITE 255
#define PIN_HIGH 3
#define PIN_LOW 2

String inText;

void setup() {
  Serial.begin(9600);
  Serial.println("ArduDroid 0.12 Alpha by TechBitar (2013)");
  Serial.flush();
  
    for (int var = 3; var <= 8; var++){
    pinMode(var, OUTPUT);
  }
  
}

void loop()
{
  Serial.flush();
  int ard_command = 0;
  int pin_num = 0;
  int pin_value = 0;

  char get_char = ' ';  //read serial

  // wait for incoming data
  if (Serial.available() < 1) return; // if serial empty, return to loop().

  // parse incoming command start flag 
  get_char = Serial.read();
  if (get_char != START_CMD_CHAR) return; // if no command start flag, return to loop().

  // parse incoming command type
  ard_command = Serial.parseInt(); // read the command
  
  // parse incoming pin# and value  
  pin_num = Serial.parseInt(); // read the pin
  pin_value = Serial.parseInt();  // read the value

  // 1) GET TEXT COMMAND FROM ARDUDROID
  if (ard_command == CMD_TEXT){   
    inText =""; //clears variable for new input   
    while (Serial.available())  {
      char c = Serial.read();  //gets one byte from serial buffer
      delay(5);
      if (c == END_CMD_CHAR) { // if we the complete string has been read
        // add your code here
        break;
      }              
      else {
        if (c !=  DIV_CMD_CHAR) {
          inText += c; 
          delay(5);
        }
      }
    }
  }

  // 2) GET digitalWrite DATA FROM ARDUDROID
  if (ard_command == CMD_DIGITALWRITE){  
    if (pin_value == PIN_LOW) pin_value = LOW;
    else if (pin_value == PIN_HIGH) pin_value = HIGH;
    else return; // error in pin value. return. 
    set_digitalwrite( pin_num,  pin_value);  // Uncomment this function if you wish to use 
    return;  // return from start of loop()
  }

  // 3) GET analogWrite DATA FROM ARDUDROID
  if (ard_command == CMD_ANALOGWRITE) {  
    analogWrite(  pin_num, pin_value ); 
    // add your code here
    return;  // Done. return to loop();
  }

  // 4) SEND DATA TO ARDUDROID
  if (ard_command == CMD_READ_ARDUDROID) { 
    // char send_to_android[] = "Place your text here." ;
    // Serial.println(send_to_android);   // Example: Sending text
    Serial.print(" Analog 0 = "); 
    Serial.println(analogRead(A0));  // Example: Read and send Analog pin value to Arduino
    return;  // Done. return to loop();
  }
}

// 2a) select the requested pin# for DigitalWrite action
void set_digitalwrite(int pin_num, int pin_value)
{
  switch (pin_num) {
  case 13:
    pinMode(13, OUTPUT);
    digitalWrite(13, pin_value);  
    // add your code here      
    break;
  case 12:
    pinMode(12, OUTPUT);
    digitalWrite(12, pin_value);   
    // add your code here       
    break;
  case 11:
    pinMode(11, OUTPUT);
    digitalWrite(11, pin_value);         
    // add your code here 
    break;
  case 10:
    pinMode(10, OUTPUT);
    digitalWrite(10, pin_value);         
    // add your code here 
    break;
  case 9:
    pinMode(9, OUTPUT);
    digitalWrite(9, pin_value);         
    // add your code here 
    break;
  case 8:
    pinMode(8, OUTPUT);
    digitalWrite(8, pin_value);         
    // add your code here 
    break;
  case 7:
    pinMode(7, OUTPUT);
    digitalWrite(7, pin_value);         
    // add your code here 
    break;
  case 6:
    pinMode(6, OUTPUT);
    digitalWrite(6, pin_value);         
    // add your code here 
    break;
  case 5:
    pinMode(5, OUTPUT);
    digitalWrite(5, pin_value); 
    // add your code here       
    break;
  case 4:
    pinMode(4, OUTPUT);
    digitalWrite(4, pin_value);         
    // add your code here 
    break;
  case 3:
    pinMode(3, OUTPUT);
    digitalWrite(3, pin_value);   
  break;
   case 2:
    pinMode(2, OUTPUT);
    digitalWrite(2, pin_value);  
    
   while(1){
    digitalWrite(3, pin_value);
    delay(500);
    digitalWrite(3, LOW);
    digitalWrite(4, pin_value);
    delay(500);
    digitalWrite(4, LOW);
    digitalWrite(5, pin_value);
    delay(500);
    digitalWrite(5, LOW);
    digitalWrite(6, pin_value);
    delay(500);
    digitalWrite(6, LOW);
    digitalWrite(7, pin_value);
    delay(500);
    digitalWrite(7, LOW);
    digitalWrite(8, pin_value);
    delay(500);
    digitalWrite(8, LOW);
   }
    
   
    
    break;      
  }  
  
}
  Serial.flush();

Block until all pending outgoing serial data has been sent. Just why is THAT necessary?

  char get_char = ' ';  //read serial

No, it doesn’t.

While(1) makes it loop.

There is nothing to make it stop. What is supposed to make it stop?

Given your statement:

  if (get_char != START_CMD_CHAR) return; // if no command start flag, return to loop().

Where do you think this "returns" to? Return means to send program control out of this function to whatever code called loop() in the first place. So where do you think control is being sent?

Well Paul this code is a example.

I just added some the code of the roulette. My plan is to stop the roulette when i pus a input on the android app.

This works fine with 1 led. But when i have a roulette. It doesn't stop when i push the same input again.

So my problem is how to stop it?

And econjack. Sorry man i did't write this code. Its a example

My question is what to do?

while(1){
    digitalWrite(3, pin_value);
    delay(500);
    digitalWrite(3, LOW);
    digitalWrite(4, pin_value);
    delay(500);
    digitalWrite(4, LOW);
    digitalWrite(5, pin_value);
    delay(500);
    digitalWrite(5, LOW);
    digitalWrite(6, pin_value);
    delay(500);
    digitalWrite(6, LOW);
    digitalWrite(7, pin_value);
    delay(500);
    digitalWrite(7, LOW);
    digitalWrite(8, pin_value);
    delay(500);
    digitalWrite(8, LOW);
   }

Is this the loop you want to stop? Since this code never interacts with anything else it will never break and there's no way to make it. You could have a line in there that checks for serial input or checks for a button or whatever you want to use to tell it to break.

YES thats what i want. When a serian input is given. the loop has to stop. But i don't know how.

And econjack. Sorry man i did't write this code. Its a example

Ok, but I'm assuming you want to be able to program it at some point so it will do exactly what you want it to. To that end, look at the examples provided to you in the IDE. For example, take a look at File --> Examples --> Communications --> SerialCallResponse and try to figure out what each statement does. The underlying purpose of this Forum is self-discovery so simply saying it's not your code tell us you're expecting us to write your answer and that's not our job...it's yours.

I know that.

I just need some directions.

But i will look at that.

You should separate reading data from the Android from acting on the data read from the phone.

You need to ditch the delay() calls. The blink without delay example shows how.

When the Android sends "start roulette" (whatever that means), then you start the state machine/timing to make the LED(s) turn on or off at the appropriate times. When the Android sends "stop roulette" (whatever that means), you change to a not-doing-the-roulette-thing state.

I just need some directions.

That’s what all of us are doing. Paul just suggested using a State Machine, which is usable in something like 99% of all microcontroller programs. If you don’t know what a State Machine is, try searching the Arduino Forum using the Search facility of this Forum. If that doesn’t work, I just Googled “State Machine” and got 5.5million hits, so one of those will be perfect for helping you understand what they are. Studying examples, typing that code in, and playing with it is the best (only?) way to learn this stuff.

Aright thanks guys.

I will look it up and try till i "die" Haha

Thanks for the effort !

I'll reply when i make a progression.

If you want to stop the loop you have to do something inside the loop to stop it. The simplest is something like this:

while(1){

     // do some stuff

     if(digitalRead(buttonPin) == LOW){      // if button is pressed
          break;
     }
  
    //  Maybe do some more stuff that won't happen on the iteration we break
}

Now this loop checks a button and if it is pressed terminates the loop. Is that what you're asking? It doesn't have to be a button press, you could have it read from Serial or anything else. The button press is just the easiest for me to write to illustrate the point.

You could even have the check be part of the while condition like this:

while (digitalRead(buttonPin) == HIGH)  {   // keep looping as long as the button is not pressed

     //do some stuff

}

And now if you press the button and that digitalRead gets LOW the while loop doesn't execute again and you move on.

You've got to consider exactly what you want it to do when it's not actually "doing roulette" Waiting for an input from the user is an active task that belongs in the loop.If you stop the loop then it has no way of checking to see if the user is pressing anything. It'll just be a brick.