Stuck in a project

Hi, I am trying to make a project that consists in controlling a car with the mobile phone via bluetooth.
I dont want that the car just follow in real time the orders of the mobile I want to lay up all the orders that the user clicked on the mobile app and when the user clicks the ON button all the orders run.
The problem is that I have added a speed sensor and when the motor reaches the value that I have inserted doesnt stop.
Here’s the

Code:

#include <SoftwareSerial.h> //Librería que permite establecer comunicación serie en otros pins
#include <TimerOne.h>

//Aquí conectamos los pins RXD,TDX del módulo Bluetooth.
SoftwareSerial BT(10,11); //10 RX, 11 TX.

unsigned int RIGHTHOLES=0; 
unsigned int LEFTHOLES=0;
char cadena[25]; //Creamos un array de caracteres de 25 cposiciones
int i=0; //Tamaño actual del array
int j=0;
int cl=0;
int data=0;
int state= 0;       // Estado motor        
int velocity= 120;
int E1 = 5;  // SPEED MOTOR1
int M1 = 4;  // DIRECTION MOTOR 1
int E2 = 6; //VSPEED MOTOR 2
int M2 = 7; // DIRECTION MOTOR 2



void setup()
{
BT.begin(9600);
Serial.begin(9600);
pinMode(13,OUTPUT);

Serial.begin(9600);

pinMode(E1, OUTPUT); 
pinMode(M1, OUTPUT);
pinMode(E2, OUTPUT); 
pinMode(M2, OUTPUT); 

Timer1.initialize(1000000); // set timer for 1sec
attachInterrupt(1, RH, RISING);  // Motorb Dret
attachInterrupt(0, LH, RISING);  // Motorb Dret
Timer1.attachInterrupt( timerIsr ); // enable the timer

}

void RH()  // counts from the speed sensor
{
RIGHTHOLES++;  // increase +1 the counter value
} 

void LH()  // counts from the speed sensor
{
LEFTHOLES++;  // increase +1 the counter value
} 

void timerIsr()
{
Timer1.detachInterrupt();  //stop the timer
Serial.print("RIGHT MOTOR:");
Serial.println(RIGHTHOLES,DEC); 
Serial.print("LEFT MOTOR:");
Serial.println(LEFTHOLES,DEC);  
Timer1.attachInterrupt( timerIsr );  //enable the timer
}




void executeprogram()
{
for (j=0;j<i-1;j++)    //desde 0 a i (numero de ordenes)
  {
    char data=cadena[j];  //pone en dato ordenes almacenadas en cadena
    Serial.print(RIGHTHOLES);
  
    if (data == '3') 
    {
      while  (RIGHTHOLES<500)
       {
          analogWrite (E1, 200);  // set speed of motor (0-255)
          digitalWrite(M1, 1);  // set rotation of motor to Clockwise

        }
      while  (RIGHTHOLES>500)
        {
          analogWrite(E1, 0);  // set speed of motor (0-255)
          digitalWrite(M1, 1);  // set rotation of motor to Clockwise
          

        }
        while (LEFTHOLES<500) 
          {
          analogWrite  (E2, 200);
          digitalWrite (M2,1);
          }

        while (LEFTHOLES>500) 
          {
          analogWrite  (E2, 0);
          digitalWrite (M2,1);
           }
         RIGHTHOLES=0;
         LEFTHOLES=0;
         clean();  
    }
  }



   
}  
    


//Limpia el array
void clean()
{
for (cl=0; cl<=i; cl++)
{
cadena[cl]=0;
}
i=0;
}


