/*
Project: RGB Card Game
Description: An RGB LED indicates the dealer.
A number of cards are dealt, starting
with the player clockwise from the dealer.
Creation date: 10/26/23
Author: AnonEngineering
for Bob, Discord | Wokwi | Questions
License: https://en.wikipedia.org/wiki/Beerware
*/
const int NUMBER_OF_CARDS = 12; // must be multiple of 4
const int LED_PINS[] = {12, 2, 3, 11}; // Define the LED pins
const int BTN_PINS[] = {9, 5, 4, 10}; // Define the switch pins
const int RGB_PINS[] = {8, 7, 6}; //red, green, blue pins
const int LED_COLOR[][3] = {
{1, 0, 0}, // red
{0, 0, 1}, // blue
{1, 1, 0}, // yellow
{0, 1, 0} // green
};
int currentDealer = 0; // Initialize the current dealer
int currentPlayer = 0; // Initialize the current player
void lightLED(int ledIndex) {
digitalWrite(LED_PINS[ledIndex], HIGH); // Turn on the LED
}
void turnOffLED(int ledIndex) {
digitalWrite(LED_PINS[ledIndex], LOW); // Turn off the LED
}
void waitForButtonPress(int switchIndex) {
while (digitalRead(BTN_PINS[switchIndex]) == HIGH) {
// Wait for the button to be pressed
}
while (digitalRead(BTN_PINS[switchIndex]) == LOW) {
// Wait for the button to be released
}
}
void setup() {
Serial.begin(9600);
for (int i = 0; i <= 3; i++) {
pinMode(LED_PINS[i], OUTPUT); // Set LED pins as output
pinMode(BTN_PINS[i], INPUT_PULLUP); // Set switch pins as input with pull-up resistors
}
for (int i = 0; i <= 2; i++) {
pinMode (RGB_PINS[i], OUTPUT);
}
// initialize
digitalWrite(RGB_PINS[0], HIGH);
currentPlayer = currentDealer + 1;
Serial.print("Current dealer: ");
Serial.println(currentDealer + 1);
}
void loop() {
for (int dealer = 0; dealer < 4; dealer++) {
for (int cards = 0; cards < NUMBER_OF_CARDS; cards++) {
lightLED(currentPlayer);
waitForButtonPress(currentPlayer);
turnOffLED(currentPlayer);
currentPlayer++;
if (currentPlayer == 4) currentPlayer = 0;
}
currentDealer++;
if (currentDealer == 4) currentDealer = 0;
for (int i = 0; i < 3; i++) {
digitalWrite(RGB_PINS[i], LOW);
digitalWrite(RGB_PINS[i], LED_COLOR[currentDealer][i]);
}
Serial.print("Current dealer: ");
Serial.println(currentDealer + 1);
currentPlayer = currentDealer + 1;
if (currentPlayer == 4) currentPlayer = 0;
}
}