/*
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
*/
#include <U8x8lib.h>
const unsigned long INTERVAL = 1000;
const unsigned long LONG_TIME = 3000;
const int BLU_BTN_PIN = 2;
const int RED_BTN_PIN = 3;
const int BLU_LED_PIN = 7;
const int RED_LED_PIN = 8;
int currTeam = 0;
unsigned long teamTime[2];
unsigned long prevTime = 0;
unsigned long pressTime = 0;
bool isPaused = false;
U8X8_SSD1306_128X64_NONAME_HW_I2C oled(U8X8_PIN_NONE);
void checkButtons() {
static int oldRedState = HIGH;
static int oldBluState = HIGH;
static bool redIsPressed = false;
static bool bluIsPressed = false;
int redBtnState = digitalRead(RED_BTN_PIN);
if (redBtnState != oldRedState) {
oldRedState = redBtnState;
isPaused = false;
if (redBtnState == LOW) {
Serial.println("Red pressed");
redIsPressed = true;
} else {
Serial.println("Red released");
redIsPressed = false;
}
delay(20);
}
int bluBtnState = digitalRead(BLU_BTN_PIN);
if (bluBtnState != oldBluState) {
oldBluState = bluBtnState;
isPaused = false;
if (bluBtnState == LOW) {
Serial.println("Blue pressed");
bluIsPressed = true;
} else {
Serial.println("Blue released");
bluIsPressed = false;
}
delay(20);
}
if (redIsPressed && bluIsPressed) {
if (!isPaused) {
isPaused = true;
pressTime = millis();
} else {
if (millis() - pressTime >= LONG_TIME) {
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(BLU_LED_PIN, LOW);
currTeam = 0;
teamTime[0] = 0;
teamTime[1] = 0;
initOled();
Serial.println("Game reset!");
delay(3000);
Serial.println("Ready\n");
}
}
} else if (redIsPressed) {
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(BLU_LED_PIN, LOW);
oled.setCursor(0, 1);
oled.print(" RED Team ");
currTeam = 1;
} else if (bluIsPressed) {
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(BLU_LED_PIN, HIGH);
oled.setCursor(0, 1);
oled.print("BLUE Team ");
currTeam = 2;
}
}
void initOled() {
oled.clear();
oled.drawString(0, 0, "Owner:");
oled.drawString(0, 4, "Leader:");
oled.drawString(0, 6, " RED: 00:00:00");
oled.drawString(0, 7, "BLUE: 00:00:00");
}
void updateDisplay(unsigned long numSecs) {
char timeBuff[16];
int row = 0;
oled.setCursor(9, 4);
if (teamTime[0] > teamTime[1]) {
oled.print("RED ");
} else if (teamTime[0] < teamTime[1]) {
oled.print("BLUE");
} else {
oled.print("TIE ");
}
if (currTeam == 1) {
row = 6;
} else {
row = 7;
}
oled.setCursor(6, row);
int tHour = (numSecs / 3600);
int tMin = (numSecs / 60) % 60;
int tSec = numSecs % 60;
snprintf(timeBuff, 16, "%02d:%02d:%02d", tHour, tMin, tSec);
oled.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);
oled.begin();
oled.setFlipMode(2);
oled.setFont(u8x8_font_amstrad_cpc_extended_f);
initOled();
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]);
}
}
}
}
}