Counter problem

HI everybody,

Fisrt of all, sorry for my english.

I explain my problem and will be happy if you would help me.

I want to do a game, like a Master Mind whit round time.
I make the code for the counter whit the popular mills() and it was nice.
I have the keypad librarie, when i want to get the number ( getKey, only returns if there is a key pressed ), I.m forced to write a loop in the code whit the GetKey in.
the loop break when the key is pressed and it work fine.
But the problem is when i want to input numbers in the keypad( could be other action) and want to time passes normally.
When I write a loop in the code, the counter stop until the loop is breaked.

Dou you know any solution for this kind of problem??
Can I make the counter isolated? thats will be the better option
Can I read the input whitout a loop??

Thank all of you.

Doesn't the keypad library have a non-blocking call to tell you whether something has been pressed?

http://www.arduino.cc/playground/Code/Keypad

Looks to me like getState() should do the trick.

The keypad library is non-blocking in general. Calling getKey returns 0 if no key is pressed.

As for the original question, code please? I don't understand what is happening from the description.

thanks for the quick response.

Now, I can´t paste the code, Im in the office.

I try to get the data input of the keypad whit a Listener, but it doesn´t work.
The problem is when the program pass a getKey, dont wait to get a key, and then go on whit the rest of the program.
I want to wait until a key is pressed, ( thats the big problem) and I write a code like:

do {
int keypressed = getKey();

} while keypressed != 0;

the loop stop the counter until a key is pressed.

Despite the large gap between the lines there's nothing going to happen until that do loop exits, ie a key is pressed, so of course anything outside this loop will stop for the duration.

As Nick said, code please.


Rob

here come the code:

this is the program for the time of the counter
TiempoDelay = 1sec

//--------------------TIEMPO-------------------------------------

void TIEMPO(){ // sub-rpograma controla el tiempo


  //PASO DEL TIEMPO SIN DELAY CON LA FUNCION MILLS
  unsigned long TiempoDelayActual = millis();
  if(TiempoDelayActual - TiempoDelayAnterior > TiempoDelay) {
    // Salva el tiempo anterior
    TiempoDelayAnterior = TiempoDelayActual;  
   
    tone(pin_led_rojo, 100000, 500);    // Parpadeo del led usando la funcion tone
    RELOJ();
    
  }
}
//--------------------TIEMPO-------------------------------------

And thats its the part of the game:

//MASTERMIND

void MASTERMIND () {

  
  
  //lcd.clear();              // BORRA LA PANTALLA
  lcd.setCursor(0, 0);      // posiciona el cursor
  lcd.print("MASTERMIND");  // imprime en pantalla 
  lcd.setCursor(0, 1);      // posiciona el cursor
  lcd.print("Selec. dif:");  // imprime en pantalla 
  lcd.setCursor(12, 1);      // posiciona el cursor
  keypad.getKey();
  
if (tecla_num != 0 && tecla_num < 10)
   {
   
   muestra_texto = true;
   mm_digitos = tecla_num; 
   lcd.clear();              // BORRA LA PANTALLA
   lcd.setCursor(0, 0);      // posiciona el cursor
   lcd.print("JUEGO DE   DIG");  // imprime en pantalla el boton pulsado
   lcd.setCursor(9, 0);      // posiciona el cursor
   lcd.print(mm_digitos);  // imprime en pantalla el boton pulsado
   delay(2000);
   MASTERMIND_JUEGO ();
   primera_pasada = false; //para qu no repita el loop del main
   return;
   }
  else
  {
   muestra_texto = false;
  }



  
}


void    MASTERMIND_JUEGO() {
  
  lcd.clear(); 
  lcd.setCursor(0, 1);
  lcd.cursor();

  //primera_pasada = false;

  while ( mm_digitos > 0){
   
   switch (mm_digitos){
          case 1:
            tdigito1 = keypad.getKey(); mm_digitos--;  
            break;
          case 2:
            tdigito2 = keypad.getKey(); mm_digitos--;
            break;
          case 3:
            tdigito3 = keypad.getKey(); mm_digitos--;
            break;
          case 4:
            tdigito4 = keypad.getKey(); mm_digitos--;
            break;
          case 5:
            tdigito5 = keypad.getKey(); mm_digitos--;
            break;
          case 6:
            tdigito6 = keypad.getKey(); mm_digitos--;
            break;
          case 7:
            tdigito7 = keypad.getKey(); mm_digitos--;
            break;
          case 8:
            tdigito8 = keypad.getKey(); mm_digitos--;
            break;
          case 9:
            tdigito9 = keypad.getKey(); mm_digitos--;
            break;
          
            
         
   }
  }
  int numerito = tdigito1 +tdigito2 + tdigito3 + tdigito4 + 48; //TEST
   
   if (numerito > 0) // TEST
  
  lcd.print(char(numerito)); //TEST


}



