Help for Newbie. LED lights for game board

Posted on
  • I have a project circuit I want to build. This comes from an old (1950's) horse derby game I want to build for home.

    Like a pinball machine, the ball is shot up and falls into one of 3 lanes each has rollover switches. One lane has one switch, one has two switches and one has three rollover switches.

    Every time the ball rolls over the switch, a wallboard with a horse advances one, two or the lights/spaces. There are 24 lights. This continues until the lights reach the end and a winner is declared.

    How do I make LED lights advance across the wallboard? I can't find any info about circuits to explain how to turn the lights on and leave them on until the game ends. After two years of searching, I'm asking for help. Thanks in advance. Mike


    2 Attachments

    • Horse derby.jpg
    • Horse-derby2.jpg
  • Ignore this. See the first comment. Thanks

  • Very cool...

    Use Neopixels - see WS2811/WS2812/WS2812B/APA104/APA106/SK68­12 LED String/Strip. The cool thing about them is that they are daisy chained and you feed them a string of on and offs - like light and dark beads on a string. As long as the feeding goes on, pixel n passes the information onto the next pixel n + 1, but keeps and acts on the information that it ends up on: lit or not lit. Furthermore, neopixels consist actually of three LEDs - red, green, blue (RGB) - which requires to feed 3 bytes per pixel defining the intensity of each of the base colors resulting as many different color as you want - out of 256 x 256 x 256. Since you want a balanced / equal brightness, it is a bit less, but still a great number.

    Esprunio has everything you need to drive a Neopixel string. Lay it in a zig-zag pattern onto the board. To make it simpler, use 25 pixles per lane, and light the first in each lane at start time (horse in start position).

    1. In 1st row, positions are pixels 1..25 w/ pixel 1 on at start
    2. in 2nd row, positions are pixels 50..26 w/ pixel 25 on at start
    3. in 3rd row, positions are pixels 51..75 w/ pixel 49 on at start
    4. in 4th row, positions are pixels 100..76 w/ pixel 76 on at start

    Let's go to some coding. Note that it is not optimized to point out the data structure.
    Also note that in code we start to count from 0, such as 1st lane is lane 0, etc.:

    // ---- some declarations to make it flexible (we will start with much smaller values)
    var maxPos = 25; // ...if a horse reaches - or overshoots - this position, it has won
    var maxLanes = 19; // ...took this from your board in the picture
    // --- initialization of data buffers
    var lanePositions = new Uint8ClampedArray(maxLanes); // knows each lane's position
    var npx = new Uint8ClampedArray(maxLanes*maxPos*3); // NeoPixeL buffer 3 bytes / neopixel
    
    // --- initialize board
    function boardInit() {
      for (var lane=0;lane<maxLanes;lane++) lanePositions[lane]=0; // init w/ start positions
      for (var pdx=0;pdx<maxLanes*maxPos*3;pdx++) npx[pdx]=0; // make all pixels dark
      for (lane=0;lane<maxLanes;lane++) { var basePos = lane*maxPos;
        if (lane%2 === 0) { // positions incrementing
          pdx =  basePos*3; // 0|0|0, 2|50|150,... lane|basePos|pixel index
        } else { // positions decrementing from adjusted basePos
          pdx = (basePos + maxPos + maxPos - 1)*3; // 1|49|147, 3|99|297,...
        }
        // set pixel w/ random color - each line will have its own random color
        npx[pdx  ] = Math.random()*255;
        npx[pdx+1] = Math.random()*255;
        npx[pdx+3] = Math.random()*255;
      }
    }
    

    When you now send the npx buffer to the string as described in WS2811/WS2812/WS2812B/APA104/APA106/SK68­12 LED String/Strip, all horses show in start position - first LED in each lane is on.

    Nineteen (19) lanes w/ twentyfive (25) positions is a lot of hardware... you may start out with much smaller number of lanes and positions to validate the concepts and the software implementation: take three (3) lanes with five (5) positions only... which requires 15 neopixel LEDs or a string with at least 15 neopixels.

    For moving a horse in a lane by 1...3 positions and detecting passing the finish line, we can go like this in the code:

    // advances horse in lane by positions
    // and returns true when at/passed finish line, else false
    function advance(lane,positions) {
      var oldPos = lanePos[lane];
      var oldBasePos = lane*maxPos + oldPos;
      var newPos = oldPos += positions; 
      if (newPos >= maxPos) newPos = maxPos - 1;
      lanePos[lane] = newPos;
      var newBasePos = lane*maxPos + newPos;
      var oldPdx, newPdx;
      if (lane%2 === 0) {
        oldPdx = oldBasePos*3;
        newPdx = newBasePos*3;
      } else {
        oldPdx = (oldBasePos + maxPos - 2*oldPos - 1)*3;
        newPdx = (newBasePos + maxPos - 2*newPos - 1)*3;
      }
      npx[newPdx  ] = npx[oldPdx  ]; npx[oldPdx  ] = 0;
      npx[newPdx+1] = npx[oldPdx+1]; npx[oldPdx+1] = 0;
      npx[newPdx+2] = npx[oldPdx+2]; npx[oldPdx+2] = 0;
      return (newPos >= maxPos - 1);
    }
    

    After every advancement, the buffer is again sent to the Neopixl string, and when advancement returned true, player of lane has won and game is over...

    After you have this working, you may go over the top and also light the fields before the lanes and above the lanes... it just adds/inserts corrective terms into the buffer... Using a second, independent string for those things though keeps it as simple as above.

  • What are you using to program the lights? Arduino? PC? I wouldn't know where to begin right off but I have programmed my Arduino and my Raspberry Pi 3B+. Not saying I'm really good, far from it but my sensor worked, collected and stored the data with a time stamp. Very limited but I can find help.

  • @user96449 and @user96445,

    this forum is a JavaScript oriented forum. If you know a bit Web Programming / Web Page development w/ JavaScript included, Espruino is great for you, because it makes IoT and access to micro controller technology a breeze.

    There are Espruino boards available - but Espruino runs also on other boards. Functionality and support on Espruino boards though is superior.

    @user96445, if this is one of your first entries into this world, give Espruino a shot... You are up in seconds - no need to download or install anything - and roundtripping is excellent - no compiling needed...

  • Cool. Didn't know that. I'm just trying to build a game I used to play at the carnival with my mom when I was a kid. I don't want to be a programmer but I'm ok with learning something new if its not complicated. Never tried JAVA.

  • Yes, it would be a lot of hardware. I was thinking of building a couple then if it works I can add more. Right now it would be fun to race my grandkids (13 total) on like 6 tracks at home. I'm assuming each player/horse is a complete circuit independent if another, right?

  • @user96449

    Java is a great language and Java SE/EE has a huge amount of class libraries / packages to tackle all kinds of problems... more on general purpose computers though. In the case at hand, we are talking microcontroller and where - before Espruino/Javascript or MicroPython was not much else available than C/C++. Lot of low-level stuff - even the JavaScript Interpreter Espruino is written in C/C++. C/C++ and Java are for sure not the easy part... and pain does not promise much fun either... That is the reason the @Gordon, the creator of Espruino, motivated to put JavaScript interpreter on a micro controller, so that it is even easier than Arduino - which is really geared towards C/C++.

    @user96449, to get your game build going, you need not much hardware, either a pinned Espruino PICO, Espruino Wifi or an Espruino Pixl, a Neopixel string, a USB power bank to run disconnected (from battery), 3 LDR, 3 white LEDs, 4 100 Ohm resistors, a push button, some wires, and a bread board. That's it. The LDRs - light dependent resistors - white LEDS and resistors you use to sense which exit path the (pin) ball took - 1, 2 or 3. The push button you use to start the game (and move the game forward - from player to player / lane to lane). The rest is all software... the core pieces to control the display board are already provided above.

    The Espruino forum and main sites provide you with all kinds of examples showing how to use the different components, write code for them - if even needed, let the components talk to each other, etc.

    each player/horse is a complete circuit independent of another

    ...not really... because that would be too much for too little. One circuit can handle all the things. The software makes it possible to think of the lanes/players/horses as independent... at least for some time, but the first reaching the finish line defines 'Game Over' for everyone else.

    What we did not talk about is keep scores over more than one game... if you go for a Pixl, you even have a display to show the score, 4 push buttons to setup the game - how many players, how many positions, start the game, control the game flow, do score management, and- last but not least - give instructions what's next in the game...

  • Wow, you make it sound so easy. I was told an Arduino nano or uno was the way to go. If I'm reading this right, the Espruino is better designed for what I'm doing. Can it run 10 players at one time instead of one Espruino for each player? I was looking at the code and it seems like that is what is happening.

  • @user96449,

    that's right. maxLanes is the number of players that can play at the same time...

    Now there is the case that not all lanes may be occupied... the game control flow would take care of that when passing turns. The initialization needs to be adjusted as well in line 12 of first code block: The turning on of the first position would only loop thru the lines with a player and leave the remaining unset.

  • Wow, you make it sound so easy. I was told an Arduino nano or uno was the way to go. If I'm reading this right, the Espruino is better designed for what I'm doing. Can it run 10 players at one time instead of one Espruino for each player? I was looking at the code and it seems like that is what is happening.

  • LDR's and LEDs probably won't be fast enough to sense a ball going by them. Opto switches based on phototransistor/photodiode are much more effective. Or just use pinball rollover switches (depends on how how much of the hardware is "pre-made" for you....)

    The display on the scoreboard for each "lane" need not be separate. I'd just use one string of neopixels for the whole thing. Specifically, my first attempt would be to get one of those strings of 50 WS2811 pixels (the ones that look kinda like strings of christmas lights), using the appropriate number of LEDs for each lane. Unlike the "strip", you can move the bulbs around to position them the way you want (it's unclear to me whether you have an existing score-board for a derby game - if you do, you could tuck these lights where the old bulbs went)....

    Espruino should be able to handle a large number of players, and probably a bit more naturally than an Arduino would. Both of them would be effective for your task though.

    Arduino vs Espruino:

    Espruino has faster developent time (JS is easier to write than C, especially in terms of writing sane, readable code that makes sense), especially if you're interfacing with the web, where the painful parts of C become more prominent. The fact that you get a js console that you can execute code on "live" during development is indescribably useful.

    On Arduino, "String" is the devil - strings that you can easily concatenate with + exist, but the usage patterns they lead to result in weird memory problems; the alternative is c-strings (null terminated character arrays), and operations on them are, shall we say, less than intuitive - on Espruino, strings are a great data type and are work the way you expect them to without caveats.

    Espruino code execution is slow - even though the processor is several times faster than an arduino, the overhead of the interpreter often makes it slower (though it's worth noting that where you can send the Espruino a bunch of data to work on at once, it's fast).

    Arduino is cheaper. Code execution is very fast. RAM is very limited, and you need to think much more carefully about how you use resources. C doesn't have a native data type like js objects, and it really hurts if you're coming from a modern language where you're used to that.

    My rule of thumb is that if if i'm interacting with the internet (including calls to webservers on a local network), or if the application is difficult to realize without dynamically sized arrays use Espruino. If I'm working at a low level, or have very tightly timing critical things, if the task is very well defined, predictable, and amenable to fixed size arrays, use Arduino.

    Many of my larger projects have both of them - for example, I have an Arduino sketch that sends and receives data using those el-cheapo 433mhz transmitter/receivers, and interfaces via serial, and have that talk to an Espruino to act as a Serial <-> RF gateway. In a current project I also have an Arduino (well, arduino-programmed attiny4313) converting serial to the curious parallel interface used for the futaba VFD I'm using - but those serial commands are sent by the Espruino, which provides the web interface, the menu, and all that jazz.

  • @DrAzzy, thanks for chiming in...

    For me, all things where you use some Arduino like implementation in your projects are satellites to the Espruino, and you use these satellites to handle extreme time sensitive things, such as with the 433mHz RF connection to handle the individual bits and communicate with Espruino after the work is done on Byte or even String level. That's where I would put the focus on why to use Arduino. One could do these things too on Espruino, but it would have to be part of the firmware - in C/C++ - or at least compiled JS.

    Yeah, LDRs are slow... the question is how long the ball stays in there. Sensing mechanical things and creating an electrical signal varies greatly. With steel balls, hall sensor and permanent magnet would work as well. Even plain metal contacts - like two whiskers - can be used because the setWatch provides debounce technology. Whiskers are then much simpler to handle.

    I dug up my supplementary stuff from Espruino PICO Kickstarter reward which includes Neopixel LEDs and I'm about to post about the prototype of the play board... so stay tuned...

  • Or a microswitch with the bent-wire on the actuator arm, available as spare parts for pinball machines would work too.

  • Whiskers seems to be very easy to build just need some metal wire.


    1 Attachment

    • whiskercloseup.jpg
  • Cool multi-purpose whiskers. Lot of hardware... I prefer shifting 'materialization' from concrete to abstract - hardware to software - muscles to brain - wires to code...

    Btw, a prototype of the horse derby race board has materialized in this conversation about Retro Horse Derby Game Board with Neopixels showing Reaction Game Status

    The code is configurable to any number of lanes and positions by changing a few values...

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Help for Newbie. LED lights for game board

Posted by Avatar for user96445 @user96445

Actions