Go Down

Topic: scoreboard 2 digits x 2 players (Read 320 times) previous topic - next topic

Nanni1972

Hi All,
I would like to build a scoreboard 0-99 points for 2 players. Points going up/down using 2 buttons each player.
I am using a Nano board connected to a 4 x 7 segments display. 


I've build a scoreboard for only 1 player (2 digits) and works fine. 


The code use a seveg.h library and the circuit doesn't use any driver such MAX7219 or similar. Arduino is connected directly to 7seg display like this example: http://www.arduinotutorialonline.com/2018/08/how-to-make-arduino-push-button-counter.html




I need some help to modify the code to implement 2 more buttons and 2 more digits for second player.

Can be possible?

Thank you for any help

TheMemberFormerlyKnownAsAWOL

Any good reason you didn't post the code you want help with?

Paul__B

Any good reason you didn't post the details of the 4 x 7 segment display and the schematic of how you have wired it?

Nanni1972

Any good reason you didn't post the code you want help with?
the code is on the attachment file

TheMemberFormerlyKnownAsAWOL

the code is on the attachment file
That's not a good reason.
I can't open attachments on my phone.

Paul__B

#5
Sep 21, 2019, 11:53 pm Last Edit: Sep 21, 2019, 11:54 pm by Paul__B
OK, you have answered the questions in a fashion, even though you have clearly not read the instructions for posting questions.

Here is your code:
Code: [Select]
#include <SevSeg.h>

//Create an instance of the object.
SevSeg myDisplay;

//Create global variables
unsigned long timer;
int deciSecond = 0;
 bool bPress = false;
const int IncbuttonPin = 4;
const int DecbuttonPin = 5;

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses

int IncbuttonState = 0;         // current state of the button
int lastIncbuttonState = 0;     // previous state of the button

int DecbuttonState = 0;         // current state of the button
int lastDecbuttonState = 0;     // previous state of the button

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
  int displayType = COMMON_CATHODE; //Your display is either common cathode or common anode

   pinMode( IncbuttonPin ,INPUT_PULLUP);
   pinMode( DecbuttonPin ,INPUT_PULLUP);
  /*
  //This pinout is for a regular display
   //Declare what pins are connected to the digits
   int digit1 = 2; //Pin 12 on my 2 digit display
   int digit2 = 3; //Pin 9 on my 2 digit display
   int digit3 = 4; //Pin 8 on my 2 digit display
   int digit4 = 5; //Pin 6 on my 2 digit display
  
   //Declare what pins are connected to the segments
   int segA = 6; //Pin 11 on my 2 digit display
   int segB = 7; //Pin 7 on my 2 digit display
   int segC = 8; //Pin 4 on my 2 digit display
   int segD = 9; //Pin 2 on my 2 digit display
   int segE = 10; //Pin 1 on my 2 digit display
   int segF = 11; //Pin 10 on my 2 digit display
   int segG = 12; //Pin 5 on my 2 digit display
   int segDP= 13; //Pin 3 on my 2 digit display
   */

  //This pinout is for OpenSegment PCB layout
  //Declare what pins are connected to the digits
  int digit1 = 3; //Pin 7 on my 2 digit display
  int digit2 = 2; //Pin 8 on my 2 digit display
  int digit3 = 0;
  int digit4 = 0;

  //Declare what pins are connected to the segments
  int segA = 6; //Pin 10 on my 2 digit display
  int segB = 7; //Pin 9 on my 2 digit display
  int segC = 8; //Pin 1 on my 2 digit display
  int segD = 9; //Pin 4 on my 2 digit display
  int segE = 10; //Pin 3 on my 2 digit display
  int segF = 11; //Pin 6 on my 2 digit display
  int segG = 12; //Pin 5 on my 2 digit display
  int segDP= 13; //Pin 2 on my 2 digit display

  int numberOfDigits = 2; //Do you have a 1, 2 or 4 digit display?

  myDisplay.Begin(displayType, numberOfDigits, digit1, digit2, digit3, digit4, segA, segB, segC, segD, segE, segF, segG, segDP);
  
  myDisplay.SetBrightness(100); //Set the display to 100% brightness level

  timer = millis();
}

void loop() {
  // put your main code here, to run repeatedly:
IncbuttonState = digitalRead(IncbuttonPin);
  DecbuttonState = digitalRead(DecbuttonPin);

    checkIncButtonPress();
   checkDecButtonPress();
  //Example ways of displaying a decimal number
  char tempString[10]; //Used for sprintf
  sprintf(tempString, "%2d", buttonPushCounter); //Convert deciSecond into a string that is right adjusted
  //sprintf(tempString, "%d", deciSecond); //Convert deciSecond into a string that is left adjusted
  //sprintf(tempString, "%04d", deciSecond); //Convert deciSecond into a string with leading zeros
  //sprintf(tempString, "%4d", deciSecond * -1); //Shows a negative sign infront of right adjusted number
  //sprintf(tempString, "%4X", deciSecond); //Count in HEX, right adjusted

  
  //Produce an output on the display
  myDisplay.DisplayString(tempString, 0); //(numberToDisplay, decimal point location)

  //Other examples
  //myDisplay.DisplayString(tempString, 0); //Display string, no decimal point
  //myDisplay.DisplayString("-23b", 3); //Display string, decimal point in third position

  

  delay(5);
}

 void checkIncButtonPress()
{
   // compare the IncbuttonState to its previous state
  if (IncbuttonState != lastIncbuttonState) {
    // if the state has changed, increment the counter
    if (IncbuttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      bPress = true;
      buttonPushCounter++;
      if( buttonPushCounter > 99) buttonPushCounter =0 ;
      Serial.println("on");
      
    } 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
  lastIncbuttonState = IncbuttonState;

}
void checkDecButtonPress()
{
   // compare the IncbuttonState to its previous state
  if (DecbuttonState != lastDecbuttonState) {
    // if the state has changed, increment the counter
    if (DecbuttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      bPress = true;
      buttonPushCounter--;
      if( buttonPushCounter < 0) buttonPushCounter =0 ;
      Serial.println("on");
      
    } 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
  lastDecbuttonState = DecbuttonState;

}  

PaulRB

#6
Sep 22, 2019, 09:43 am Last Edit: Sep 22, 2019, 10:08 am by PaulRB
Here is your link.

The circuit is not very good. The LEDs will be quite dim because of the 1K resistors. Also the brightness will change depending on how many segments are lit. So "1" will be much brighter than "8" for example.

To make the circuit better you need 8 resistors, perhaps 220R, connected to each segment. This will make the display much brighter and it will be the same brightness for any digit. But then the current will be too high for the Arduino pins connected to the common pins, so you need a transistor for each digit, and another resistor for each transistor.

Go Up