void keypadEvent(KeypadEvent tecla){   // intentar hacer un bulce que salte antes de que pase el segundo, comparandolo con el mills()
  switch (keypad.getState()){
    case HOLD:
      switch (tecla){
        case '0':
      tecla_num = 0;
      if (muestra_texto == true)
      lcd.print("0"); // imprime en pantalla el boton pulsado
      
      //return tecla_num;
      break;

    case '1':
      tecla_num = 1;
      if (muestra_texto == true)
      lcd.print("1"); // imprime en pantalla el boton pulsado
      //return tecla_num;
      break;


    case '2':
      tecla_num = 2;
      if (muestra_texto == true)
      lcd.print("2"); // imprime en pantalla el boton pulsado
      //return tecla_num;
      break;


    case '3':
      tecla_num = 3;
      if (muestra_texto == true)
      lcd.print("3"); // imprime en pantalla el boton pulsado
      //return tecla_num;
      break;


    case '4':
      tecla_num = 4;
      if (muestra_texto == true)
      lcd.print("4"); // imprime en pantalla el boton pulsado
      //return tecla_num;
      break;


    case '5':
      tecla_num = 5;
      if (muestra_texto == true)
      lcd.print("5"); // imprime en pantalla el boton pulsado
      //return tecla_num;
      break;


    case '6':
      tecla_num = 6;
      if (muestra_texto == true)
      lcd.print("6"); // imprime en pantalla el boton pulsado
      //return tecla_num;
      break;


    case '7':
      tecla_num = 7;
      if (muestra_texto == true)
      lcd.print("7"); // imprime en pantalla el boton pulsado
      //return tecla_num;
      break;


    case '8':
      tecla_num = 8;
      if (muestra_texto == true)
      lcd.print("8"); // imprime en pantalla el boton pulsado
      //return tecla_num;
      break;


    case '9':
      tecla_num = 9;
      if (muestra_texto == true)
      lcd.print("9"); // imprime en pantalla el boton pulsado
      //return tecla_num;
      break;


    case '*':
      tecla_num = 14;
      if (muestra_texto == true)
      lcd.print("*"); // imprime en pantalla el boton pulsado
      //return tecla_num;
      break;


    case '#':
      tecla_num = 15;
      if (muestra_texto == true)
      lcd.print("#"); // imprime en pantalla el boton pulsado
      //return tecla_num;  
      break;

    case 'A':
      tecla_num = 10;
      if (muestra_texto == true)
      lcd.print("A"); // imprime en pantalla el boton pulsado
      //return tecla_num;
      break;


    case 'B':
      tecla_num = 11;
      if (muestra_texto == true)
      lcd.print("B"); // imprime en pantalla el boton pulsado
      //return tecla_num;
      break;


    case 'C':
      tecla_num = 12;
      if (muestra_texto == true)
      lcd.print("C"); // imprime en pantalla el boton pulsado
      //return tecla_num;
      break;


    case 'D':
      tecla_num = 13;
      if (muestra_texto == true)
      lcd.print("D"); // imprime en pantalla el boton pulsado
      //return tecla_num;
      break;
      }
   
    
  }
}
  switch (keypad.getState()){
    case HOLD:
 
    case '1':

'1', '2', '3', etc. are not valid return values from keypad.getState(). They are from keypad.getKey(), but, then, HOLD isn't.

if the getState() dont return values, should i change that for a getKey??? and remove the HOLD part?

if the getState() dont return values,

It does; it returns (according to the page I linked) IDLE, PRESSED, RELEASED, or HOLD.
As PaulS says, it does not return '1' etc.

pal0man:
I write a code like:

do {

int keypressed = getKey();

} while keypressed != 0;




the loop stop the counter until a key is pressed.

You have thrown away whatever key you got. You need something more like:

byte keypressed;

do {
   keypressed = kpd.getKey();
} while (keypressed == 0);

// use 'keypressed' here ...

That loops (you get zero while no key is pressed) and when a key is pressed you now have it outside the loop.

The point is, I can´t use loops on my program, becose the loop stop the counter.
if i dont press the key, the counter dont go on.

Can I make to the same counter dont affect the loops? I mean, like a parallel program run simultaneously or a subprogram ?

Thanks

The point is, I can´t use loops on my program, becose the loop stop the counter.

Not if the loop is the counter.

pal0man:
The point is, I can´t use loops on my program, becose the loop stop the counter.
if i dont press the key, the counter dont go on.

Oh I see. You want to make the key be pressed within (say) 5 seconds? It helps to express the "real" problem rather than whether or not counters stop inside loops.

If that is what you want, then test for it, eg.

long start_time = millis ();
byte keypressed;

do {
   keypressed = kpd.getKey();
} while (keypressed == 0 && 
            millis () < (start_time + 5000));

if (keypressed)
  {
  // they pressed a key
  }
else
  {
  // timeout!
  }

... I can´t use loops on my program ...

Most programs use loops. You just have to make the loops do what you want them to. For example, inside them you could count things.