Make a simple Game Scoreboard with 7 Segment Displays



7 Segment Display

A 7 segment display is basically just a couple of  regular LEDs behind a stencil. Each led lights up a particular segment


MAX7219

MAX7219 is LED driver available it's probably the easiest to use and can control many LEDs using only one chip




schematic diagram

This schematic diagram  connect some capacitors to filter out the noise from the power supply,but this is optional ,You can built it without




Arduino Sketch

#include "LedControl.h"  // Library used for communcation with 7 segment

LedControl lc=LedControl(12,11,10,1);  //  (DIN, CLK, LOAD, number of Max7219 chips)

// Variable to hold current scores
int playerOne=0;
int playerTwo=0;

// Variables to split whole number into single digits
int rightdigit;
int leftdigit;

// Switches pin connection to Arduino UNO
#define switchOne 2
#define switchTwo 3

bool bPress = false;

int p1buttonState = 0;         // current state of the button
int lastp1buttonState = 0;     // previous state of the button

int p2buttonState = 0;         // current state of the button
int lastp2buttonState = 0;     // previous state of the button

void setup() {
  
  pinMode(switchOne,INPUT_PULLUP);
  pinMode(switchTwo,INPUT_PULLUP);
  
  lc.shutdown(0,false);  // Wake up MAX7219

  lc.setIntensity(0,7);  // Set brightness to medium

  lc.clearDisplay(0);  // Clear all displays connected to MAX7219 chip #

// Put zeros on both displays at startup
  
  lc.setDigit(0,0,0,false);  // (Max7219 chip #, Digit, value, DP on or off)
  lc.setDigit(0,1,0,false);
  
  lc.setDigit(0,2,0,false);
  lc.setDigit(0,3,0,false);

}


void loop() { 

   p1buttonState = digitalRead(switchOne);
   p2buttonState = digitalRead(switchTwo);

  // If switch 1 is clicked
  if (p1ButtonPress()) {
    
    playerOne++;  // Increase score by 1
  
    // convert whole number to single digits
    rightdigit=playerOne%10;
    leftdigit=playerOne%100/10;

    // Display extracted digits on the display
    lc.setDigit(0,0,rightdigit,true);
    lc.setDigit(0,1,leftdigit,false);

    
   
    delay(5);  // Small delay to debounce the switch
  }

    if (p2ButtonPress()) {
      
    playerTwo++;
  
    rightdigit=playerTwo%10;
    leftdigit=playerTwo%100/10;

    lc.setDigit(0,2,rightdigit,true);
    lc.setDigit(0,3,leftdigit,false);

   
    delay(5);
  }
}

bool p1ButtonPress()
{
   bool isPress = false;
   // compare the p1buttonState to its previous state
  if (p1buttonState != lastp1buttonState) {
    // if the state has changed, increment the counter
    if (p1buttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      bPress = true;
      isPress = true;
      Serial.println("Plaer One score");
      
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastp1buttonState = p1buttonState;
  return isPress;
}
bool p2ButtonPress()
{
   bool isPress = false;
   // compare the p1buttonState to its previous state
  if (p2buttonState != lastp2buttonState) {
    // if the state has changed, increment the counter
    if (p2buttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      bPress = true;
      isPress = true;
      Serial.println("Plaer Two score");
      
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastp2buttonState = p2buttonState;
  return isPress;
}  

ความคิดเห็น