//##########################################################################################################################################################################################################################
//### Initialize
//##########################################################################################################################################################################################################################
//MODULE 4 - BIG BUTTON
//#############################################################################################################
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include <Adafruit_NeoPixel.h>
// OLED display settings
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SH1107 display = Adafruit_SH1107(128, 64, &Wire, OLED_RESET);
// RGB LED settings
#define RGB_PIN 52
Adafruit_NeoPixel rgbLed = Adafruit_NeoPixel(1, RGB_PIN, NEO_GRB + NEO_KHZ800);
// Button settings
#define BUTTON_PIN 33
// Variables for game logic
unsigned long mod1RemainingTime;
int batteryCount = 3; // Example battery count, change as needed
bool litIndicatorCAR = true; // Example indicator status, change as needed
bool litIndicatorFRK = false; // Example indicator status, change as needed
String buttonColor = "blue"; // Example button color, change as needed
String buttonText = "Abort"; // Example button text, change as needed
//##########################################################################################################################################################################################################################
//### SETUP
//##########################################################################################################################################################################################################################
void setup() {
Serial.begin(9600);
//MODULE 4 - BIG BUTTON
// Initialize display
display.begin(0x3c, OLED_RESET);
display.display();
delay(2000); // Pause for 2 seconds
// Initialize RGB LED
rgbLed.begin();
rgbLed.show();
// Initialize button
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Display initial button color and text
displayButtonInfo();
}
//##########################################################################################################################################################################################################################
//### FUNCTIONS
//##########################################################################################################################################################################################################################
//#############################################################################################################
//### MODULE 4 - BIG BUTTON
//#############################################################################################################
void displayButtonInfo() {
display.clearDisplay();
display.setTextSize(1);
//display.setTextColor(SH110X_WHITE);
display.setCursor(0, 0);
display.print("Button Color: ");
display.println(buttonColor);
display.print("Button Text: ");
display.println(buttonText);
display.display();
}
void handleButtonPress() {
if (buttonColor == "blue" && buttonText == "Abort") {
holdButton("Blue strip");
} else if (batteryCount > 1 && buttonText == "Detonate") {
pressAndReleaseButton();
} else if (buttonColor == "white" && litIndicatorCAR) {
holdButton("White strip");
} else if (batteryCount > 2 && litIndicatorFRK) {
pressAndReleaseButton();
} else if (buttonColor == "yellow") {
holdButton("Yellow strip");
} else if (buttonColor == "red" && buttonText == "Hold") {
pressAndReleaseButton();
} else {
holdButton("Any other color strip");
}
}
void pressAndReleaseButton() {
// Press and immediately release the button
delay(100); // Simulate pressing time
}
void holdButton(String stripColor) {
// Light up the RGB LED based on the strip color
if (stripColor == "Blue strip") {
rgbLed.setPixelColor(0, rgbLed.Color(0, 0, 255)); // Blue
} else if (stripColor == "White strip") {
rgbLed.setPixelColor(0, rgbLed.Color(255, 255, 255)); // White
} else if (stripColor == "Yellow strip") {
rgbLed.setPixelColor(0, rgbLed.Color(255, 255, 0)); // Yellow
} else {
rgbLed.setPixelColor(0, rgbLed.Color(255, 0, 0)); // Red or any other color
}
rgbLed.show();
// Wait for the correct time to release the button
waitForReleaseTime(stripColor);
}
void waitForReleaseTime(String stripColor) {
while (true) {
unsigned long timeRemaining = mod1RemainingTime % 10000; // Assume mod1RemainingTime is in milliseconds
if (stripColor == "Blue strip" && String(timeRemaining).indexOf('4') != -1) {
break;
} else if ((stripColor == "White strip" || stripColor == "Any other color strip") && String(timeRemaining).indexOf('1') != -1) {
break;
} else if (stripColor == "Yellow strip" && String(timeRemaining).indexOf('5') != -1) {
break;
}
}
// Release the button
rgbLed.setPixelColor(0, rgbLed.Color(0, 0, 0)); // Turn off RGB LED
rgbLed.show();
}
//##########################################################################################################################################################################################################################
//### Loop
//##########################################################################################################################################################################################################################
void loop() {
//MODULE 4 - BIG BUTTON
//#############################################################################################################
// Check button state
if (digitalRead(BUTTON_PIN) == LOW) {
delay(20); // Debounce delay
if (digitalRead(BUTTON_PIN) == LOW) {
handleButtonPress();
}
}
}