How to Code a Arduino for a Air Hockey Scoreboard

Thread Starter

fh9717da

Joined Apr 9, 2019
6
Hello,
I need help making the code for scoreboard that is going on an air hockey table (school project). I am new to the Arduino and have no experience creating code. What I have is 4 7-segmented displays (will have parts no. of each part below)and I need 2 displays to display one score and the other 2 displays to display a different score. In air hockey regulations the first one to 7 wins, I just need these displays to go to 7. Once one set of displays reach 7 can reset to 0. These displays are being controlled by SparkFun Arduino Redboard. I also using Adafruit IR sensors to control what score to display. I have use SparkFun's Large Digit Drive Hookup Guide to get me to at least to display numbers. Link: 'https://learn.sparkfun.com/tutorials/large-digit-driver-hookup-guide

Parts I have:
4- 7-segmented displays- Part NO.: COM-08530 (From SparkFun Website)
4-Large digit driver-Part NO.: WIG-13279 (From SparkFun Website)
1-SparkFun Redboard-Part NO.: DEV-13975 (From SparkFun Website)
2- Adafruit 3mm IR Break Beam Sensors-Part NO.: 2167 (From Adafruit Website)

Any help with setting up the code would be greatly appreciated!

I could post a schematic of the parts, if that would be helpful.
 

Raymond Genovese

Joined Mar 5, 2016
1,653
You can do this.

Yes, a schematic would help, but you are going to have to get a little bit of experience in writing a simple program or two or three first. You will need to get your feet wet. The good news is that the board you have chosen as well as the parts you have all have example code available.

Start here https://learn.sparkfun.com/tutorials/redboard-hookup-guide when you have it hooked up and running (you can write and run "blink"), start sketching out what you want the program to do, step by step.

Lots of people here will help and the more you do or try to do on your own, the more worthwhile it will be.
 

djsfantasi

Joined Apr 11, 2010
9,156
Yes! Excellent selection of parts, by the way.

Since you don’t have coding experience, Raymond's suggestion of trying the sample sketches is an excellent start. After you have them running, try to read them and learn how they work. I suggest you bookmark the Arduino Reference page and look up anything in the sample programs you don’t understand.

You’ll have to download the Arduino IDE to look at the sketches (Arduino-speak for programs) and upload them to the Arduino (which Arduino are you using?). The IDE is free. I’ve provided the link to the Windows version.

Arduino C has a fairly simple structure. The setup() functions initializes the Arduino resources you need (General Purpose IO Pins and any shields you attach for example). The loop() contains the code that implements whatever you’re doing. Note that once your code is done, the loop() function restarts.

Raymond also said that you should start sketching out what you want the code to do. You can start this at any time and polish it while you try out the IDE and sample sketches. Sketching out your ideas this way is referred to pseudo-code. Write out, in English, step-by-step what you want to do. Once it makes sense, then repeat the process adding more detail to each step. When you can’t think of any more detail then translate the English description into Arduino code.

You can post your results in trying out the sample programs and what you think you learned. I (and others) can comment on your experiences which may help you understand better.

The same thing goes for each iteration of your pseudo-code. As well as your sketch as it’s developed.

Most importantly, don’t be discouraged. The members of this forum are pleased to help you.

Good luck in your start to this journey!
 

Thread Starter

fh9717da

Joined Apr 9, 2019
6
One of the requirements is to have an overhead display. On one side it will show your score and the opponents score. The same on the other side. Similar to the image below.


 

Thread Starter

fh9717da

Joined Apr 9, 2019
6
Curious, if you only need two scores up to seven, why 4 7-segment displays and not just two?
One of the requirements is to have an overhead display. On one side it will show your score and the opponents score. The same on the other side. Similar to the image below.


 

Travm

Joined Aug 16, 2016
363
Makes sense.
Air hockey tables from my childhood had one score board mounted on the side.

As a starting point try to put in words what the Arduino has to do to accomplish your task. Break it into small pieces and then take it one piece at a time, then put all your pieces together.
I've never used Arduino but once you start getting specific questions others will chime in with help no doubt.
 

djsfantasi

Joined Apr 11, 2010
9,156
I dislike Ardublock, so can’t help you there. I know other member(s) who like it, so let’s hope they chime in.

