const uint8_t redPin = 10; // Red pin (Anode)
const uint8_t greenPin = 11; // Green pin (Anode)
const uint8_t bluePin = 12; // Blue pin (Anode)
const uint8_t buttonPin = 13; // Button pin
const int ledPins[] = {2, 3, 4, 5}; // Define the LED pins
const int switchPins[] = {6, 7, 8, 9}; // Define the switch pins
int currentLED = 0; // Variable to track the current LED
int currentDealer = 0; // Initialize the current dealer
int currentPlayer = 0; // Initialize the current player
int cardsPlayed = 1; // Initialize the cards played
bool isButtonPressed = false;
bool oldBtnState = false;
int mode = 0;
void checkButton() {
int btnState = digitalRead(buttonPin);
if (btnState != oldBtnState) {
oldBtnState = btnState;
if (btnState == LOW) {
isButtonPressed = true;
Serial.println("Button pressed");
} else {
Serial.println("Button released");
}
delay(500);
}
}
void setup() {
for (int i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT); // Set LED pins as output
pinMode(switchPins[i], INPUT_PULLUP); // Set switch pins as input with pull-up resistors
}
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor
Serial.begin(9600); // Initialize Serial Monitor
}
void loop() {
while (true) { // Outer loop to repeat the entire sequence
checkButton();
if (isButtonPressed) {
mode++;
if (mode > 3) mode = 0;
isButtonPressed = false;
}
// DEALER LOOP
while (currentDealer <= 3) {
// Print current dealer's color
switch (currentDealer) {
case 0:
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
break;
case 1:
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);
break;
case 2:
digitalWrite(redPin, 150);
digitalWrite(greenPin, 150);
digitalWrite(bluePin, LOW);
break;
case 3:
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, LOW);
break;
default:
break;
}
// Set currentPlayer to currentDealer + 1
currentPlayer = currentDealer + 1;
// currentPlayer Loop
while (cardsPlayed <= 12) {
for (int i = 0; i < 2; i++) { // Flash all LEDs 2 times
for (int j = 0; j < 4; j++) {
// Light current player's LED
lightLED(j);
// Wait for current player button input
waitForButtonPress(j);
// Turn off the LED
turnOffLED(j);
// Increment currentPlayer and cardsPlayed
currentPlayer++;
cardsPlayed++;
Serial.println("cardsPlayed: " + String(cardsPlayed)); // Print cardsPlayed to the Serial Monitor
}
}
}
// Increment currentDealer and reset currentPlayer and cardsPlayed
currentDealer++;
currentPlayer = 0;
cardsPlayed = 1;
}
}
}
void lightLED(int ledIndex) {
digitalWrite(ledPins[ledIndex], HIGH); // Turn on the LED
currentLED = ledIndex; // Update the current LED
}
void turnOffLED(int ledIndex) {
digitalWrite(ledPins[ledIndex], LOW); // Turn off the LED
}
void waitForButtonPress(int switchIndex) {
while (digitalRead(switchPins[switchIndex]) == HIGH) {
// Wait for the button to be pressed
}
while (digitalRead(switchPins[switchIndex]) == LOW) {
// Wait for the button to be released
}
}