(First Post!)
Here I have a code that... was made by ChatGPT, and modified for a project I'm making.
It supposedly is to do the following:
- When the Arduino turns on, the LEDS will do an idle animation that has running lights going from left to right. The LCD Screen should display "Reaction-Time-Test!"
- The player presses the button again to start the game.
- The LEDS will randomize its lighting first before it stops at one random LED, then the Piezzo Buzzer sounds for half a second.
- The LCD Screen should play a stopwatch before the player moves an object aligned to a lit up LED.
- The Ultrasonic Sensor will detect (after 2 seconds) if an object is in the correct distance. The farthest LED will be about 40cm, second LED will be about 30cm, third LED will be about 20cm, fourth LED will be about 10cm.
- After two seconds, if the player aligns the object to the LIT up LED, the LCD screen will display "CORRECT!" then the stopwatch stops. Step 3 will happen again for Round 2. There will be 10 rounds.
- However, if the player doesn't align the object with an LED, the LCD displays "WRONG!" and they go back to the first round, regardless of what round they are in.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
const int echoPin = 2; // EchoPin-ULTRASONIC
const int trigPin = 3; // TriggerPin-ULTRASONIC
const int ONOFF = 4; // BUTTON
const int BEEP = 5; // BUZZER
const int LIGHT[] = { 6, 7, 8, 9 }; // LEDs
// For ULTRASONIC (in centimeters)
const int distance1 = 40;
const int distance2 = 30;
const int distance3 = 20;
const int distance4 = 10;
// Game Configuration
const int rounds = 10;
enum GameModes {
IDLE,
WAITING_FOR_BUTTON,
RANDOMIZING_LEDS,
WAITING_FOR_OBJECT,
CHECKING_DISTANCE,
CORRECT_ANSWER,
WRONG_ANSWER
};
// Default GameMode
int state = IDLE;
unsigned long startTime;
int currentRound = 1;
int targetLED;
void setup() {
for (int i = 0; i < 4; i++) {
pinMode(LIGHT[i], OUTPUT);
}
pinMode(ONOFF, INPUT_PULLUP);
pinMode(BEEP, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
lcd.begin(16, 2);
lcd.backlight();
Serial.begin(9600);
}
void loop() {
switch (state) {
case IDLE:
lcd.setCursor(0, 0);
lcd.print("The R.T.T!");
lcd.setCursor(0, 1);
lcd.print("Press Button");
idleAnimation();
break;
case WAITING_FOR_BUTTON:
int buttonState = digitalRead(ONOFF);
Serial.println("Button State: " + String(buttonState));
if (ONOFF == LOW) {
startGame();
}
break;
case RANDOMIZING_LEDS:
randomizeLEDs();
break;
case WAITING_FOR_OBJECT: // DeterminingAnswer-STEP1
if (millis() - startTime >= 2000) {
state = CHECKING_DISTANCE;
}
break;
case CHECKING_DISTANCE: // DeterminingAnswer-STEP2
int distance = getDistance();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance == distance1) {
targetLED = 0;
state = CORRECT_ANSWER;
} else if (distance == distance2) {
targetLED = 1;
state = CORRECT_ANSWER;
} else if (distance == distance3) {
targetLED = 2;
state = CORRECT_ANSWER;
} else if (distance == distance4) {
targetLED = 3;
state = CORRECT_ANSWER;
} else {
state = WRONG_ANSWER;
}
break;
case CORRECT_ANSWER: // Answer-CORRECT
lcd.clear();
lcd.print("CORRECT!");
stopWatch();
currentRound++;
if (currentRound <= rounds) {
state = RANDOMIZING_LEDS;
} else {
state = WAITING_FOR_BUTTON;
lcd.clear();
lcd.print("Game Over!");
lcd.setCursor(0, 1);
lcd.print("Press button to play");
currentRound = 1;
}
break;
case WRONG_ANSWER: // Answer-WRONG
lcd.clear();
lcd.print("WRONG!");
state = WAITING_FOR_BUTTON;
currentRound = 1;
break;
}
}
void idleAnimation() {
static int ledIndex = 0;
static bool direction = true;
static unsigned long previousMillis = 0;
const unsigned long interval = 100;
if (millis() - previousMillis >= interval) {
previousMillis = millis();
if (direction) {
ledIndex++;
if (ledIndex == 4) {
ledIndex = 2;
direction = false;
}
} else {
ledIndex--;
if (ledIndex == -1) {
ledIndex = 1;
direction = true;
}
}
for (int i = 0; i < 4; i++) {
digitalWrite(LIGHT[i], i <= ledIndex ? HIGH : LOW);
}
}
}
void startGame() {
lcd.clear();
lcd.print("Round ");
lcd.print(currentRound);
lcd.setCursor(0, 1);
lcd.print("Get ready!");
state = RANDOMIZING_LEDS;
}
void randomizeLEDs() {
static unsigned long previousMillis = 0;
const unsigned long interval = 50;
static int ledIndex = 0;
if (millis() - previousMillis >= interval) {
previousMillis = millis();
ledIndex = random(4);
for (int i = 0; i < 4; i++) {
digitalWrite(LIGHT[i], i == ledIndex ? HIGH : LOW);
}
if (ledIndex == 0) {
startTime = millis();
state = WAITING_FOR_OBJECT;
tone(BEEP, 1000, 100);
}
}
}
int getDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2;
return distance;
}
void stopWatch() {
lcd.clear();
lcd.print("Round ");
lcd.print(currentRound);
lcd.setCursor(0, 1);
unsigned long roundStartTime = millis();
while (true) {
unsigned long elapsedTime = millis() - roundStartTime;
lcd.print(elapsedTime / 1000);
lcd.print(".");
lcd.print(elapsedTime % 1000 / 100);
if (digitalRead(ONOFF) == LOW) {
break;
}
}
}
Here's the problem:
- The idle animation works, but I'm only stuck at the idle animation mode and the click of a button doesn't do anything. It doesn't progress me to the game and/or to start a round.
- I tried to fix this myself by reading the signal from both button and the ultrasonic sensor. But Serial Monitor doesn't show up anything.
To be honest, it took me quite a while to figure out anything from what ChatGPT sent out so please try to simplify your answer to me. Any help is appreciated.
As background, I went from learning how to light up LEDs to .. this.
(As much as possible, resist the urge to smite me for using ChatGPT to make a project. Thanks )