Pages: [1]   Go Down
Author Topic: getKey keypad entry not detected when moved out of void loop  (Read 505 times)
0 Members and 1 Guest are viewing this topic.
London
Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all,
A rotary encoder displays a simple Lcd menu with 7 menu items.
For each menu item I need to be able to change the value of the associated  variable (DropSize, NumberOf Drops, etc)  using the keypad.
The various menu items are displayed in menuDisplay(). Currently the routine 1 works and accepts a keypad entry. However, I need to move this working  section ( getKey(), enterNewNumber() and getNumber() from the void loop  into the menuDisplay() so that it accepts a keypad entry for  each of the different menu items,
When I move the working segment into a menu itme within menuDisplay() keypad entry is no longer detected. Thanks in advance for any advice.


Woking routine 1

Code:

void loop(){// open void
  if (fired) // if ISR has triggered calls Rotary Encoder routine   
  {// open if
    menuDisplay();// Display menu options on LCD
    fired = false;
  }// close if
  // -------end of encoder
  [color=blue]char key = getKey();// this routine will be used to change all integer variables like
  // DropSize, FlashDelay, Number of drops etc
  [color=green][font=Verdana]if(key != 0) // Do nothing if no key is pressed.
  {// open first if
    switch (key)
    {// open switch key
    case '#':
      enterNewNumber();
      getNumber(0,1,rotaryCount);
      break;
    }// close switch
  }// close first if[/font][/color]
}[/color]// close void loop returns with the key pressed, or 0 if no key is pressed


The Serial output for the above working code is shown below. Comments and user input are shown between <  >.
Code:
Serial output for working routine 1

Drop Control Menu 
Turn Knob for       
Menu options       
Inside menuDisplay()          < knob turned to menu "number of drops">
Number of drops   0
            rotaryCount = 0
Row cleared
enterNewNumber()
Before calling getNumber()
inside getNumber
currentKeyIn = 1          < 5 on keypad pressed>
num = 5
currentKeyIn = 2          < 6 on keypad pressed>
num = 56
currentKeyIn = 0
Row cleared
New number = 56
A = Save to EEPROM 


Here is the code where the getKey() and getNumber() are moved from the void loop to the menuDisplay(). The keypad entry is not detected.

Code:
void menuDisplay() { // Displays LCD Menu options when rotary encoder is moved
  // Called from within void loop
  Serial.println("Inside menuDisplay() ");// testing
  rotaryCount = constrain(rotaryCount, 0, Number_of_Menu_Items);// if rotary count is > 7 back to 0.
  if (rotaryCount>=Number_of_Menu_Items)
  {// open if
    rotaryCount=0; // loop menu to first item on menu
  }// close if
  initialiseLcd();// clear and set cursor home
 char key = 0;
  switch(rotaryCount)
  {// used for displaying Menu items and variable value.
  case S_number_Of_Drops:
    displayNumber_Of_Drops(); //
      key = getKey();//
  if(key != 0) // Do nothing if no key is pressed.
  {// open first if
    switch (key)
    {// open switch key
    case '#':
      enterNewNumber();// on lcd print  "enter new number"
      getNumber(0,1);// gets two char entry and convert to an integer
      break;
    }// close switch
  }// close first if

  case S_dropSize:  //
    displayDrop_Size();
         key = getKey();//
  if(key != 0) // Do nothing if no key is pressed.
  {// open first if
    switch (key)
    {// open switch key
    case '#':
      enterNewNumber();// on lcd print  "enter new number"
      getNumber(0,1);// gets two char entry and convert to an integer
      break;
    }// close switch
  }// close first if

    break;


Here is the Serial output for the above code which is not working. No keypad entry is detected.

Code:
Set up started
Drop Control Menu 
Turn Knob for       
Menu options       
Inside menuDisplay()    < rotary encoder knob move to "Number of Drops" menu item.
Number of drops   0
            rotaryCount = 0
Inside getKey()
keypressed        <nothing happens when any key is pressed>



Here is the working getNumber() segment.
Code:


int getNumber(byte lcdCol, byte lcdRow)// lcdCol and LcdRow are located in getNumber(10,1)
/*
called from get Key()
getNumber() is used to change the variables like DropSize, NumberOfShots FlashDelay etc.
 From the keypad entry it creates a 1-3 digit number which will be stored in EEPROM.
 */
{// open getNumber
  Serial.println("inside getNumber");
  unsigned int num = 0;
  lcd.setCursor(lcdCol, lcdRow);  //put the cursor to (0,1)
  char key = getKey();
  while(key != '#')// # is the start of keypad entry key
  {// open while
    switch (key)
    {// open switch
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
      Data[currentKeyIn++]= key;// each digit entry is stored in array
      lcd.print(key);// print key
      num = num * 10 + (key - '0');// converts char key entry to integer
      Serial.print("currentKeyIn = ");
      Serial.println(currentKeyIn);
      Serial.print("num = ");
      Serial.println(num);// test to see if correct integer has been entered
      break;

    case 'D'://backspace to beginning of number to change digits over write and then press '*'
      Data[currentKeyIn--]= key;
      cursorPosition = currentKeyIn;
      lcd.setCursor(cursorPosition, lcdRow);
      num = 0;
      Serial.print("currentKeyIn = ");
      Serial.println(currentKeyIn);
      lcd.blink();   // blinks over last digit entered.
      Serial.print("currentKeyIn = ");
      Serial.println(currentKeyIn);
      Serial.print("num = ");
      Serial.println(num);
      break;

    case '*':// signals end of input
      while(currentKeyIn !=0)
      {   
        Data[currentKeyIn--] = 0; //clear for new data
      }
      Serial.print("currentKeyIn = ");
      Serial.println(currentKeyIn);// working = 0
      lcd.noBlink();
      lcd.setCursor(0,3);
      clearLcdRow();// clears "Enter new number
      strcpy_P(buffer_a, (char*)pgm_read_word(&(Menu_List[9]))); // "A = Save to EEPROM"
      lcd.setCursor(0,3);//
      lcd.print(buffer_a);
      Serial.println(buffer_a);
      return num;// required otherwise rotary encoder does not work.
     break;

    }// close switch
    key = getKey();// *****required here otherwise no response to keypad
  }// close while
  Serial.println("end of getNumber()");
}// close getNumber()



Here is the working getChar() segment.
Code:

char getKey()
{
  char key = 0;                                  // 0 indicates no key pressed
  for(int column = 0; column < numCols; column++)
  {
    digitalWrite(colPins[column],LOW);         // Activate the current column.
    for(int row = 0; row < numRows; row++)     // Scan all rows for  a key press.
    {
      if(digitalRead(rowPins[row]) == LOW)     // Is a key pressed?
      {
        delay(debounceTime);                   // debounce
        while(digitalRead(rowPins[row]) == LOW);
        // wait for key to be released
        key = keymap[row][column];             // Remember which key was pressed.
      }
    }
    digitalWrite(colPins[column],HIGH);     // De-activate the current column.
  }
  return key;  // returns the key pressed or 0 if none
  Serial.print("key pressed");
  Serial.println(key);
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8070
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 if (fired)                 // if ISR has triggered calls Rotary Encoder routine  
  {                           // open if
    menuDisplay();      // Display menu options on LCD
    fired = false;
  }                           // close if

Looks like menuDisplay() is only called when the knob is turned.  If you put the keyboard checking inside menuDisplay() it will only work when the knob is moved.  Having it in loop() was the right thing to do.
« Last Edit: March 29, 2013, 10:59:08 pm by Nick Gammon » Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

London
Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Having it in loop() was the right thing to do.

Thanks John.
Logged

Pages: [1]   Go Up
Jump to: