0

I'm hoping that someone might be able to help me figure out how to prevent my LCD from giving me double digits (i.e. 55, 66, 77 - instead of 5,6,7);

I have 2 functions that are printing, so I'm expecting the issue to be related to this.

Here's the code:

#include <Wire.h>

#include <Adafruit_LiquidCrystal.h>
#include <Keypad.h>

unsigned int deBounce = 100;
int curs = 0;


Adafruit_LiquidCrystal lcd(8, 9, 3, 10, 11, 12, 13);    

const byte numRows = 4; //number of rows on the keypad
const byte numCols = 4; //number of columns on the keypad

char keymap[numRows][numCols] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'.', '0', '#', 'D'}
};

char keymap2[numRows][numCols] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'.', '0', '#', 'D'}
};

//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {25, 27, 29, 31};
byte colPins[numCols] = {17, 19, 21, 23};

//initializes an instance of the Keypad class
Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
Keypad myKeypad2 = Keypad(makeKeymap(keymap2), rowPins, colPins, numRows, numCols);

char enteredDigits[10]  = " ";//null terminated character array to hold entry
char enteredDigits2[10]  = " ";//null terminated character array to hold entry
boolean entryComplete = false;
boolean entryComplete2 = false;
boolean first = 0;

float result; // Create an integer for the result of 'A' and 'B' (in this case, it will be the 'Inductance' x 'Capacitance')
float power = .5; // Used in the equation
float r1; // Result 1
float r2; // Result 2
float r3; // Result 3
float r4; // Result 4
float fc; // Result 5
float power2 = 6; // Used in the last part of the equation
float result2 = 10; // Also used in the last part of the equation
#define TWO_PI 6.283185307179586476925286766559 // two Pi (:
float num1;
float num2;


void setup()
{
  myKeypad.setDebounceTime(deBounce);
  myKeypad2.setDebounceTime(deBounce);

  lcd.begin(16, 2);
}

void loop()
{
  getNumber();
  if (entryComplete)
  {
    entryComplete = false;
    lcd.setCursor(0, 1);
    num1 = atof(enteredDigits);
    first = 1;
  }

  if (first == 1) {
    getNumber2();
    if (entryComplete2)
    {
      entryComplete2 = false;
      lcd.setCursor(0, 1);
      num2 = atof(enteredDigits2);
      values();
      first = 0;
    }
  }
}

void getNumber()
{
  static byte index = 0;
  char key = myKeypad.getKey();
  if (key != NO_KEY)
  {
    switch (key)
    {
      case '0': case '1': case '2': case '3': case '4':
      case '5': case '6': case '7': case '8': case '9': case'.':
        lcd.print(key);//print entry on top row
        enteredDigits[index] = key;//build character string
        index++;
        enteredDigits[index] = '\0'; //null terminate
        break;

      case '#':
        lcd.setCursor(8, 0);
        lcd.print(F("= Ind."));
        delay(1000);
        entryComplete = true;
        index = 0;//reset for next entry
        break;

      case 'D'://clear entry and display
        memset(enteredDigits, '\0', 10); //clear buffer
        lcd.clear();
        index = 0;
        entryComplete = false;
        break;
    }
  }
}


void getNumber2()
{
  static byte index2 = 0;
  char key2 = myKeypad2.getKey();
  if (key2 != NO_KEY)
  {
    switch (key2)
    {
      case '0': case '1': case '2': case '3': case '4':
      case '5': case '6': case '7': case '8': case '9': case'.':
        lcd.print(key2);//print entry on top row
        enteredDigits2[index2] = key2;//build character string
        index2++;
        enteredDigits2[index2] = '\0'; //null terminate
        break;

      case '#':
        lcd.setCursor(8, 1);
        lcd.print(F("= Cap."));
        delay(1000);
        entryComplete2 = true;
        index2 = 0;//reset for next entry

        break;

      case 'D'://clear entry and display
        memset(enteredDigits2, '\0', 10); //clear buffer
        lcd.clear();
        index2 = 0;
        entryComplete2 = false;
        break;
    }
  }
}

void values() {

  result = num1 * num2; // If a 'comma' key is pressed (in this case, the # key on a 4x4 keypad)

  lcd.clear(); // Clear the writing from the LCD
  lcd.setCursor(0, 0); // Set the cursor position to the first block on the first line
  lcd.print(F("Ind. =")); // Display 'Ind. =' onto the LCD Screen
  lcd.setCursor(7, 0); // Set the cursor position to display after the previous text
  lcd.print(num1, 4); // Display the number value given to num1
  lcd.setCursor(0, 1); // Set the cursor for the first block on the second line
  lcd.print(F("Cap. ="));// Display 'Cap. =' onto the LCD Screen
  lcd.setCursor(7, 1); // Set the cursor position to display after the previoust text
  lcd.print(num2, 2); // Display the number value given to num2
  delay(2500);

  r1 = pow(result, power);
  r2 = r1 * TWO_PI;
  r3 = 1 / r2;
  r4 = pow(result2, power2);
  fc = r3 * r4;
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(F("Res. Peak =")); // Display the answer onto the LCD Screen
  lcd.setCursor(0, 1);
  lcd.print(fc);
  lcd.setCursor(9, 1);
  lcd.print(F("(Hz)"));
}
  • I fear it's related to debounce time and the duration of pressing a keypad button. Is it always exactly duplicating the entries? What about a debug output (Serial) to see more, what's going on? – DataFiddler Oct 8 at 11:47
  • Hmm, I didn't notice anything after changing the debounce settings. Even with a relatively high debounce (500 milliseconds), it still prints double. – zRockafellow Oct 8 at 14:14
  • If I take out this line from the getNumber2 function: – zRockafellow Oct 8 at 14:15
0

You need to break your code into parts and debug it part by part. Start by printing a single character to the LCD once, and setting a bool so you don't do it repeatedly. If that works, the LCD is not your problem.

As DataFiddler suggested, bounce problems with your switch seem like a likely culprit. Add serial print statements to your button handling code and see if it's being triggered more than once. If so, research debouncing code.

0

I had to remove this line from the getNumber2 function!

lcd.print(key2);

works now (:

  • keymap2 is not useful at all and just a duplicate. But having both keypads act on the same pins is your issue. Or don't you intend to have two keypads ? – DataFiddler Oct 8 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.