Components and supplies
10kOhm potentiometer
Jumper wires (generic)
16x2 LCD
Breadboard 100x70
Push Button
Carte Arduino Uno R3
Tools and machines
LiquidCrystal.h Library
Apps and platforms
Arduino IDE 2.0 (beta)
Project description
Code
scoreboard
c
have fun!
1/* 2 * **DuoScore: A Digital Scoreboard with LCD Display for Two-Player Games** 3 * 4 * This project implements a versatile digital scoreboard using an LCD display, designed to track scores for any two-player game. 5 * 6 * **Features:** 7 * - **Player Switching**: Allows toggling between two players with a dedicated button. 8 * - **Point Tracking**: Adds points to the active player with a separate button. 9 * - **Reset Functionality**: Resets the game with a visually engaging animation. 10 * - **Loading Animation**: Displays a loading animation upon initialization to enhance the user experience. 11 * 12 * **Code Overview:** 13 * - **Setup**: Initializes the LCD display and shows a welcome animation. 14 * - **Loop**: Handles player switching, point addition, and game reset operations. 15 * - **Display Updates**: Continuously updates the LCD with the current scores and active player. 16 * - **Game Initialization**: Features a loading animation to signal the start of the game. 17 * 18 * This code is suitable for enhancing any two-player game with a digital scoring system and is a great example of combining hardware and software for interactive projects. 19 */ 20 21#include <LiquidCrystal.h> 22 23// Initialize the LCD with the respective pins 24LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 25 26// Variables to store the points for both players 27int ptsP1 = 0, ptsP2 = 0; 28 29// Pin for the button to add points and its last state 30const int buttonAddPts = 7; 31int lastButtonAddPtsState = LOW; 32 33// Pin for the button to switch players and its last state 34const int buttonPlayer = 8; 35int lastButtonPlayerState = LOW; 36 37// Pin for the reset button 38const int buttonReset = 9; 39 40// Variable to track the current player (0 for player 1, 1 for player 2) 41int player = 0; 42 43// Declare the game initialization function 44void gameInit(void); 45 46void setup() { 47 // Initialize the LCD 48 lcd.begin(16, 2); 49 delay(100); 50 // Display the welcome animation 51 gameInit(); 52} 53 54void loop() { 55 // Player switching logic 56 int buttonPlayerState = digitalRead(buttonPlayer); 57 if (buttonPlayerState != lastButtonPlayerState) { 58 if (buttonPlayerState == HIGH) { 59 // Toggle between players (switch between 0 and 1) 60 player = (player == 0) ? 1 : 0; 61 } 62 // Update the button state to avoid repetitive toggling 63 lastButtonPlayerState = buttonPlayerState; 64 } 65 66 // Adding points logic 67 int buttonAddPtsState = digitalRead(buttonAddPts); 68 if (buttonAddPtsState != lastButtonAddPtsState) { 69 if (buttonAddPtsState == HIGH) { 70 // Add points to the active player 71 if (player == 0) { 72 ptsP1 += 1; 73 } else { 74 ptsP2 += 1; 75 } 76 } 77 // Update the button state to avoid repetitive additions 78 lastButtonAddPtsState = buttonAddPtsState; 79 } 80 81 // Reset logic with animation 82 if (digitalRead(buttonReset) == HIGH) { 83 // Reset animation 84 int j = 0; 85 for (int i = 0; i < 16; i++) { 86 lcd.clear(); 87 // Alternate between lines 0 and 1 to display "NEW GAME" 88 if (i % 2 == 0) { 89 j = 0; 90 } else { 91 j = 1; 92 } 93 lcd.setCursor(i, j); 94 lcd.print("NEW GAME"); 95 delay(160); 96 } 97 98 // Reset variables after the animation 99 player = 0; 100 ptsP1 = 0; 101 ptsP2 = 0; 102 103 // Re-run the welcome animation 104 gameInit(); 105 106 // Wait for the user to release the reset button 107 while (digitalRead(buttonReset) == HIGH) { 108 delay(10); 109 } 110 } 111 112 // Displaying scores 113 lcd.clear(); 114 lcd.setCursor(0, 0); 115 lcd.print("PLAYER 1 - "); 116 lcd.print(ptsP1); 117 118 // Indicator for the active player (a "#" is shown next to the active player) 119 if (player == 0) { 120 lcd.setCursor(15, 0); 121 lcd.print("#"); 122 lcd.setCursor(15, 1); 123 lcd.print(" "); 124 } else { 125 lcd.setCursor(15, 0); 126 lcd.print(" "); 127 lcd.setCursor(15, 1); 128 lcd.print("#"); 129 } 130 131 lcd.setCursor(0, 1); 132 lcd.print("PLAYER 2 - "); 133 lcd.print(ptsP2); 134 135 // Small delay to stabilize the display 136 delay(10); 137} 138 139// Game initialization function with a loading screen animation 140void gameInit(void) { 141 lcd.clear(); 142 lcd.setCursor(3, 0); 143 lcd.print("SCOREBOARD"); 144 145 // Loading animation with dots on the bottom line 146 for (int i = 0; i <= 15; i++) { 147 lcd.setCursor(i, 1); 148 lcd.print("."); 149 delay(120); // Delay to adjust the speed of the animation 150 lcd.setCursor(i, 1); 151 lcd.print(" "); 152 } 153 154 // Small pause before clearing the screen 155 delay(200); 156 lcd.clear(); 157} 158 159/* 160 * **Wiring Diagram Description:** 161 * 162 * **LCD Display:** 163 * - VSS (Pin 1) to Arduino GND 164 * - VDD (Pin 2) to Arduino 5V 165 * - V0 (Pin 3) to a 10k potentiometer (center pin for contrast adjustment) 166 * - RS (Pin 4) to Arduino pin 12 167 * - RW (Pin 5) to Arduino GND 168 * - EN (Pin 6) to Arduino pin 11 169 * - D0-D3 (Pins 7-10) not used (set to GND or left unconnected) 170 * - D4 (Pin 11) to Arduino pin 5 171 * - D5 (Pin 12) to Arduino pin 4 172 * - D6 (Pin 13) to Arduino pin 3 173 * - D7 (Pin 14) to Arduino pin 2 174 * - A (Pin 15) to Arduino 5V (for backlight) 175 * - K (Pin 16) to Arduino GND (for backlight) 176 * 177 * **Buttons:** 178 * - Button to Add Points: 179 * - One side to Arduino pin 7 180 * - Other side to Arduino GND (with a pull-down resistor if needed) 181 * - Button to Switch Players: 182 * - One side to Arduino pin 8 183 * - Other side to Arduino GND (with a pull-down resistor if needed) 184 * - Button to Reset Game: 185 * - One side to Arduino pin 9 186 * - Other side to Arduino GND (with a pull-down resistor if needed) 187 * 188 * **Additional Components:** 189 * - Ensure all components are connected securely and verify connections before powering up the Arduino. 190 */
Downloadable files
scoreboard.ino
scoreboard.ino
Comments
Only logged in users can leave comments