While you wait for help with Ardublock (one of the reasons I dislike it*), start sketching out what you want to do. Both Raymond and I have outlined a process you’ll have to follow. Even with Ardublock. So reread our posts and take a shot.

Dj


* there are many more support resources for coding an Arduino sketch. Ardublock? I didn’t stick with it long enough to find out. I WILL say I struggled with Ardublock. With the resources on the Arduino site alone, I had my first operational device in less than an hour without Ardublock. On the other hand, I have been coding since the mid-60’s
 

danadak

Joined Mar 10, 2018
4,057
I think mBlock seems to be better supported, closer to Scratch, bigger following,
more active and current participants. Plus mBlock allows you to control an Arduino
and also bridge that Arduino into Windows which makes it very interesting..

Ardublock was more attractive, at least for a while, because several vendors jumped
it with sensor support. But looking at their site it seems enthusiasm has waned.

I play with both because there are differences in them. Real embedded jobs of course
I do in C.

These languages, IDEs, step you away from actual HW. assembly closest, C next,
then stuff like Forth,. Scratch, mBlock, Ardublock. Always tradeoffs.

mBlock comes in 2 flavors, 3.x and 5.x. Seems like the 3.x has more Arduino
focused extensions.



Regards, Dana.
 
Last edited:

Thread Starter

fh9717da

Joined Apr 9, 2019
6
Below is my code I tried to use. I cannot get the sensors to work. Can someone help me with editting

/*My First attempt to write Arduino code for the BSU Air Hockey Table Scoreboard
* This is a simple test to try to get one set of scoreboards to
* work with the Adafruit sensor.

Arduino pin 6 -> CLK (Green on the 6-pin cable)
5 -> LAT (Blue)
7 -> SER on the IN side (Yellow)
5V -> 5V (Orange)
Power Arduino with 12V and connect to Vin -> 12V (Red)
GND -> GND (Black)
Each display will use about 150mA with all segments and decimal point on.
*/

//GPIO declarations
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
byte segmentClock = 6;
byte segmentLatch = 5;
byte segmentData = 7;

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#define SENSORPIN 4

// variables will change:
int sensorState = 0, lastState=0; // variable for reading

void setup()
{
Serial.begin(9600);
Serial.println("Scoreboard Display");
//initialize the scoreboard as an output:

pinMode(segmentClock, OUTPUT);
pinMode(segmentData, OUTPUT);
pinMode(segmentLatch, OUTPUT);

// initialize the sensor pin as an input:
pinMode(SENSORPIN, INPUT);
digitalWrite(SENSORPIN, LOW); // ir sensor is align not broken
digitalWrite(segmentClock, LOW);
digitalWrite(segmentData, LOW);
digitalWrite(segmentLatch, LOW);

int x = 0;
while(1)
{
if(x == 9)
postNumber(x, true); //Show decimal
else
postNumber(x, false);

digitalWrite(segmentLatch, LOW);
digitalWrite(segmentLatch, HIGH); //Register moves storage register on the rising edge of RCK
delay(500);

x++;
x %= 10; //Reset x after 9

Serial.println(x); //For debugging
}
}
void loop()
{ // read the state of the pushbutton value:
sensorState = digitalRead(SENSORPIN);

// check if the sensor beam is broken
// if it is, the sensorState is HIGH:
if (sensorState == HIGH) {
// to switch scoreboard:
digitalWrite(segmentLatch, HIGH); //Register moves storage register on the rising edge of RCK
}
else {
digitalWrite(segmentLatch, LOW);
}
}
//Given a number, or '-', shifts it out to the display
void postNumber(byte number, boolean decimal)
{
// - A
// / / F/B
// - G
// / / E/C
// -. D/DP

#define a 1<<0
#define b 1<<6
#define c 1<<5
#define d 1<<4
#define e 1<<3
#define f 1<<1
#define g 1<<2
#define dp 1<<7

byte segments;

switch (number)
{
case 1: segments = b | c; break;
case 2: segments = a | b | d | e | g; break;
case 3: segments = a | b | c | d | g; break;
case 4: segments = f | g | b | c; break;
case 5: segments = a | f | g | c | d; break;
case 6: segments = a | f | g | e | c | d; break;
case 7: segments = a | b | c; break;
case 8: segments = a | b | c | d | e | f | g; break;
case 9: segments = a | b | c | d | f | g; break;
case 0: segments = a | b | c | d | e | f; break;
case ' ': segments = 0; break;
case 'c': segments = g | e | d; break;
case '-': segments = g; break;
}

if (decimal) segments |= dp;

//Clock these bits out to the drivers
for (byte x = 0 ; x < 8 ; x++)
{
digitalWrite(segmentClock, LOW);
digitalWrite(segmentData, segments & 1 << (7 - x));
digitalWrite(segmentClock, HIGH); //Data transfers to the register on the rising edge of SRCK
}
}
 
