/*
Arduino | coding-help
Help for create a dart scorer with 7 segment digits and esp32
aledre% — 4/12/24 at 9:30 AM
Hi, I'm very new in coding, but I have (someone gift me)
some 7 segment digits... with a lot o googling, I connect
3 digit to esp32 and I can print numbers on.
Now I need help for coding it for use as a dart scorer...
so start from 501 and with keyboard input score sustract to 0
for win a game... can anyone help me?
In a real circuit you must use resistors on the segments,
and a transistor on each digit.
https://speedyhiredarts.com/darts-rules
*/
const int START_VAL = 501;
const int INTERVAL = 1000;
const int MAX_DIGITS = 3;
const int NUM_CHARS = 8;
const int DATA_PIN = 33;
const int LATCH_PIN = 25;
const int CLOCK_PIN = 26;
const int DIGIT_PINS[MAX_DIGITS] = {12, 14, 27};
const int SEGMENTS[] = { // segments a b c d e f g dp
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110, // 9
B00000000 // Blank
};
boolean newData = false;
char receivedChars[NUM_CHARS]; // an array to store the received data
int scoreVal = START_VAL;
int oldScore = 0;
//unsigned long prevMillis = 0;
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= NUM_CHARS) {
ndx = NUM_CHARS - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
int showNewData() {
int dataNumber = 0;
if (newData == true) {
if (receivedChars[0] == 'r') {
scoreVal = START_VAL;
} else {
dataNumber = atoi(receivedChars);
Serial.print("Points entered: ");
Serial.println(dataNumber);
}
newData = false;
}
return dataNumber;
}
// for hardware test
void testDisplay() {
writeShiftDigit(0, 1);
writeShiftDigit(1, 2);
writeShiftDigit(2, 3);
}
void updateDisplay(int scoreVal) {
// with leading zero blanking
int displayValH = scoreVal < 100 ? 10 : (scoreVal % 1000) / 100; // hundreds
int displayValD = scoreVal < 10 ? 10 : (scoreVal % 100) / 10; // tens
int displayValU = scoreVal % 10; // units
// write values to display
writeShiftDigit(0, displayValH);
writeShiftDigit(1, displayValD);
writeShiftDigit(2, displayValU);
}
void writeShiftDigit(int digit, int number) {
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, SEGMENTS[number]);
digitalWrite(LATCH_PIN, HIGH);
digitalWrite(DIGIT_PINS[digit], LOW);
digitalWrite(DIGIT_PINS[digit], HIGH);
}
void setup() {
Serial.begin(9600);
pinMode(DATA_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
for (int i = 0; i < MAX_DIGITS; i++) {
pinMode(DIGIT_PINS[i], OUTPUT);
digitalWrite(DIGIT_PINS[i], HIGH);
}
Serial.println("\nDart Score - ready");
}
void loop() {
delay(10); // speeds up wokwi sim
//testDisplay();
recvWithEndMarker();
int pointVal = showNewData();
scoreVal = scoreVal - pointVal;
if (scoreVal < 0) {
Serial.println("Bust!");
oldScore = scoreVal;
scoreVal = scoreVal + pointVal;
}
if (scoreVal != oldScore) {
oldScore = scoreVal;
Serial.print("Current score: ");
Serial.println(scoreVal);
if (scoreVal == 0) {
Serial.println("WIN!");
Serial.println("Type 'r' to restart.");
}
}
updateDisplay(scoreVal);
}