Hello!
I have just bought 8-digit 7-segment module and I would like to use it to display the score of game. So how to connect it to arduino and what is the simplest code to do that?
Hello!
I have just bought 8-digit 7-segment module and I would like to use it to display the score of game. So how to connect it to arduino and what is the simplest code to do that?
What code I should use?
Give us a link to the display you purchased.
Is this to display 1 or 2 players/teams?
What does the score increment by?
Game would be some kind pinball, where targets work like pushbuttons; when ball hits target, current flows trough circuit and arduino reads it with digitalRead function and adds certain score to total points and 8 digit 7 segment display total score.
Here is an example you can start with:
//An example showing how to increment/decrement an 8 digit, seven segment, display.
//LarryD
//
//Version
//1.00 working code May 13, 2017
//
#include <SPI.h>
/*
Arduino pins MAX7219 pins
SS 10 LOAD 12
MOSI 11 DIN 1
MISO 12 No Connection
SCK 13 CLK 13
*/
const byte incSwitch = 2; //increment switch, pushed = LOW
byte lastIncSwitchState;
const byte decSwitch = 3; //decrement switch, pushed = LOW
byte lastDecSwitchState;
byte dp = 0;
unsigned long timer1;
unsigned long currentMillis;
unsigned long SwitchMillis;
unsigned long debounceMillis = 100UL;
unsigned long num = 00000000ul;
//unsigned long num = 99999990ul; //Use this for testing over flow condition
unsigned long nbr = 99999999ul; //(8 digits) largest number before overflow
//unsigned long nbr = 9999999ul; //(7 digits) largest number before overflow
//7219 registers Register addresses
const byte MAX7219_REG_NOOP = 0x0;
// codes 1 to 8 are the digit positions
const byte MAX7219_REG_DECODEMODE = 0x9;
const byte MAX7219_REG_INTENSITY = 0xA;
const byte MAX7219_REG_SCANLIMIT = 0xB;
const byte MAX7219_REG_SHUTDOWN = 0xC;
const byte MAX7219_REG_DISPLAYTEST = 0xF;
//**********************************************************************
void setup ()
{
pinMode (10, OUTPUT); //set up SS pin 10
digitalWrite(10, HIGH); //disable the 7219
SPI.begin ();
clear7219();
pinMode(incSwitch, INPUT_PULLUP);
pinMode(decSwitch, INPUT_PULLUP);
SwitchMillis = millis();
} // End of setup()
//**********************************************************************
void loop ()
{
//save the current time
currentMillis = millis();
// //*****************************************
// //example of automatic counting
// if (micros() - timer1 >= 10000)
// {
// timer1 = micros();
// num++;
// displayNumber(num); //display the new number
// }
//*****************************************
if (currentMillis - SwitchMillis >= debounceMillis)
{
//code here runs every debounceMillis ms
//get ready for the next iteration
SwitchMillis = millis();
//go and check the switches
checkSwitches();
}
//*****************************************
}// End of loop()
//**********************************************************************
// FUNCTIONS
//**********************************************************************
//switches are checked every debounceValue milli seconds
//no minimum switch press time is validated with this code (i.e. No glitch filter)
void checkSwitches()
{
//re-usable for all the switches
boolean thisState;
//*****************************************
//check if the increment switch has changed state
thisState = digitalRead(incSwitch);
if (thisState != lastIncSwitchState)
{
//update the switch state
lastIncSwitchState = thisState;
//this switch position has changed so do some stuff
//"HIGH condition code"
//switch went from LOW to HIGH
if (thisState == HIGH)
{
//Do some HIGH switch stuff here
}
//"LOW condition code"
//switch went from HIGH to LOW
else
{
num++;
displayNumber(num); //display the new number
}
} //END of incSwitch code
//*****************************************
//check if the decrement switch has changed state
thisState = digitalRead(decSwitch);
if (thisState != lastDecSwitchState)
{
//update the switch state
lastDecSwitchState = thisState;
//this switch position has changed so do some stuff
//"HIGH condition code"
//switch went from LOW to HIGH
if (thisState == HIGH)
{
//Do some HIGH switch stuff here
}
//"LOW condition code"
//switch went from HIGH to LOW
else
{
if (num != 0)
{
num--;
}
displayNumber(num); //display the new number
}
} //END of decSwitch code
} // END of checkSwitches()
//**********************************************************************
// function to display up to eight digits on a 7-segment display
void displayNumber( unsigned long myNumber)
{
unsigned long number = myNumber;
for (byte digit = 1; digit < 9; digit++)
{
byte character = number % 10; // get the value of the rightmost decade
//replace leadings 0s with blanks
if (number == 0 && digit > 1)
{
character = 0xF; //value needed to blank a 7221 digit
}
//turn on the decimal point to act as a comma if number is > 999 or >999999
if ((digit == 7 && myNumber > 999999) || (digit == 4 && myNumber > 999))
{
//add a dp
character |= 0b10000000;
}
//if we have overflowed the maximum count turn on all the dp to indicate this
if (myNumber >= nbr)
{
// this adds the dp at overflow
character |= 0b10000000;
}
sendByte (9 - digit, character);
number = number / 10; //remove the LSD for the next iteration
}
}// End of displayNumber
//**********************************************************************
// Clear the bubble display
void clear7219()
{
//SPI.setBitOrder(MSBFIRST); // MAX7219 requires most significant bit first
sendByte (MAX7219_REG_SCANLIMIT, 7); // show 8 digits zero relative
sendByte (MAX7219_REG_DECODEMODE, 0xFF); // in the MSD use bit patterns not digits
sendByte (MAX7219_REG_DISPLAYTEST, 0); // no display test
sendByte (MAX7219_REG_INTENSITY, 0x9); // character intensity: range: 0 to 15
sendByte (MAX7219_REG_SHUTDOWN, 1); // not in shutdown mode (ie. start it up)
//num = 0UL; // clear the digits. Leading 0s will be blanked.
displayNumber(num);
}// End of clear7219()
//**********************************************************************
//configure the 7219
void configure (const char mode)
{
//SPI.setBitOrder(MSBFIRST); // MAX7219 requires most significant bit first
sendByte (MAX7219_REG_SCANLIMIT, 7); // show 8 digits zero relative
// Change next line to 0xFF if you want all positions to display a number
sendByte (MAX7219_REG_DECODEMODE, 0xFF); // all digits
sendByte (MAX7219_REG_DISPLAYTEST, 0); // no display test
sendByte (MAX7219_REG_INTENSITY, 0xA); // character intensity: range: 0 to 15
sendByte (MAX7219_REG_SHUTDOWN, 1); // not in shutdown mode (ie. start it up)
}// End of configure()
//**********************************************************************
//write a byte to the 7219 register in question
void sendByte (const byte reg, const byte data)
{
digitalWrite (SS, LOW); //enable 7219
SPI.transfer (reg); //select the 7219 register
SPI.transfer (data); //send the data for this register
digitalWrite (SS, HIGH); //disable 7219
}// End of sendbyte()
//**********************************************************************
// END OF PROGRAM
//**********************************************************************
Edit:
Updated code