//ZZZ-Skee-4 player
// Define segment pins
const int segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; // A, B, C, D, E, F, G, DP
// Define digit pins
//Remember to specify Common Cathode or Common Anode LEDs!!!!!!!!!!!!!!!!!!
const int digitPins[] = {10, 11, 12, 13}; // Digit 1, Digit 2, Digit 3, Digit 4
// Segment patterns for digits 0-9 (common cathode)
const byte digitPatterns[] = {
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111 // 9
};
////////////////////////////////////////////////////
#define NUM_BUTTONS 6 //Scoring switches.
int buttonPins[NUM_BUTTONS] = {A0, A1, A2, A3, A4, A5}; // Analog pins for buttons
//int buttonPins[NUM_BUTTONS] = {2, 3, 4}; // GPIO pins for buttons
int buttonStates[NUM_BUTTONS];
int lastButtonStates[NUM_BUTTONS];
unsigned long lastDebounceTimes[NUM_BUTTONS];
const int DEBOUNCE_TIME = 50; //50 ms, the time
////////////////////////////////////////////////////
const int buttonPin = 9; //Button to select player.
int buttonState = 0;
int lastButtonState = 0;
int playerNum = 1;
//int digit4Mod;
int P1_total;
int P2_total;
int P3_total;
int P4_total;
void setup() {
// Set segment pins as outputs
for (int i = 0; i < 8; i++) {
pinMode(segmentPins[i], OUTPUT);
}
// Set digit pins as outputs
for (int i = 0; i < 4; i++) {
pinMode(digitPins[i], OUTPUT);
}
Serial.begin(9600);
////////////////////////////////////////////////////////
for (int i = 0; i < NUM_BUTTONS; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
buttonStates[i] = HIGH;
lastButtonStates[i] = HIGH;
lastDebounceTimes[i] = 0;
}
////////////////////////////////////////////////////////
pinMode(buttonPin, INPUT_PULLUP);
P1_total = 0;
P2_total = 0;
P3_total = 0;
P4_total = 0;
playerNum = 1;
}//End Setup()
void loop() {
//Button to select player order.
buttonState = digitalRead(buttonPin);
//if (buttonState == HIGH && lastButtonState == LOW) {
if (buttonState == LOW && lastButtonState == HIGH) {
playerNum++;
if (playerNum > 4) //Counter limit.
{
playerNum = 1;
}
//Serial.println("OK");
// Serial.print("Player: ");
// Serial.println(playerNum);
delay(50); // Debounce delay - 200
}
lastButtonState = buttonState;
//////////////////////////////////////////////////
//Buttons for scoring.
//Button selection and debounce handler
for (int i = 0; i < NUM_BUTTONS; i++) {
int reading = digitalRead(buttonPins[i]);
if (reading != lastButtonStates[i]) {
lastDebounceTimes[i] = millis();
}
if ((millis() - lastDebounceTimes[i]) > DEBOUNCE_TIME) {
if (reading != buttonStates[i]) {
buttonStates[i] = reading;
if (buttonStates[i] == LOW) {
handleButtonPress(i);
if (playerNum == 1){
Serial.print("P1 total score: ");
Serial.println(P1_total);
}
else if (playerNum == 2){
Serial.print("P2 total score: ");
Serial.println(P2_total);
}
else if (playerNum == 3){
Serial.print("P3 total score: ");
Serial.println(P3_total);
}
else if (playerNum == 4){
Serial.print("P4 total score: ");
Serial.println(P4_total);
}
}
}
}
lastButtonStates[i] = reading;
}
//////////////////////////////////////////
//Show number in SevenSegment LED.
if (playerNum == 1) {
displayNumber(P1_total);
} else if (playerNum == 2) {
displayNumber(P2_total);
} else if (playerNum == 3) {
displayNumber(P3_total);
} else if (playerNum == 4) {
displayNumber(P4_total);
} else {
// Code to execute if none of the above conditions are true
}
} //End loop()
void displayNumber(int number) {
//Show player number. Zero-based index 3 (Digit 4).
setSegments(digitPatterns[playerNum]); // Set segments for the digit
digitalWrite(digitPins[3], LOW); // Activate the digit
delay(5); // Short delay for multiplexing
digitalWrite(digitPins[3], HIGH); // Deactivate the digit
//Show score. (Digit 1 to 3).
for (int digit = 0; digit < 3; digit++) {
int digitValue = (number / (int)pow(10, digit)) % 10; // Extract digit
setSegments(digitPatterns[digitValue]); // Set segments for the digit
digitalWrite(digitPins[digit], LOW); // Activate the digit
delay(5); // Short delay for multiplexing
digitalWrite(digitPins[digit], HIGH); // Deactivate the digit
}
}
void setSegments(byte pattern) {
for (int i = 0; i < 8; i++) {
digitalWrite(segmentPins[i], (pattern >> i) & 0x01); // Set each segment
}
}
////////////////////////
//Button selected - code for selected button.
void handleButtonPress(int buttonIndex) {
// Implement logic for each button press
//Use case statement to handle each pushbutton.
switch(buttonIndex) {
case 0:
// code to execute for value1
if (playerNum == 1) {
P1_total = P1_total + 10;
} else if (playerNum == 2) {
P2_total = P2_total + 10;
} else if (playerNum == 3) {
P3_total = P3_total + 10;
} else if (playerNum == 4) {
P4_total = P4_total + 10;
} else {
// Code to execute if none of the above conditions are true
}
//digitalWrite(ledPin, HIGH);
delay(100);
//digitalWrite(ledPin, LOW);
//Serial.println("Button #1 pressed!");
break;
case 1:
// code to execute for value2
if (playerNum == 1) {
P1_total = P1_total + 20;
} else if (playerNum == 2) {
P2_total = P2_total + 20;
} else if (playerNum == 3) {
P3_total = P3_total + 20;
} else if (playerNum == 4) {
P4_total = P4_total + 20;
} else {
// Code to execute if none of the above conditions are true
}
//digitalWrite(ledPin, HIGH);
delay(100);
//digitalWrite(ledPin, LOW);
//Serial.println("Button #2 pressed!");
break;
case 2:
// code to execute for value3
if (playerNum == 1) {
P1_total = P1_total + 30;
} else if (playerNum == 2) {
P2_total = P2_total + 30;
} else if (playerNum == 3) {
P3_total = P3_total + 30;
} else if (playerNum == 4) {
P4_total = P4_total + 30;
} else {
// Code to execute if none of the above conditions are true
}
//digitalWrite(ledPin, HIGH);
delay(100);
//digitalWrite(ledPin, LOW);
//Serial.println("Button #3 pressed!");
break;
case 3:
// code to execute for value4
if (playerNum == 1) {
P1_total = P1_total + 40;
} else if (playerNum == 2) {
P2_total = P2_total + 40;
} else if (playerNum == 3) {
P3_total = P3_total + 40;
} else if (playerNum == 4) {
P4_total = P4_total + 40;
} else {
// Code to execute if none of the above conditions are true
}
//digitalWrite(ledPin, HIGH);
delay(100);
//digitalWrite(ledPin, LOW);
//Serial.println("Button #4 pressed!");
break;
case 4:
// code to execute for value5
if (playerNum == 1) {
P1_total = P1_total + 50;
} else if (playerNum == 2) {
P2_total = P2_total + 50;
} else if (playerNum == 3) {
P3_total = P3_total + 50;
} else if (playerNum == 4) {
P4_total = P4_total + 50;
} else {
// Code to execute if none of the above conditions are true
}
//digitalWrite(ledPin, HIGH);
delay(100);
//digitalWrite(ledPin, LOW);
//Serial.println("Button #5 pressed!");
break;
case 5:
// code to execute for value6
if (playerNum == 1) {
P1_total = P1_total + 100;
} else if (playerNum == 2) {
P2_total = P2_total + 100;
} else if (playerNum == 3) {
P3_total = P3_total + 100;
} else if (playerNum == 4) {
P4_total = P4_total + 100;
} else {
// Code to execute if none of the above conditions are true
}
//digitalWrite(ledPin, HIGH);
delay(100);
//digitalWrite(ledPin, LOW);
//Serial.println("Button #6 pressed!");
break;
default:
// code to execute if no cases match
break;
}
}