How to Code a Arduino for a Air Hockey Scoreboard

Discussion in 'Embedded Systems and Microcontrollers' started by fh9717da, Apr 9, 2019.

  1. fh9717da

    Thread Starter New Member

    Apr 9, 2019
    6
    0
    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.
     
  2. Raymond Genovese

    Well-Known Member

    Mar 5, 2016
    1,643
    990
    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.
     
  3. danadak

    Distinguished Member

    Mar 10, 2018
    3,417
    768
  4. shteii01

    AAC Fanatic!

    Feb 19, 2010
    4,667
    745
  5. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    5,376
    2,030
    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!
     
  6. Travm

    Member

    Aug 16, 2016
    261
    22
    Curious, if you only need two scores up to seven, why 4 7-segment displays and not just two?
     
    djsfantasi likes this.
  7. fh9717da

    Thread Starter New Member

    Apr 9, 2019
    6
    0
    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.


    [​IMG]
     
  8. fh9717da

    Thread Starter New Member

    Apr 9, 2019
    6
    0
    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.


    [​IMG]
     
  9. fh9717da

    Thread Starter New Member

    Apr 9, 2019
    6
    0
  10. Travm

    Member

    Aug 16, 2016
    261
    22
    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.
     
  11. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    5,376
    2,030
    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
     
  12. danadak

    Distinguished Member

    Mar 10, 2018
    3,417
    768
    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: Apr 11, 2019
  13. fh9717da

    Thread Starter New Member

    Apr 9, 2019
    6
    0
    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
    }
    }
     
  14. Raymond Genovese

    Well-Known Member

    Mar 5, 2016
    1,643
    990
    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.

    Code (C):
    1.  
    2. /*My First attempt to write Arduino code for the BSU Air Hockey Table Scoreboard
    3. * This is a simple test to try to get one set of scoreboards to
    4. * work with the Adafruit sensor.
    5.  
    6. Arduino pin 6 -> CLK (Green on the 6-pin cable)
    7. 5 -> LAT (Blue)
    8. 7 -> SER on the IN side (Yellow)
    9. 5V -> 5V (Orange)
    10. Power Arduino with 12V and connect to Vin -> 12V (Red)
    11. GND -> GND (Black)
    12. Each display will use about 150mA with all segments and decimal point on.
    13. */
    14.  
    15. //GPIO declarations
    16. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    17. byte segmentClock = 6;
    18. byte segmentLatch = 5;
    19. byte segmentData = 7;
    20.  
    21. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    22.  
    23. #define SENSORPIN 4
    24.  
    25. // variables will change:
    26. int sensorState = 0, lastState=0; // variable for reading
    27.  
    28. void setup()
    29. {
    30. Serial.begin(9600);
    31. Serial.println("Scoreboard Display");
    32. //initialize the scoreboard as an output:
    33.  
    34. pinMode(segmentClock, OUTPUT);
    35. pinMode(segmentData, OUTPUT);
    36. pinMode(segmentLatch, OUTPUT);
    37.  
    38. // initialize the sensor pin as an input:
    39. pinMode(SENSORPIN, INPUT);
    40. digitalWrite(SENSORPIN, LOW); // ir sensor is align not broken
    41. digitalWrite(segmentClock, LOW);
    42. digitalWrite(segmentData, LOW);
    43. digitalWrite(segmentLatch, LOW);
    44.  
    45. int x = 0;
    46. while(1)
    47. {
    48. if(x == 9)
    49. postNumber(x, true); //Show decimal
    50. else
    51. postNumber(x, false);
    52.  
    53. digitalWrite(segmentLatch, LOW);
    54. digitalWrite(segmentLatch, HIGH); //Register moves storage register on the rising edge of RCK
    55. delay(500);
    56.  
    57. x++;
    58. x %= 10; //Reset x after 9
    59.  
    60. Serial.println(x); //For debugging
    61. }
    62. }
    63. void loop()
    64. { // read the state of the pushbutton value:
    65. sensorState = digitalRead(SENSORPIN);
    66.  
    67. // check if the sensor beam is broken
    68. // if it is, the sensorState is HIGH:
    69. if (sensorState == HIGH) {
    70. // to switch scoreboard:
    71. digitalWrite(segmentLatch, HIGH); //Register moves storage register on the rising edge of RCK
    72. }
    73. else {
    74. digitalWrite(segmentLatch, LOW);
    75. }
    76. }
    77. //Given a number, or '-', shifts it out to the display
    78. void postNumber(byte number, boolean decimal)
    79. {
    80. // - A
    81. // / / F/B
    82. // - G
    83. // / / E/C
    84. // -. D/DP
    85.  
    86. #define a 1<<0
    87. #define b 1<<6
    88. #define c 1<<5
    89. #define d 1<<4
    90. #define e 1<<3
    91. #define f 1<<1
    92. #define g 1<<2
    93. #define dp 1<<7
    94.  
    95. byte segments;
    96.  
    97. switch (number)
    98. {
    99. case 1: segments = b | c; break;
    100. case 2: segments = a | b | d | e | g; break;
    101. case 3: segments = a | b | c | d | g; break;
    102. case 4: segments = f | g | b | c; break;
    103. case 5: segments = a | f | g | c | d; break;
    104. case 6: segments = a | f | g | e | c | d; break;
    105. case 7: segments = a | b | c; break;
    106. case 8: segments = a | b | c | d | e | f | g; break;
    107. case 9: segments = a | b | c | d | f | g; break;
    108. case 0: segments = a | b | c | d | e | f; break;
    109. case ' ': segments = 0; break;
    110. case 'c': segments = g | e | d; break;
    111. case '-': segments = g; break;
    112. }
    113.  
    114. if (decimal) segments |= dp;
    115.  
    116. //Clock these bits out to the drivers
    117. for (byte x = 0 ; x < 8 ; x++)
    118. {
    119. digitalWrite(segmentClock, LOW);
    120. digitalWrite(segmentData, segments & 1 << (7 - x));
    121. digitalWrite(segmentClock, HIGH); //Data transfers to the register on the rising edge of SRCK
    122. }
    123. }
    124.  
     
  15. fh9717da

    Thread Starter New Member

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

    Code (C):
    1.  
    2. /*My First attempt to write Arduino code for the BSU Air Hockey Table Scoreboard
    3. * This is a simple test to try to get one set of scoreboards to
    4. * work with the Adafruit sensor.
    5.  
    6. Arduino pin 6 -> CLK (Green on the 6-pin cable)
    7. 5 -> LAT (Blue)
    8. 7 -> SER on the IN side (Yellow)
    9. 5V -> 5V (Orange)
    10. Power Arduino with 12V and connect to Vin -> 12V (Red)
    11. GND -> GND (Black)
    12. Each display will use about 150mA with all segments and decimal point on.
    13. */
    14.  
    15. //GPIO declarations
    16. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    17. byte segmentClock = 6;
    18. byte segmentLatch = 5;
    19. byte segmentData = 7;
    20.  
    21. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    22.  
    23. #define SENSORPIN 4
    24.  
    25. // variables will change:
    26. int sensorState = 0, lastState=0; // variable for reading
    27.  
    28. void setup()
    29. {
    30. Serial.begin(9600);
    31. Serial.println("Scoreboard Display");
    32. //initialize the scoreboard as an output:
    33.  
    34. pinMode(segmentClock, OUTPUT);
    35. pinMode(segmentData, OUTPUT);
    36. pinMode(segmentLatch, OUTPUT);
    37.  
    38. // initialize the sensor pin as an input:
    39. pinMode(SENSORPIN, INPUT);
    40. digitalWrite(SENSORPIN, LOW); // ir sensor is align not broken
    41. digitalWrite(segmentClock, LOW);
    42. digitalWrite(segmentData, LOW);
    43. digitalWrite(segmentLatch, LOW);
    44.  
    45. int x = 0;
    46. while(1)
    47. {
    48. if(x == 9)
    49. postNumber(x, true); //Show decimal
    50. else
    51. postNumber(x, false);
    52.  
    53. digitalWrite(segmentLatch, LOW);
    54. digitalWrite(segmentLatch, HIGH); //Register moves storage register on the rising edge of RCK
    55. delay(500);
    56.  
    57. x++;
    58. x %= 10; //Reset x after 9
    59.  
    60. Serial.println(x); //For debugging
    61. }
    62. }
    63. void loop()
    64. { // read the state of the pushbutton value:
    65. sensorState = digitalRead(SENSORPIN);
    66.  
    67. // check if the sensor beam is broken
    68. // if it is, the sensorState is HIGH:
    69. if (sensorState == HIGH) {
    70. // to switch scoreboard:
    71. digitalWrite(segmentLatch, HIGH); //Register moves storage register on the rising edge of RCK
    72. }
    73. else {
    74. digitalWrite(segmentLatch, LOW);
    75. }
    76. }
    77. //Given a number, or '-', shifts it out to the display
    78. void postNumber(byte number, boolean decimal)
    79. {
    80. // - A
    81. // / / F/B
    82. // - G
    83. // / / E/C
    84. // -. D/DP
    85.  
    86. #define a 1<<0
    87. #define b 1<<6
    88. #define c 1<<5
    89. #define d 1<<4
    90. #define e 1<<3
    91. #define f 1<<1
    92. #define g 1<<2
    93. #define dp 1<<7
    94.  
    95. byte segments;
    96.  
    97. switch (number)
    98. {
    99. case 1: segments = b | c; break;
    100. case 2: segments = a | b | d | e | g; break;
    101. case 3: segments = a | b | c | d | g; break;
    102. case 4: segments = f | g | b | c; break;
    103. case 5: segments = a | f | g | c | d; break;
    104. case 6: segments = a | f | g | e | c | d; break;
    105. case 7: segments = a | b | c; break;
    106. case 8: segments = a | b | c | d | e | f | g; break;
    107. case 9: segments = a | b | c | d | f | g; break;
    108. case 0: segments = a | b | c | d | e | f; break;
    109. case ' ': segments = 0; break;
    110. case 'c': segments = g | e | d; break;
    111. case '-': segments = g; break;
    112. }
    113.  
    114. if (decimal) segments |= dp;
    115.  
    116. //Clock these bits out to the drivers
    117. for (byte x = 0 ; x < 8 ; x++)
    118. {
    119. digitalWrite(segmentClock, LOW);
    120. digitalWrite(segmentData, segments & 1 << (7 - x));
    121. digitalWrite(segmentClock, HIGH); //Data transfers to the register on the rising edge of SRCK
    122. }
    123. }
    124.  
     
  16. Raymond Genovese

    Well-Known Member

    Mar 5, 2016
    1,643
    990
    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.
     
    Travm likes this.
Loading...