First post! I’m a complete beginner in Arduino with very little coding knowledge. Can someone tell me what’s wrong with my circuit? I’m building a reaction time game, and the code was generated by ChatGPT.
code:
#include <LiquidCrystal.h>
// LCD pins: RS, E, D4, D5, D6, D7
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
// LEDs on pins 2, 3, 4, 5
const int leds[4] = {2, 3, 4, 5};
// Buttons on A0 and A1
const int button1 = A0;
const int button2 = A1;
// Contrast pin
const int contrastPin = 6;
// Game variables
int currentLED = -1;
unsigned long ledOnTime = 0;
bool ledActive = false;
int roundCount = 0;
const int TOTAL_ROUNDS = 5;
int scores[5];
bool gameRunning = false;
void setup() {
Serial.begin(9600);
// Set LCD contrast (important for Tinkercad!)
analogWrite(contrastPin, 100);
delay(100);
// Initialize LCD
lcd.begin(16, 2);
delay(100);
// Setup LEDs
for(int i = 0; i < 4; i++) {
pinMode(leds[i], OUTPUT);
digitalWrite(leds[i], LOW);
}
// Setup buttons
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
randomSeed(analogRead(5));
// Welcome screen
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reaction Game");
lcd.setCursor(0, 1);
lcd.print("Press button");
delay(2000);
// Wait for button press to start
while(digitalRead(button1) == HIGH && digitalRead(button2) == HIGH) {
delay(100);
}
delay(500); // Debounce
gameRunning = true;
roundCount = 0;
}
void loop() {
if(!gameRunning) return;
// Check if all rounds completed
if(roundCount >= TOTAL_ROUNDS) {
showResults();
gameRunning = false;
return;
}
// Start new round
playRound();
}
void playRound() {
// Show round number
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Round ");
lcd.print(roundCount + 1);
lcd.print("/");
lcd.print(TOTAL_ROUNDS);
lcd.setCursor(0, 1);
lcd.print("Wait...");
// Random wait time (1-3 seconds)
long waitTime = random(1000, 3000);
delay(waitTime);
// Turn LED on for 500ms
currentLED = random(0, 4);
digitalWrite(leds[currentLED], HIGH);
// Display message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PRESS NOW!");
// Record LED on time
ledOnTime = millis();
// Keep LED on for 500ms
delay(500);
digitalWrite(leds[currentLED], LOW);
// Wait for button press (max 5 seconds)
unsigned long buttonWaitStart = millis();
bool buttonPressed = false;
unsigned long reactionTime = 0;
while(millis() - buttonWaitStart < 5000) {
if(digitalRead(button1) == LOW || digitalRead(button2) == LOW) {
reactionTime = millis() - ledOnTime;
buttonPressed = true;
break;
}
delay(10);
}
// Show result
lcd.clear();
if(buttonPressed) {
lcd.setCursor(0, 0);
lcd.print("Time:");
lcd.setCursor(0, 1);
lcd.print(reactionTime);
lcd.print(" ms");
scores[roundCount] = reactionTime;
Serial.print("Round ");
Serial.print(roundCount + 1);
Serial.print(" - Time: ");
Serial.print(reactionTime);
Serial.println(" ms");
} else {
lcd.setCursor(0, 0);
lcd.print("TOO SLOW!");
lcd.setCursor(0, 1);
lcd.print("No response");
scores[roundCount] = 9999;
}
delay(2000);
roundCount++;
// Wait before next round
if(roundCount < TOTAL_ROUNDS) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Round ");
lcd.print(roundCount + 1);
lcd.setCursor(0, 1);
lcd.print("Get ready...");
delay(2000);
}
}
void showResults() {
// Calculate best and average
int bestTime = 9999;
int totalTime = 0;
int validScores = 0;
for(int i = 0; i < TOTAL_ROUNDS; i++) {
if(scores[i] < 9999) {
if(scores[i] < bestTime) bestTime = scores[i];
totalTime += scores[i];
validScores++;
}
}
int avgTime = (validScores > 0) ? totalTime / validScores : 0;
// Screen 1: Average
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("GAME OVER!");
lcd.setCursor(0, 1);
lcd.print("Avg: ");
lcd.print(avgTime);
lcd.print("ms");
delay(3000);
// Screen 2: Best
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Best Time:");
lcd.setCursor(0, 1);
lcd.print(bestTime);
lcd.print(" ms");
delay(3000);
}