LCD turning on but not displaying things

Is there a problem with this code? The LCD seems to turn on but nothing is displayed.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// I2C address for the LCD
#define LCD_ADDRESS 0x27 // Change this to your LCD's I2C address

// Constants
const int redLEDs[6] = {8, 9, 10, 11, 12, 13};
const int buttonPins[6] = {2, 3, 4, 5, 6, 7};
const int timeButton30 = A1; // Button to set game time to 30 seconds (using pin A6, not available on Uno, choose another)
const int timeButton60 = A2; // Button to set game time to 60 seconds (using pin A7, not available on Uno, choose another)

// Initialize the LCD with I2C address
LiquidCrystal_I2C lcd(LCD_ADDRESS, 16, 2); // Change 16x2 if your LCD is a different size

void setup() {
  // Initialize Wire library for I2C
  Wire.begin();

  // Initialize LED pins
  for (int i = 0; i < 6; i++) {
    pinMode(redLEDs[i], OUTPUT);
    digitalWrite(redLEDs[i], LOW);
  }

  // Initialize button pins
  for (int i = 0; i < 6; i++) {
    pinMode(buttonPins[i], INPUT_PULLUP);
  }

  // Initialize time control button pins
  pinMode(timeButton30, INPUT_PULLUP);
  pinMode(timeButton60, INPUT_PULLUP);

  // Initialize the LCD
  lcd.init();
  lcd.backlight(); // Turn on the backlight
  lcd.setCursor(0, 0);
  lcd.print("Select Time:");
  lcd.setCursor(0, 1);
  lcd.print("30s or 60s");

  // Wait for time selection
  unsigned long gameTime = 30000; // Default 30 seconds
  while (true) {
    if (digitalRead(timeButton30) == LOW) {
      gameTime = 30000;
      break;
    }
    if (digitalRead(timeButton60) == LOW) {
      gameTime = 60000;
      break;
    }
  }

  // Clear the LCD
  lcd.clear();

  // Display game time selection
  lcd.setCursor(0, 0);
  lcd.print("Time Selected:");
  lcd.setCursor(0, 1);
  lcd.print(gameTime / 1000);
  lcd.print(" seconds");

  // Wait for 2 seconds before starting
  delay(2000);

  // Initialize score
  int score = 0;

  // Initialize start time
  unsigned long startTime = millis();

  // Start game loop
  while (millis() - startTime < gameTime) {
    // Choose random LED
    int ledIndex = random(6);
    int ledPin = redLEDs[ledIndex];

    // Light up red LED
    digitalWrite(ledPin, HIGH);

    // Wait for button press
    int buttonPressed = -1;
    unsigned long buttonStartTime = millis();
    while (buttonPressed == -1 && millis() - buttonStartTime < 2000) {
      for (int i = 0; i < 6; i++) {
        if (digitalRead(buttonPins[i]) == LOW) {
          buttonPressed = i;
          break;
        }
      }
    }

    // Check if button was pressed
    if (buttonPressed == ledIndex) {
      // Add point to score
      score++;
    } else if (buttonPressed != -1) {
      // Deduct point for wrong button press
      score--;
    }

    // Turn off red LED
    digitalWrite(ledPin, LOW);

    // Update score on LCD
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Score:");
    lcd.print(score);
    lcd.setCursor(0, 1);
    lcd.print("Time:");
    lcd.print((gameTime - (millis() - startTime)) / 1000);
    lcd.print("s");
  }

  // Display final score
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Final Score:");
  lcd.setCursor(0, 1);
  lcd.print(score);

  // Wait for 10 seconds before resetting
  delay(10000);

  // Reset LED states
  for (int i = 0; i < 6; i++) {
    digitalWrite(redLEDs[i], LOW);
  }
}

void loop() {
  // No need for any code here as the game logic is handled in setup()
}

Did you run the i2c scanner example to see if the LCD screen shows up there as address 0x27?

Edit: Did you try the LCD Display example included with the LCD library?

check with an I2C Scanner Sketch (you will find it in the Wire examples) if your LCD Expander is responding to 0x27 or to another address.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.