Hello everyone,
I am doing Arduino project for my high school class. It's simple reactor game, where person who clicks button first gets points and eventually wins. I have written and tested code on Arduino simulator website (tinkercad) but when I use that code in Arduino IDE and upload it to Arduino (which should be wired correctly, I checked 3 times) nothing happens. TX and RX lights blink few times and then everything stays the same. I will put my code below. Please note that this is my very first ever project and I appreciate all the help I get. Best wishes, Dzoni.
// C++ code
//
#include <LiquidCrystal.h>
#define B_LED_P1 8
#define R_LED_P2 9
#define G_LED 10
#define B_TST_P1 3
#define R_TST_P2 2
volatile bool b_tst_p1_pressed = false;
volatile bool r_tst_p2_pressed = false;
unsigned int score_blue = 0;
unsigned int score_red = 0;
unsigned int penalty_blue = 0;
unsigned int penalty_red = 0;
enum game_state_enum {idle, start_pressed, wait_rnd, reflex, winner};
game_state_enum game_state = idle;
unsigned int time_at_start = 0;
unsigned int rnd_generated_interval = 0;
unsigned int time_when_to_light_led = 0;
LiquidCrystal lcd_1(12, 11, 7, 6, 5, 4);
void setup()
{
lcd_1.begin(16, 2);
pinMode(B_LED_P1, OUTPUT);
pinMode(R_LED_P2, OUTPUT);
pinMode(G_LED, OUTPUT);
pinMode(B_TST_P1, INPUT_PULLUP);
pinMode(R_TST_P2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(B_TST_P1), tst_ISR_P1, RISING);
attachInterrupt(digitalPinToInterrupt(R_TST_P2), tst_ISR_P2, RISING);
}
void loop()
{
switch(game_state)
{
case idle:
lcd_1.setCursor(0,0);
lcd_1.print("Press any button");
digitalWrite(B_LED_P1, LOW);
digitalWrite(R_LED_P2, LOW);
digitalWrite(G_LED, LOW);
if (b_tst_p1_pressed || r_tst_p2_pressed)
{
b_tst_p1_pressed = false;
r_tst_p2_pressed = false;
game_state = start_pressed;
lcd_1.clear();
lcd_1.print("R: -- :B");
}
break;
case start_pressed:
if (r_tst_p2_pressed || b_tst_p1_pressed)
{
if (r_tst_p2_pressed)
{
penalty_red++;
}
if (b_tst_p1_pressed)
{
penalty_blue++;
}
r_tst_p2_pressed = false;
b_tst_p1_pressed = false;
}
time_at_start = millis();
rnd_generated_interval = random(500, 7000);
time_when_to_light_led = time_at_start + rnd_generated_interval;
game_state = wait_rnd;
break;
case wait_rnd:
if (r_tst_p2_pressed || b_tst_p1_pressed)
{
if (r_tst_p2_pressed)
{
penalty_red++;
}
if (b_tst_p1_pressed)
{
penalty_blue++;
}
r_tst_p2_pressed = false;
b_tst_p1_pressed = false;
}
if (millis() >= time_when_to_light_led)
{
digitalWrite(G_LED, HIGH);
game_state = reflex;
}
break;
case reflex:
if (b_tst_p1_pressed)
{
b_tst_p1_pressed = false;
digitalWrite(G_LED, LOW);
digitalWrite(B_LED_P1, HIGH);
score_blue++;
if (penalty_blue > 2){
score_blue = 0;
}
if (penalty_red > 2){
score_red = 0;
}
lcd_1.setCursor(9,0);
lcd_1.print(score_blue);
lcd_1.setCursor(6,0);
lcd_1.print(score_red);
penalty_blue = 0;
penalty_red = 0;
delay(3000);
digitalWrite(B_LED_P1, LOW);
if(score_blue==3){
game_state = winner;
break;
}
game_state = start_pressed;
}
if (r_tst_p2_pressed)
{
r_tst_p2_pressed = false;
digitalWrite(G_LED, LOW);
digitalWrite(R_LED_P2, HIGH);
score_red++;
if (penalty_blue > 2){
score_blue = 0;
}
if (penalty_red > 2){
score_red = 0;
}
lcd_1.setCursor(9,0);
lcd_1.print(score_blue);
lcd_1.setCursor(6,0);
lcd_1.print(score_red);
penalty_blue = 0;
penalty_red = 0;
delay(3000);
digitalWrite(R_LED_P2, LOW);
if(score_red==3){
game_state = winner;
break;
}
game_state = start_pressed;
}
break;
case winner:
if(score_blue==3){
lcd_1.clear();
lcd_1.setCursor(0,0);
lcd_1.print("Blue wins!");
delay(5000);
score_blue = 0;
score_red = 0;
penalty_blue = 0;
penalty_red = 0;
lcd_1.setCursor(0,0);
lcd_1.print("B: -- :R");
lcd_1.setCursor(9,0);
lcd_1.print(score_red);
lcd_1.setCursor(6,0);
lcd_1.print(score_blue);
game_state = idle;
}
if(score_red==3){
lcd_1.clear();
lcd_1.setCursor(0,0);
lcd_1.print("Red wins!");
delay(5000);
score_blue = 0;
score_red = 0;
penalty_blue = 0;
penalty_red = 0;
lcd_1.setCursor(0,0);
lcd_1.print("B: -- :R");
lcd_1.setCursor(9,0);
lcd_1.print(score_red);
lcd_1.setCursor(6,0);
lcd_1.print(score_blue);
game_state = idle;
}
break;
}
}
void tst_ISR_P1()
{
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
if (interrupt_time - last_interrupt_time > 20)
{
b_tst_p1_pressed = true;
}
last_interrupt_time = interrupt_time;
}
void tst_ISR_P2()
{
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
if (interrupt_time - last_interrupt_time > 20)
{
r_tst_p2_pressed = true;
}
last_interrupt_time = interrupt_time;
}