void loop()
{
if(BT.available())      //Comienza la programación
  {
      char data=BT.read(); //Guarda los datos carácter a carácter en la variable "dato"
      cadena[i++]=data;    //Despues en cadena
    
        if (data=='1')        //Si se pulsa ON ejecuta programa en 2 segundos. Se indica con led 13 (verd) encendido.
          {
            digitalWrite(13,1);
            for (j=0;j<i;j++)    //Escribe cadena comandos en puerto serie
            {
              Serial.print(cadena[j]);
             }
         
                Serial.print('\n');
                Serial.print(i);           //Numero de ordenes
                Serial.print(" ordenes");
                Serial.print('\n');
            executeprogram();
          }
      
       if (data == '2') 
        {
        clean(); 
                
        }
}
}
unsigned int RIGHTHOLES=0;
unsigned int LEFTHOLES=0;

Since these are used in three different functions, one of which is an interrupt handler, these must be declared volatile.

Actually, two of the functions that access these variables are interrupt handlers, but timerIsr() should NOT be accessing them for the purpose of Serial.print()ing them, since Serial.print() should NOT be used in an interrupt handler (since it relies on interrupts).

I can't see how this will work

void timerIsr()
{
  Timer1.detachInterrupt();  //stop the timer
  Serial.print("RIGHT MOTOR:");
  Serial.println(RIGHTHOLES,DEC);
  Serial.print("LEFT MOTOR:");
  Serial.println(LEFTHOLES,DEC);  
  Timer1.attachInterrupt( timerIsr );  //enable the timer
}

If this really is an Interrupt Service Routine it will have interrupts disabled so the Serial stuff can't work properly and detaching the interrupt won't fix that AFAIK.

Also, an ISR should complete very quickly so it does not interfere with other stuff and Serial stuff is sloowww.

...R

Not related to your question (PaulS addressed that), it is convention to not name variables in all-caps. All-caps, by convention, is used for things defined in #define statements.

The compiler does not care, but you may find that convention useful for sharing code.

Thanks to all of you, I have solved the problem with the Serial.Print and with volatile data but now the problem is that I don’t know how to make that happens at the same time, I mean to make it works, because it doesnt work well.

void EXECUTEPROGRAM()
{
    for (j=0;j<i-1;j++)    //desde 0 a i (numero de ordenes)
      {
        char data=cadena[j];  //pone en dato ordenes almacenadas en cadena
   
      
        if (data == '3') // 
        {
          while  (RIGHTHOLES<100 || LEFTHOLES<100) 
           {
              analogWrite  (E1, speedR);  // THIS IS A VARIABLE FOR THE SPEED(150)
              digitalWrite (M1, 1);  // set rotation of motor to Clockwise
              analogWrite  (E2, speedL); // THIS IS A VARIABLE FOR THE SPEED(150)
              digitalWrite (M2,1);
           }
              if (RIGHTHOLES>100)
                {
                   speedR=0;
                   
                }
              if (LEFTHOLES>100)
                {
                   speedL=0;
             

                

            
           }
                         
              RIGHTHOLES=0;
              LEFTHOLES=0;
              clean();  
        }
      }
 


       
 }
          while  (RIGHTHOLES<100 || LEFTHOLES<100)
           {
              analogWrite  (E1, speedR);  // THIS IS A VARIABLE FOR THE SPEED(150)
              digitalWrite (M1, 1);  // set rotation of motor to Clockwise
              analogWrite  (E2, speedL); // THIS IS A VARIABLE FOR THE SPEED(150)
              digitalWrite (M2,1);
           }
              if (RIGHTHOLES>100)

If statements and while statements are NOT interchangeable. RIGHTHOLES and LEFTHOLES are not changed while the while loop is running, so once the while loop starts, only the interrupts will change the values of RIGHTHOLES and LEFTHOLES. It should be obvious that nothing in the body depends on the values for RIGHTHOLES or LEFTHOLES, so the whole body belongs before the statement. The body of the statement should be empty.

The interrupt handlers only increment the values in RIGHTHOLES and LEFTHOLES, so the while loop, if it starts, will end only when RIGHTHOLES or LEFTHOLES overflows.

You need to make a call on the fine folks at http://snippets-r-us.com for help with your snippets.

Thanks all of you very much, I will keep that in mind for my project