When you post code, do it with "tags' as in...

[ code=c ]

post your code lines here

[ /code ]

I have added a space after each bracket above so that you can see it without it being interpreted as a tag, but don't use those spaces. Try editing your post so that we read the code easier.

C:
/*My First attempt to write Arduino code for the BSU Air Hockey Table Scoreboard
* This is a simple test to try to get one set of scoreboards to
* work with the Adafruit sensor.

Arduino pin 6 -> CLK (Green on the 6-pin cable)
5 -> LAT (Blue)
7 -> SER on the IN side (Yellow)
5V -> 5V (Orange)
Power Arduino with 12V and connect to Vin -> 12V (Red)
GND -> GND (Black)
Each display will use about 150mA with all segments and decimal point on.
*/

//GPIO declarations
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
byte segmentClock = 6;
byte segmentLatch = 5;
byte segmentData = 7;

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#define SENSORPIN 4

// variables will change:
int sensorState = 0, lastState=0; // variable for reading

void setup()
{
Serial.begin(9600);
Serial.println("Scoreboard Display");
//initialize the scoreboard as an output:

pinMode(segmentClock, OUTPUT);
pinMode(segmentData, OUTPUT);
pinMode(segmentLatch, OUTPUT);

// initialize the sensor pin as an input:
pinMode(SENSORPIN, INPUT); 
digitalWrite(SENSORPIN, LOW); // ir sensor is align not broken
digitalWrite(segmentClock, LOW);
digitalWrite(segmentData, LOW);
digitalWrite(segmentLatch, LOW);

int x = 0;
while(1)
{
if(x == 9)
postNumber(x, true); //Show decimal
else
postNumber(x, false);

digitalWrite(segmentLatch, LOW);
digitalWrite(segmentLatch, HIGH); //Register moves storage register on the rising edge of RCK
delay(500);

x++;
x %= 10; //Reset x after 9

Serial.println(x); //For debugging
}
}
void loop()
{ // read the state of the pushbutton value:
sensorState = digitalRead(SENSORPIN);

// check if the sensor beam is broken
// if it is, the sensorState is HIGH:
if (sensorState == HIGH) { 
// to switch scoreboard:
digitalWrite(segmentLatch, HIGH); //Register moves storage register on the rising edge of RCK
}
else {
digitalWrite(segmentLatch, LOW);
}
}
//Given a number, or '-', shifts it out to the display
void postNumber(byte number, boolean decimal)
{
// - A
// / / F/B
// - G
// / / E/C
// -. D/DP

#define a 1<<0
#define b 1<<6
#define c 1<<5
#define d 1<<4
#define e 1<<3
#define f 1<<1
#define g 1<<2
#define dp 1<<7

byte segments;

switch (number)
{
case 1: segments = b | c; break;
case 2: segments = a | b | d | e | g; break;
case 3: segments = a | b | c | d | g; break;
case 4: segments = f | g | b | c; break;
case 5: segments = a | f | g | c | d; break;
case 6: segments = a | f | g | e | c | d; break;
case 7: segments = a | b | c; break;
case 8: segments = a | b | c | d | e | f | g; break;
case 9: segments = a | b | c | d | f | g; break;
case 0: segments = a | b | c | d | e | f; break;
case ' ': segments = 0; break;
case 'c': segments = g | e | d; break;
case '-': segments = g; break;
}

if (decimal) segments |= dp;

//Clock these bits out to the drivers
for (byte x = 0 ; x < 8 ; x++)
{
digitalWrite(segmentClock, LOW);
digitalWrite(segmentData, segments & 1 << (7 - x));
digitalWrite(segmentClock, HIGH); //Data transfers to the register on the rising edge of SRCK
}
}
 

