/*
Wokwi | questions
add leds to my sketch
Swampy Rat Paintball OP — 9/19/24 at 9:55 PM
I’m not smart and I don’t understand how to do this, is there
someone who can walk me through step by step on adding two leds
a red and a blue and having them come on when the corresponding
button is pressed and stays on until the other button is pressed
and then that led comes on and so on…..
https://wokwi.com/projects/409497674171128833
Switch team to red or blue
Simultaneous short press pauses / unpauses
Simultaneous long press resets game
Show Leader based on most time
TO DO
fix button release order issue (favors red)
*/
#include <LiquidCrystal_I2C.h>
const unsigned long INTERVAL = 1000;
const unsigned long LONG_TIME = 3000;
const int BLU_BTN_PIN = 2;
const int RED_BTN_PIN = 4;
const int BLU_LED_PIN = 3;
const int RED_LED_PIN = 5;
int currTeam = 0;
unsigned long teamTime[2];
unsigned long prevTime = 0;
unsigned long pressTime = 0;
bool isPaused = false;
bool redIsPressed = false;
bool bluIsPressed = false;
LiquidCrystal_I2C lcd(0x27, 20, 4);
void checkButtons() {
static int oldRedState = HIGH;
static int oldBluState = HIGH;
int redBtnState = digitalRead(RED_BTN_PIN);
if (redBtnState != oldRedState) {
oldRedState = redBtnState;
if (redBtnState == LOW) {
Serial.println("Red pressed");
redIsPressed = true;
if (bluIsPressed) {
if (!isPaused) {
isPaused = true;
Serial.println("Paused");
lcd.setCursor(19, 0);
lcd.print("*");
pressTime = millis();
} else {
isPaused = false;
Serial.println("Unpaused");
lcd.setCursor(19, 0);
lcd.print(" ");
}
}
} else {
//Serial.println("Red released");
redIsPressed = false;
}
delay(20);
}
int bluBtnState = digitalRead(BLU_BTN_PIN);
if (bluBtnState != oldBluState) {
oldBluState = bluBtnState;
if (bluBtnState == LOW) {
Serial.println("Blue pressed");
bluIsPressed = true;
if (redIsPressed) {
if (!isPaused) {
isPaused = true;
Serial.println("Paused");
lcd.setCursor(19, 0);
lcd.print("*");
pressTime = millis();
} else {
isPaused = false;
Serial.println("Unpaused");
lcd.setCursor(19, 0);
lcd.print(" ");
}
}
} else {
//Serial.println("Blue released");
bluIsPressed = false;
}
delay(20);
}
//if (!isPaused) {
if (redIsPressed && !bluIsPressed) {
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(BLU_LED_PIN, LOW);
lcd.setCursor(8, 0);
lcd.print("RED Team ");
currTeam = 1;
} else if (bluIsPressed && !redIsPressed) {
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(BLU_LED_PIN, HIGH);
lcd.setCursor(8, 0);
lcd.print("BLUE Team");
currTeam = 2;
}
//}
delay(50);
}
void initLcd() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Owner:");
lcd.setCursor(0, 1);
lcd.print("Leader:");
lcd.setCursor(2, 2);
lcd.print(" RED: 00:00:00");
lcd.setCursor(2, 3);
lcd.print("BLUE: 00:00:00");
}
void resetGame() {
isPaused = false;
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(BLU_LED_PIN, LOW);
currTeam = 0;
teamTime[0] = 0;
teamTime[1] = 0;
initLcd();
Serial.println("Game reset!");
delay(3000);
Serial.println("Ready\n");
}
void updateDisplay(unsigned long numSecs) {
char timeBuff[16];
int row = 0;
lcd.setCursor(8, 1);
if (teamTime[0] > teamTime[1]) {
lcd.print("RED ");
} else if (teamTime[0] < teamTime[1]) {
lcd.print("BLUE");
} else {
lcd.print("TIE ");
}
if (currTeam == 1) {
row = 2;
} else {
row = 3;
}
lcd.setCursor(8, row);
int tHour = (numSecs / 3600);
int tMin = (numSecs / 60) % 60;
int tSec = numSecs % 60;
snprintf(timeBuff, 16, "%02d:%02d:%02d", tHour, tMin, tSec);
lcd.print(timeBuff);
//Serial.println(timeBuff);
}
void setup() {
Serial.begin(115200);
pinMode(BLU_BTN_PIN, INPUT_PULLUP);
pinMode(RED_BTN_PIN, INPUT_PULLUP);
pinMode(BLU_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
lcd.init();
lcd.backlight();
initLcd();
Serial.println("Ready\n");
}
void loop() {
checkButtons();
if (currTeam != 0) {
if (!isPaused) {
if ( millis() - prevTime >= INTERVAL) {
prevTime = millis();
if (currTeam == 1) {
teamTime[0]++;
updateDisplay(teamTime[0]);
} else {
teamTime[1]++;
updateDisplay(teamTime[1]);
}
}
} else {
if (redIsPressed && bluIsPressed) {
if (millis() - pressTime >= LONG_TIME) resetGame();
}
}
}
}