Electronic Scoreboard With Arduino

by jjosemayorga in Circuits > Arduino

516 Views, 0 Favorites, 0 Comments

Electronic Scoreboard With Arduino

Miniatura.png

In this Instructable I'll show you how to build a very simple electronic scoreboard with Arduino.

This is a non-rigurous English translation for an article in my personal website: https://jjosemayorga.com/construyendo-un-pequeno-marcador-electronico/


You can find this and many other interesting projects in my website and my social media.

🌐 https://jjosemayorga.com/

Twitter ► https://twitter.com/jjosemayorga

Instagram ► https://www.instagram.com/jjosemayorga/

Facebook ► https://www.facebook.com/jjosemayorgau

Youtube ► https://www.youtube.com/@jjosemayorga

TikTok ► https://www.tiktok.com/@jjosemayorga

Supplies

IMG20220918123209.jpg

Wire the Components All Together

display7s_bb.png

In order to make the whole setup work, you just need to wire all the components in the way I show you in this picture.

If you need further explanation on how each element works or why I use them that way, you just need to visit the article on my website and get some instruction on it (it's in Spanish but, if you are not fluent in that language, Google translator will do the job 😉).

Load the Arduino Code

As for the Arduino part, I provide you the Arduino code I've used.

It's a pretty simple of the use of serial communication. Should you need to learn the very basics of this, let me address you again to the article on my website.

Downloads

Build the Java App

7-segment-led.png
seg0.gif
seg1.gif
seg2.gif
seg3.gif
seg4.gif
seg5.gif
seg6.gif
seg7.gif
seg8.gif
seg9.gif

Here you will find the Java code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent; 
import gnu.io.SerialPortEventListener; 
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Enumeration;

public class display7s implements SerialPortEventListener 
{
JFrame marco=new JFrame("Display de 7 segmentos");
JLabel numeroactual=new JLabel("<html><font size=5>Número actual:</font></size>");
JTextField numact=new JTextField("0", 20);
JPanel digitos=new JPanel();
JLabel decima=new JLabel(new ImageIcon("seg0.gif"));
JLabel uniima=new JLabel(new ImageIcon("seg0.gif"));
int unidades=0;
int decenas=0;
int numerorecibido=0;
JLabel enviarnumero=new JLabel("<html><font size=5>Enviar número:</font></size>");
JTextField envnum=new JTextField(20);
JButton enviar=new JButton("<html><font size=5>ENVIAR</font></size>");
int numeroenviado=0;

/** The output stream to the port */
private OutputStream output = null;
private BufferedReader input;
SerialPort serialPort;
private final String PORT_NAME = "COM9";
/** Milliseconds to block while waiting for port open */
private static final int TIME_OUT = 2000;
/** Default bits per second for COM port. */
private static final int DATA_RATE = 9600;

display7s()
{
marco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
marco.setIconImage(Toolkit.getDefaultToolkit().createImage("7-segment-led.png"));
marco.setResizable(false);
GridBagLayout gridbag=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
marco.setLayout(gridbag);
gbc.insets=new Insets(5,20,5,20);
gbc.gridx=0;
gbc.gridy=0;
gbc.gridwidth=2;
gbc.gridheight=1;
marco.add(numeroactual,gbc);
gbc.gridx=0;
gbc.gridy=1;
gbc.gridwidth=2;
gbc.gridheight=1;
numact.setEditable(false);
numact.setBackground(Color.white);
marco.add(numact,gbc);
digitos.setLayout(new GridLayout(1,2));
digitos.add(decima);
digitos.add(uniima);
gbc.gridx=0;
gbc.gridy=2;
gbc.gridwidth=2;
gbc.gridheight=1;
marco.add(digitos,gbc);
gbc.gridx=0;
gbc.gridy=3;
gbc.gridwidth=2;
gbc.gridheight=1;
gbc.anchor=GridBagConstraints.CENTER;
marco.add(enviarnumero,gbc);
gbc.gridx=0;
gbc.gridy=4;
gbc.gridwidth=2;
gbc.gridheight=1;
marco.add(envnum,gbc);
gbc.gridx=0;
gbc.gridy=5;
gbc.gridwidth=2;
gbc.gridheight=1;
enviar.setName("enviar");
enviar.setToolTipText("Enviar número");
enviar.addMouseListener(new Seleccion());
marco.add(enviar,gbc);
initialize();
marco.pack();
marco.setLocationRelativeTo(null);
marco.setVisible(true);
}

public void initialize() 
{
 
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
 
// iterate through, looking for the port
while (portEnum.hasMoreElements()) 
{
CommPortIdentifier currPortId = (CommPortIdentifier)
portEnum.nextElement();
 
if (PORT_NAME.equals(currPortId.getName())) 
{
portId = currPortId;
break;
}
}
if (portId == null) 
{
System.out.println("Could not find COM port.");
return;
}

try 
{
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);

// set port parameters
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);

// open the streams
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
output = serialPort.getOutputStream();

// add event listeners
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);

catch (Exception e) 
{
System.err.println(e.toString());
}
}

