/*
Arduino | coding-help
I am trying to make a scoreboard using a Arduino
loki — 7/7/24 at 3:11 PM
I am trying to make a scoreboard using a Arduino and
a four digit seven segment display and a 74HC595
I think I have the hardware made correctly.
I just can’t get the code to do anything I want to do.
*/
//#include <Arduino.h>
// Pin definitions, redone just to match a layout I had
const int latchPin = A4;
const int clockPin = A5;
const int dataPin = A3;
const int digitPins[] = {12, 9, 10, 11};
const int buttonPins[] = {7, 6, 5, 4};
// Button states
int buttonState[4] = {0, 0, 0, 0};
int lastButtonState[4] = {0, 0, 0, 0};
unsigned long lastDebounceTime[4] = {0, 0, 0, 0};
const unsigned long debounceDelay = 50;
// Scores
int teamAScore = 0;
int teamBScore = 0;
/*
// Segment data for digits 0-9
const byte segmentMap[] = {
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111 // 9
};
*/
const int SEG_BYTES[] = { // same as your map,
// but in a b c d e f g dp order
0xFC, /* 0 */
0x60, /* 1 */
0xDA, /* 2 */
0xF2, /* 3 */
0x66, /* 4 */
0xB6, /* 5 */
0xBE, /* 6 */
0xE0, /* 7 */
0xFE, /* 8 */
0xF6, /* 9 */
0x00 /* blank */
};
// this combines displayDigit and shiftOutData
// and changes the order of operations
void writeShiftDigit(int digit, int number) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, ~SEG_BYTES[number]);
digitalWrite(latchPin, HIGH);
digitalWrite(digitPins[digit], HIGH);
digitalWrite(digitPins[digit], LOW);
}
/*
// Function to shift out data to the 74HC595
void shiftOutData(byte data) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, data);
digitalWrite(latchPin, HIGH);
}
// Function to display a number on a specific digit
void displayDigit(int digit, int number) {
// Select the digit by turning on the corresponding transistor
for (int i = 0; i < 4; i++) {
//digitalWrite(digitPins[i], (i == digit) ? LOW : HIGH);
digitalWrite(digitPins[i], LOW);
}
// Send the segment data to the 74HC595
shiftOutData(segmentMap[number]);
}
*/
void setup() {
// Set up pins
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
for (int i = 0; i < 4; i++) {
pinMode(digitPins[i], OUTPUT);
digitalWrite(digitPins[i], HIGH);
}
for (int i = 0; i < 4; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
// Read and debounce buttons
for (int i = 0; i < 4; i++) {
int reading = digitalRead(buttonPins[i]);
if (reading != lastButtonState[i]) {
lastDebounceTime[i] = millis();
}
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
if (reading != buttonState[i]) {
buttonState[i] = reading;
if (buttonState[i] == LOW) {
if (i == 0 && teamAScore < 99) teamAScore++;
if (i == 1 && teamAScore > 0) teamAScore--;
if (i == 2 && teamBScore < 99) teamBScore++;
if (i == 3 && teamBScore > 0) teamBScore--;
}
}
}
lastButtonState[i] = reading;
}
// Display scores
//writeShiftDigit(0, 1); // for hardware test
//writeShiftDigit(1, 2); //
//writeShiftDigit(2, 3); //
//writeShiftDigit(3, 4); //
writeShiftDigit(0, teamAScore / 10);
writeShiftDigit(1, teamAScore % 10);
writeShiftDigit(2, teamBScore / 10);
writeShiftDigit(3, teamBScore % 10);
/*
displayDigit(0, teamAScore / 10); // Tens digit for Team A
delay(10); // Increase delay for better stability
displayDigit(1, teamAScore % 10); // Units digit for Team A
delay(10); // Increase delay for better stability
displayDigit(2, teamBScore / 10); // Tens digit for Team B
delay(10); // Increase delay for better stability
displayDigit(3, teamBScore % 10); // Units digit for Team B
delay(10); // Increase delay for better stability
*/
// Debugging output
Serial.print("Team A Score: ");
Serial.print(teamAScore);
Serial.print(" | Team B Score: ");
Serial.println(teamBScore);
}