my project of reaction time game is not working, can anyone tell me the issues or edit the project and explain me what was my error?
Welcome to the forum
You started a topic in the Uncategorised category of the forum when its description explicitly tells you not to
Your topic has been moved to a relevant category. Please be careful in future when deciding where to start new topics
Not working.... Wiring is wrong, code is faulty or both of them.
@lohit1010 - post the code.
In the link, the code is for "blink", nothing else.
Obvsly that makes a poor reaction time game. ![]()
a7
Show the original link. Otherwise, choose one of many "arduino reaction time game" projects on the internet.
look this over
re-wire your button and LEDs as the comments describe.
more code can be added later
// simple button LED test code
const byte PinLed [] = { 10, 11, 12, 13 }; // wire LED anode (long lead) to 5V
// wire resistor between Ground
// and LED cathode (short lead)
const byte PinBut [] = { A1, A2, A3, A4 }; // wire between pin and ground
const int Npin = sizeof(PinLed);
enum { LedOff = HIGH, LedOn = LOW }; // LED wire active LOW
// -----------------------------------------------------------------------------
void loop ()
{
for (int n = 0; n < Npin; n++) {
if (LOW == digitalRead (PinBut [n]))
digitalWrite (PinLed [n], LedOn);
else
digitalWrite (PinLed [n], LedOff);
}
}
// -----------------------------------------------------------------------------
void setup()
{
Serial.begin (9600);
for (int n = 0; n < Npin; n++) {
pinMode (PinBut [n], INPUT_PULLUP);
pinMode (PinLed [n], OUTPUT);
digitalWrite (PinLed [n], LedOff);
}
}
Nice. Add timing (re: topic title) from when the LED illuminates to when the button is pressed and display on LCD, current color/time and average time.
My code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define BUZZER A2
#define LED1 2
#define LED2 3
#define LED3 4
#define LED4 5
#define BTN1 6
#define BTN2 7
#define BTN3 8
#define BTN4 9
#define START_BUTTON 10
LiquidCrystal_I2C lcd(0x20, 16, 2);
int leds[] = {LED1, LED2, LED3, LED4};
int buttons[] = {BTN1, BTN2, BTN3, BTN4};
int reactionTimes[5];
int roundNumber = 0;
int activeLED = -1;
unsigned long ledOnTime = 0;
unsigned long reactionTime = 0;
int bestTime = 999999;
long totalTime = 0;
void setup() {
Serial.begin(9600);
for (int i = 0; i < 4; i++) {
pinMode(leds[i], OUTPUT);
digitalWrite(leds[i], LOW);
pinMode(buttons[i], INPUT_PULLUP);
}
pinMode(START_BUTTON, INPUT_PULLUP);
pinMode(BUZZER, OUTPUT);
Wire.begin();
lcd.init();
lcd.backlight();
randomSeed(analogRead(A0));
showWelcome();
}
void loop() {
if (digitalRead(START_BUTTON) == LOW) {
delay(50);
while (digitalRead(START_BUTTON) == LOW) {
delay(5);
}
startGame();
}
}
void showWelcome() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Group 7");
lcd.setCursor(0, 1);
lcd.print("Press START");
}
void startGame() {
roundNumber = 0;
bestTime = 999999;
totalTime = 0;
for (int i = 0; i < 5; i++) {
reactionTimes[i] = 0;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Get Ready...");
delay(1000);
for (roundNumber = 1; roundNumber <= 5; roundNumber++) {
playRound();
if (roundNumber < 5) {
delay(1500);
}
}
showFinalResult();
delay(5000);
showWelcome();
}
void playRound() {
turnOffAllLEDs();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Round ");
lcd.print(roundNumber);
lcd.setCursor(0, 1);
lcd.print("Wait...");
int waitTime = random(4000, 7001);
unsigned long waitStart = millis();
while (millis() - waitStart < waitTime) {
if (anyInputButtonPressed()) {
wrongPress();
return;
}
}
activeLED = random(0, 4);
turnOffAllLEDs();
digitalWrite(leds[activeLED], HIGH);
ledOnTime = micros();
while (micros() - ledOnTime < 100000) {
if (digitalRead(buttons[activeLED]) == LOW) {
reactionTime = micros() - ledOnTime;
break;
}
for (int i = 0; i < 4; i++) {
if (i != activeLED && digitalRead(buttons[i]) == LOW) {
digitalWrite(leds[activeLED], LOW);
wrongPress();
return;
}
}
}
digitalWrite(leds[activeLED], LOW);
if (reactionTime == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Too Slow!");
lcd.setCursor(0, 1);
lcd.print("Game Over");
tone(BUZZER, 200, 500);
delay(2000);
return;
}
reactionTime = reactionTime / 1000;
reactionTimes[roundNumber - 1] = reactionTime;
totalTime += reactionTime;
if (reactionTime < bestTime) {
bestTime = reactionTime;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reaction:");
lcd.setCursor(0, 1);
lcd.print(reactionTime);
lcd.print(" ms");
tone(BUZZER, 1000, 100);
Serial.print("Round ");
Serial.print(roundNumber);
Serial.print(": ");
Serial.print(reactionTime);
Serial.println(" ms");
delay(1000);
reactionTime = 0;
}
bool anyInputButtonPressed() {
for (int i = 0; i < 4; i++) {
if (digitalRead(buttons[i]) == LOW) {
return true;
}
}
return false;
}
void wrongPress() {
turnOffAllLEDs();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong!");
lcd.setCursor(0, 1);
lcd.print("Game Over");
tone(BUZZER, 200, 1000);
delay(2000);
roundNumber = 5;
}
void showFinalResult() {
long averageTime = totalTime / 5;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Average:");
lcd.print(averageTime);
lcd.print("ms");
delay(2500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Best:");
lcd.print(bestTime);
lcd.print("ms");
lcd.setCursor(0, 1);
lcd.print("5 Rounds Done");
tone(BUZZER, 1200, 200);
delay(2500);
}
void turnOffAllLEDs() {
for (int i = 0; i < 4; i++) {
digitalWrite(leds[i], LOW);
}
}
I don't know what's the error, is it in code or circuit
here is the code
One more example of why the very first change one should make in their Arduino IDE is to set compile warning level to ALL.
arduino-cli compile -b arduino:avr:uno --warnings all --build-property "build.extra_flags=-Wno-strict-aliasing" --output-dir ~/tmp --no-color (in directory: /home/me/Documents/sketchbook/Uno_R3/test)
/home/me/Documents/sketchbook/Uno_R3/test/test.ino:31:16: warning: overflow in implicit constant conversion [-Woverflow]
int bestTime = 999999;
^~~~~~
/home/me/Documents/sketchbook/Uno_R3/test/test.ino: In function 'void startGame()':
/home/me/Documents/sketchbook/Uno_R3/test/test.ino:79:14: warning: overflow in implicit constant conversion [-Woverflow]
bestTime = 999999;
^~~~~~
/home/me/Documents/sketchbook/Uno_R3/test/test.ino: In function 'void playRound()':
/home/me/Documents/sketchbook/Uno_R3/test/test.ino:120:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (millis() - waitStart < waitTime) {
~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
/home/me/Documents/sketchbook/Uno_R3/test/test.ino:171:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (reactionTime < bestTime) {
~~~~~~~~~~~~~^~~~~~~~~~
/home/me/Documents/sketchbook/libraries/LiquidCrystal_I2C/LiquidCrystal_I2C.cpp: In member function 'void LiquidCrystal_I2C::begin(uint8_t, uint8_t, uint8_t)':
/home/me/Documents/sketchbook/libraries/LiquidCrystal_I2C/LiquidCrystal_I2C.cpp:71:39: warning: unused parameter 'cols' [-Wunused-parameter]
void LiquidCrystal_I2C::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
^~~~
Sketch uses 10618 bytes (32%) of program storage space. Maximum is 32256 bytes.
Global variables use 614 bytes (29%) of dynamic memory, leaving 1434 bytes for local variables. Maximum is 2048 bytes.
Compilation finished successfully.
I think @lohit1010 is attempting to use the same pins for both LEDs and buttons. I think it should work. Pin 2 is pulled down by 10K. When one of pins 3 to 6 is HIGH, the attached led will light. If the corresponding button is pressed, pin 2 will be pulled high.
EDIT: This circuit might work, but there is a danger of short-circuit if two or more buttons are pressed at the same time. The short circuit will damage the Arduino. With careful coding, this danger can be avoided. But I do not think @lohit1010 is experienced enough to avoid the danger.
What is connected to pins 7, 8, 9 in your circuit?
The code might work with the right circuit.
The circuit might work with the right code.
But this circuit and this code do not go together.
i don't understand how that's suppose to work without an external pull-down on pin-2
presumably using a common pin for all the buttons unnecessarily minimizes the time to monitor any button press by a few usec.
i think it's better for the OP to use a more conventional circuit that can easily be tested and then add code to measure/display reaction time and build it into a game
I updated my post #12, but not the above part.
But I later realised the circuit is dangerous and could cause a short circuit when more than one button is pressed, unless the code takes care of that by never setting any of the pins to LOW.
The posted code does set them to LOW. But the posted code does not seem to correspond with the circuit anyway.
Think about exchanging the buttons that will wear out in time with other ways to sense finger proximity.
Like by blocking light from a phototransistor or sensing the finger's electrical field (with a bit of metal, even foil) which is the basis of touch buttons in TV frames. That can work through an insulating cover.
Dead Simple Cap Sensing, enough to get started.
Holy CRAP! Arduino resurrected The Playground!
The Arduino Playground is a Major Source for answers and examples to do a huge range of things Arduino. Bookmark it!
ignoring the LCD and buzzer
start
timeout
timeout
but 2, led 2 - good 725
but 2, led 2 - good 441
but 1, led 1 - good 510
but 2, led 2 - good 370
but 0, led 0 - good 579
but 2, led 2 - good 352
but 2, led 2 - good 801
but 1, led 1 - good 319
but 2, led 0 - bad
timeout
but 0, led 0 - good 574
but 2, led 2 - good 461
but 0, led 0 - good 570
timeout
timeout
timeout
timeout
// simple button LED test code
const byte PinLed [] = { 10, 11, 12, 13 }; // wire LED anode (long lead) to 5V
// wire resistor between Ground
// and LED cathode (short lead)
const byte PinBut [] = { A1, A2, A3 }; // wire between pin and ground
const int Npin = sizeof(PinBut);
enum { LedOff = HIGH, LedOn = LOW }; // LED wire active LOW
char s [90];
// -----------------------------------------------------------------------------
void test ()
{
for (int n = 0; n < Npin; n++) {
if (LOW == digitalRead (PinBut [n]))
digitalWrite (PinLed [n], LedOn);
else
digitalWrite (PinLed [n], LedOff);
}
}
// -----------------------------------------------------------------------------
const unsigned long IdleMsec = 1000;
const unsigned long TmoutMsec = 3000;;
unsigned long periodMsec = IdleMsec;
unsigned long msec0;
unsigned long msec;
int iLed;
bool idle;
// ---------------------------------------------------------
void reset ()
{
digitalWrite (PinLed [iLed], LedOff);
iLed = random (0, Npin);
periodMsec = IdleMsec;
msec0 = msec;
idle = true;
}
// ---------------------------------------------------------
void loop ()
{
msec = millis ();
if (msec - msec0 >= periodMsec) {
if (idle) {
idle = false;
msec0 = msec;
digitalWrite (PinLed [iLed], LedOn);
}
else {
Serial.println (" timeout");
reset ();
}
}
if (! idle) {
for (int n = 0; n < Npin; n++) {
if (LOW == digitalRead (PinBut [n])) {
unsigned long dMsec = msec - msec0;
if (iLed == n)
sprintf (s, " but %d, led %d - good %lu", n, iLed, dMsec);
else
sprintf (s, " but %d, led %d - bad", n, iLed);
Serial.println (s);
reset ();
}
}
}
}
// -----------------------------------------------------------------------------
void setup()
{
Serial.begin (115200);
for (int n = 0; n < Npin; n++) {
pinMode (PinBut [n], INPUT_PULLUP);
pinMode (PinLed [n], OUTPUT);
digitalWrite (PinLed [n], LedOff);
}
Serial.println (" start");
reset ();
}
Arduino lost web-link control of the playground. Most or all of the external links into the playground are redirected to the main page (so they don't hear complaints of 404?). Even the main page has bad links to its internal (#subtopic) data.



