I'm trying to get a screen, keypad and some switches (along with some other devices in the future) wired together to make a fun airsoft gamemode. I'm currently testing my keypad, screen and switches, and I'm running into a problem where the levers will always make my screen go to a blank white, and my keypad inputs have a random chance of making my screen go to a blank white. I'm gonna be honest, I really have no experience or knowledge with ESP32 or SPI stuff, and this is all very new to me.
What I do know: Trying to use the LCD_RST pin will just make my screen start white instead of doing it's initial fillScreen, the adafruit graphicstest.ino runs perfectly on the screen.
The hardware I'm using:
- ESP32-S3 N16R8 (Amazon.com: MusRock ESP32-S3-N16R8 Na IoT Development Board with Wi- Fi + BLE, Pre-Soldered AI Module : Electronics)
- Waveshare 2.4" LCD Display (Amazon.com: waveshare 2.4inch LCD Display Module with 65K RGB Colors 240×320 Resolution SPI Interface : Industrial & Scientific)
- Generic 4x4 keypad (which works)
Here's my code:
#include <Keypad.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
// SINGLE PINOUT
#define LED_PIN 4
#define SERVO 5
#define BUZZER 6
#define DEFUSE 7
#define TOGGLE_1 15
#define TOGGLE_2 16
// LCD PINOUT
#define LCD_CS 10
#define LCD_DC 9
#define LCD_RST 18
// RC522 PINOUT
#define RC522_CS 14
#define RC522_RST 8
// KEYPAD PINOUT
#define R1 45
#define R2 46
#define R3 21
#define R4 17
#define C1 38
#define C2 39
#define C3 41
#define C4 42
// SHARED
#define SPI_MOSI 11
#define SPI_SCK 12
#define SPI_MISO 13
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = { { '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' } };
byte rowPins[ROWS] = { R1, R2, R3, R4 };
byte colPins[COLS] = { C1, C2, C3, C4 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
char input[32];
uint8_t len = 0;
Adafruit_ILI9341 tft(LCD_CS, LCD_DC, -1);
bool toggle_1, toggle_2, dbt_1, dbt_2;
unsigned long debounce_1, debounce_2;
const unsigned long debounce = 50;
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("BOOT");
pinMode(TOGGLE_1, INPUT_PULLDOWN);
pinMode(TOGGLE_2, INPUT_PULLDOWN);
pinMode(RC522_CS, OUTPUT);
digitalWrite(RC522_CS, HIGH);
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_RED);
Serial.println("DISPLAY OK");
dbt_1 = false;
dbt_2 = false;
delay(100);
tft.fillScreen(ILI9341_BLACK);
}
void loop() {
toggle_1 = digitalRead(TOGGLE_1);
toggle_2 = digitalRead(TOGGLE_2);
if (toggle_1 != dbt_1 && (millis() - debounce_1 > debounce)) {
dbt_1 = toggle_1;
debounce_1 = millis();
tft.fillRect(20, 40, 100, 20, ILI9341_BLACK);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(2);
tft.setCursor(20, 40);
tft.print((toggle_1) ? "TOGGLE_1: ON" : "TOGGLE_1: OFF");
}
if (toggle_2 != dbt_2 && (millis() - debounce_2 > debounce)) {
dbt_2 = toggle_2;
debounce_2 = millis();
tft.fillRect(20, 60, 100, 20, ILI9341_BLACK);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(2);
tft.setCursor(20, 60);
tft.print((toggle_2) ? "TOGGLE_2: ON" : "TOGGLE_2: OFF");
}
char key = keypad.getKey();
if (key) {
readKeypad(key);
updateDisplay();
}
}
void readKeypad(char key) {
switch (key) {
case '#':
input[0] = '\0';
len = 0;
break;
case '*':
break;
default:
if (len < sizeof(input) - 1) {
input[len++] = key;
input[len] = '\0';
}
break;
}
}
void updateDisplay() {
int x = 20;
int y = 20;
tft.fillRect(20, 20, 320, 20, ILI9341_BLACK);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(2);
tft.setCursor(x, y);
tft.print(input);
Serial.println(input);
}
Any help is appreciated :)
EDIT:
Datasheet for the LCD: 2.4inch LCD Module - Waveshare Wiki
Waiting on seller to get datasheet for the ESP32, it seems to be a standard ESP32-S3-WROOM-U1 chip