public synchronized void close() 
{
if (serialPort != null) 
{
serialPort.removeEventListener();
serialPort.close();
}
}

/**
* Handle an event on the serial port. Read the data and print it.
*/
public synchronized void serialEvent(SerialPortEvent oEvent) 
{
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) 
{
try 
{
String inputLine=input.readLine();
//System.out.println(inputLine);
numact.setText(inputLine);
numerorecibido=Integer.parseInt(inputLine);
decenas=(int)Math.floor(numerorecibido/10);
unidades=numerorecibido-(decenas*10);
numgraf(decenas,unidades);
//lect=Integer.parseInt(inputLine);
//slider.setValue(lect);
} catch (Exception e) {
System.err.println(e.toString());
}
}
}

private void sendData(String data)
{
 
try 
{
output.write(data.getBytes());
} catch (IOException e) {
showError("Error sending data");
//System.exit(ERROR);
}
}
private void showError(String errorMessage){
JOptionPane.showMessageDialog(marco,
errorMessage,
  "Error",
  JOptionPane.ERROR_MESSAGE);
}

class Seleccion extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
Component aux=e.getComponent();
}
public void mouseReleased(MouseEvent e)
{
Component aux=e.getComponent();
{
if(aux.getName().equals("enviar"))
{
try
{
numeroenviado=Integer.parseInt(envnum.getText());
if(numeroenviado>99||numeroenviado<0)
{
JOptionPane.showMessageDialog(new JFrame(), "El número enviado debe estar comprendido entre 0 y 99", "Error en datos de entrada", JOptionPane.ERROR_MESSAGE);
}
else
{
sendData(envnum.getText());
}
}
catch(NumberFormatException f)
{
JOptionPane.showMessageDialog(new JFrame(), "Debe introducir un número entero", "Error en datos de entrada", JOptionPane.ERROR_MESSAGE);
}
}
}
}
}

void numgraf(int dtir, int utir)
{
switch(dtir)
{
case 0:
decima.setIcon(new ImageIcon("seg0.gif"));
break;
case 1:
decima.setIcon(new ImageIcon("seg1.gif"));
break;
case 2:
decima.setIcon(new ImageIcon("seg2.gif"));
break;
case 3:
decima.setIcon(new ImageIcon("seg3.gif"));
break;
case 4:
decima.setIcon(new ImageIcon("seg4.gif"));
break;
case 5:
decima.setIcon(new ImageIcon("seg5.gif"));
break;
case 6:
decima.setIcon(new ImageIcon("seg6.gif"));
break;
case 7:
decima.setIcon(new ImageIcon("seg7.gif"));
break;
case 8:
decima.setIcon(new ImageIcon("seg8.gif"));
break;
case 9:
decima.setIcon(new ImageIcon("seg9.gif"));
break;
}
switch(utir)
{
case 0:
uniima.setIcon(new ImageIcon("seg0.gif"));
break;
case 1:
uniima.setIcon(new ImageIcon("seg1.gif"));
break;
case 2:
uniima.setIcon(new ImageIcon("seg2.gif"));
break;
case 3:
uniima.setIcon(new ImageIcon("seg3.gif"));
break;
case 4:
uniima.setIcon(new ImageIcon("seg4.gif"));
break;
case 5:
uniima.setIcon(new ImageIcon("seg5.gif"));
break;
case 6:
uniima.setIcon(new ImageIcon("seg6.gif"));
break;
case 7:
uniima.setIcon(new ImageIcon("seg7.gif"));
break;
case 8:
uniima.setIcon(new ImageIcon("seg8.gif"));
break;
case 9:
uniima.setIcon(new ImageIcon("seg9.gif"));
break;
}
}

public static void main(String[] args)
{
display7s app=new display7s();
}
}

You just need to copy the code into your Java IDE (Eclipse, Netbeans etc.) and build the Java project.

This code needs no frameworks, no libraries etc. It would even work just by compiling it in your CMD. As you will see, the interface is very simple.

I add as well the images required by the app. Place them in the right folder of the project.

Enjoy the Scoreboard

Marcador electr&oacute;nico con displays de 7 segmentos / Electronic scoreboard based on 7-segment displays

Here you have a demo on how it works. I hope you have a good time while working on this.