LCD display and keypad matrix do not get along well

Hi everyone,

The project
I'm in the process of building a calculator for fun. It has a 16x2 LCD screen along with 18 switches. The board is a Sparkfun pro micro 5V, 16MHz
The switches are wired in a matrix along with diodes to be able to read individual key pressed while using a limited number of pins.

The issue
I have the keypad working, I am able to print the right key as they are pressed or released.
I have the LCD working, I am able to print text on it nicely.
When I try to compile a sketch with both pieces of code mixed the board crashes (no text displayed, not event triggered from the keypad) and disappears (the usb connection is lost, serial connection is also broken)
On Ubuntu, it also freezes my computer, on windows the board just disappears from the connected devices list.

Here is the code I'm using: calculator/main.cpp at master · guyver2/calculator · GitHub
In its current shape the code works but only uses the keypad, almost everything related to the LCD screen is commented out.

I'm only having issue with the software part, the wiring is completely the same (LCD+switches) for all tests (see code for the pins used).

I'm using the adafruit_liquidCrystal along with the adafruit keypad libraries for consietency, but the same exat issue happens with the original versions of these libs:

parts:
LCD screen from Adafruit: Standard LCD 16x2 + extras [white on blue] : ID 181 : $9.95 : Adafruit Industries, Unique & fun DIY electronics and kits
Arduino Pro Micro 5V, 16MHz: https://www.sparkfun.com/products/12640

Any tip would help. I'm more a SW than HW guy and might have missed something obvious in the way...

Well, two things.

Firstly, we want to see the circuit schematic you are using, as we would definitely need to compare it with the code.

And while github is probably trustworthy, it is just a trifle rude to ask us to go there to find your code. You should post it here according to the instructions if you want many people to conveniently inspect it. :grinning:

The link to the code is a direct link to the only file I created, but you’re right I should have put it here in the first place.

#include <Arduino.h>
#include "Adafruit_LiquidCrystal.h"
#include "Adafruit_Keypad.h"

Adafruit_LiquidCrystal lcd(7, 8, 9, 5, 6, 4);

const byte ROWS = 5;
const byte COLS = 4;

// how the keypad has its keys laid out
const char keys[ROWS][COLS] = {
    {'C', 'M', '/', '*'},
    {'7', '8', '9', '-'},
    {'4', '5', '6', '+'},
    {'1', '2', '3', '='},
    {'#', '0', '.', '#'},
};

const byte rowPins[ROWS] = {2, 10, 16, 14, 15};
const byte colPins[COLS] = {18, 19, 20, 21};

Adafruit_Keypad customKeypad = Adafruit_Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup()
{
  Serial.begin(9600);
  customKeypad.begin();
  lcd.begin(16, 2);
  //lcd.print("hello, world!");
}
/*
char incomingByte = 0;

void loop()
{
  if (Serial.available() > 0)
  {
    // read the incoming byte:
    incomingByte = (char)Serial.read();

    // say what you got:
    Serial.print("I received: ");
    Serial.println(incomingByte);
    lcd.setCursor(15, 1);
    lcd.print(incomingByte);
  }
}
*/
void loop()
{
  customKeypad.tick();

  while (customKeypad.available())
  {
    keypadEvent e = customKeypad.read();
    Serial.print((char)e.bit.KEY);
    if (e.bit.EVENT == KEY_JUST_PRESSED)
      Serial.println(" pressed");
    else if (e.bit.EVENT == KEY_JUST_RELEASED)
      Serial.println(" released");
  }

  delay(10);
}

As for the wiring, isn’t the fact that I get both LCD and the keypad to work independently a proof that the wiring matches the code? (not a snarky question, I’m honestly interested if this is not the case)
To be clear, I did the wiring once (with both LCD and keypad plugged in) a and haven’t touched it since I started programming. I did not pull wires between tests.

Here is the wiring in plain text:

LCD screen:
RS pin → Pin 7
EN pin → Pin 8
D4 pin → Pin 9
D5 pin → Pin 5
D6 pin → Pin 6
D7 pin → Pin 4

switch matrix:
Row 0 → Pin 2
Row 1 → Pin 10
Row 2 → Pin 16
Row 3 → Pin 14
Row 4 → Pin 15
Col 0 → Pin 18
Col 1 → Pin 19
Col 2 → Pin 20
Col 3 → Pin 21

Can you post the actual code that causes the crash, these code examples have blocks commented in and out?
I don't know whats what.

DO you have a second Arduino micro?
Do you have a Arduino UNO or NANO?
Did you try a different USB port ?
What happens if you remove all serial commands from code?

regards

Problem solved.

Turns out I was somehow running out of memory. I used the function freeMemory() to track the memory usage between various calls and their was always a heap/stack collision at some point.

Converting the two main objects (lcd and keypad) into pointers and using dynamic allocation instead of global variables solved the issue.

thanks for the help.

Well, good work. :grinning:

I did not look into the code because I have no familiarity with the keypad library - I would just use my own code in that circumstance - but the only problem I can see is this declaration:

   keypadEvent e = customKeypad.read();

being in the main loop. :astonished:

being in the main loop. :astonished:

read updates the key pressed statuses. It has to be called in the loop otherwise it would not be able to detect new key pressed/released.