Thread Starter

fh9717da

Joined Apr 9, 2019
6
Below is my code I tried to use. I cannot get the sensors to work. Can someone help me with editting

C:
/*My First attempt to write Arduino code for the BSU Air Hockey Table Scoreboard
* This is a simple test to try to get one set of scoreboards to
* work with the Adafruit sensor.

Arduino pin 6 -> CLK (Green on the 6-pin cable)
5 -> LAT (Blue)
7 -> SER on the IN side (Yellow)
5V -> 5V (Orange)
Power Arduino with 12V and connect to Vin -> 12V (Red)
GND -> GND (Black)
Each display will use about 150mA with all segments and decimal point on.
*/

//GPIO declarations
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
byte segmentClock = 6;
byte segmentLatch = 5;
byte segmentData = 7;

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#define SENSORPIN 4

// variables will change:
int sensorState = 0, lastState=0; // variable for reading

void setup()
{
Serial.begin(9600);
Serial.println("Scoreboard Display");
//initialize the scoreboard as an output:

pinMode(segmentClock, OUTPUT);
pinMode(segmentData, OUTPUT);
pinMode(segmentLatch, OUTPUT);

// initialize the sensor pin as an input:
pinMode(SENSORPIN, INPUT); 
digitalWrite(SENSORPIN, LOW); // ir sensor is align not broken
digitalWrite(segmentClock, LOW);
digitalWrite(segmentData, LOW);
digitalWrite(segmentLatch, LOW);

int x = 0;
while(1)
{
if(x == 9)
postNumber(x, true); //Show decimal
else
postNumber(x, false);

digitalWrite(segmentLatch, LOW);
digitalWrite(segmentLatch, HIGH); //Register moves storage register on the rising edge of RCK
delay(500);

x++;
x %= 10; //Reset x after 9

Serial.println(x); //For debugging
}
}
void loop()
{ // read the state of the pushbutton value:
sensorState = digitalRead(SENSORPIN);

// check if the sensor beam is broken
// if it is, the sensorState is HIGH:
if (sensorState == HIGH) { 
// to switch scoreboard:
digitalWrite(segmentLatch, HIGH); //Register moves storage register on the rising edge of RCK
}
else {
digitalWrite(segmentLatch, LOW);
}
}
//Given a number, or '-', shifts it out to the display
void postNumber(byte number, boolean decimal)
{
// - A
// / / F/B
// - G
// / / E/C
// -. D/DP

#define a 1<<0
#define b 1<<6
#define c 1<<5
#define d 1<<4
#define e 1<<3
#define f 1<<1
#define g 1<<2
#define dp 1<<7

byte segments;

switch (number)
{
case 1: segments = b | c; break;
case 2: segments = a | b | d | e | g; break;
case 3: segments = a | b | c | d | g; break;
case 4: segments = f | g | b | c; break;
case 5: segments = a | f | g | c | d; break;
case 6: segments = a | f | g | e | c | d; break;
case 7: segments = a | b | c; break;
case 8: segments = a | b | c | d | e | f | g; break;
case 9: segments = a | b | c | d | f | g; break;
case 0: segments = a | b | c | d | e | f; break;
case ' ': segments = 0; break;
case 'c': segments = g | e | d; break;
case '-': segments = g; break;
}

if (decimal) segments |= dp;

//Clock these bits out to the drivers
for (byte x = 0 ; x < 8 ; x++)
{
digitalWrite(segmentClock, LOW);
digitalWrite(segmentData, segments & 1 << (7 - x));
digitalWrite(segmentClock, HIGH); //Data transfers to the register on the rising edge of SRCK
}
}
 
I, and many others, will emphasize that you break the task up into small bits and test each of those bits. Further, before coding, it is a good idea to write out what the program should do and then moving coding to the bits.

That process is difficult for a beginner to get around, but I think you should embrace it instead of trying to shoot the whole ball of wax at once.

Edited to add: toward that end, can you list discrete functions for the overall task - breaking down the whole into parts? I know it seems like this slows down everything, but in the end, it actually speeds it up quite a bit.
 
Top