LED pong with built-in cheat codes!

LED Pong and more adventures with the Arduino Nano 33 IoT

After finishing up constellations and logic probes, our next stop on the Object journey was to work with our microcontroller of choice for this class- the Arduino Nano 33 IoT. This version comes pre-soldered so prototyping with breadboards was a breeze. And well, that’s exactly what we did.

Our first task at hand was to implement two LEDs with appropriate resistors, as well as two buttons with pull-down resistors. For this task I chose white and blue LEDs. As these have voltage drops higher than 3.3V (our supply voltage), a resistor was technically not necessary. However, I added a 100Ω resistor somewhat by mistake (as I had been LED to believe I would need one), but in the end it turned out to work. It turns out I had dimmed the brightness and got much less power through the LED by adding it. Attached is my calculation-

As you can see by where the arrows landed in the chart, I could have not used a resistor and gotten much more brightness out of my LEDs. Honestly, though the LEDs turned on bright enough anyways. Which leads us to the building of the circuit.

We had a choice of switches to use, either momentary or SPDT, but I decided to use two momentary pushbuttons as honestly they looked quite cool. I got both sets of resistors, and plugged in my microcontroller. I assembled a single button and activated a LED without the microcontroller as a ground test for my circuit. Not really much else to say here — it worked. You can see the circuit and the very bright LED working in the third picture.

Welcome to the jungle… of jumper wires. But hey! It works

Afterwards I got to work on attaching the second button, LED, and set of resistors. It was pretty tough to fit it all on a half-sized breadboard. The jumper wires also got pretty messy- that’s my bad. Sometimes you can get carried away! My inspiration for the interaction portion came from police lights. I had basic interactions for pressing either button (either turning one or both LEDs on), but if you pressed both… well I guess I’ll just show you —

Coding this was rather trivial as well. I simply used the Arduino IDE (although I later switched to VS Code’s Arduino dev environment) and came up with a simple if/else chain to check for the different button states. For the Police effect, I simply switched one LED on, the other off, and then put a small delay before alternating the on/off states. Once I completed the code and breadboard circuit, I created a schematic in EasyEDA Designer- just before the class and I switched to using Fritzing for the rest of the project. You can check out the full code and schematic in my linked gist below —

After we completed our first breadboard circuit, we moved on to using protoboards. The task at hand was to simply create two protoboards with solid core wires that could plug back into our breadboard and Arduino. The first protoboard involved isolating the button circuit (power, ground, digital input, and pull-down resistor) on the protoboard. This would allow us to easily reuse the button as a reusable component in our future projects.

For this I challenged myself to package the button on the protoboard as tightly as possible- using as little of the board as I could. Making this board was a bit of a challenge as the soldering iron was not cooperating and creating the solder traces, especially the longer ones, proved more difficult than anticipated. But after more time than I would have liked to have spent, the button was done and tested perfectly swapped into the previous circuit.

The LED board had one less wire required, which helped a bit. I ran into some issues with solder accidentally creating connections where I didn’t want any, particularly creating a short to ground via bypassing a resistor. I had to use the solder extractor to remove this unwanted solder trace, but afterwards it was smooth sailing. I then attached both protoboards to the breadboard, and using the code from the previous section, I recorded a demo and updated my schematic- now in Fritzing, which was a bit less streamlined than EasyEDA (really wish “R” could rotate pieces!), but still very nice to work in.

And finally the final section, and probably what you all have been waiting for- LED Pong! The last requirement we had was to create an interaction using a WS2812 (aka NeoPixel) LED strip and two modes of input. For the input choice, I used the protoboard button module. As a secondary input, I added an SPDT slide switch. I like games, so I thought of a game that I could make on a single LED strip. Turns out- a single strip really limits your options- especially when it only has 5 LEDs itself. So I decided to give myself some leeway and added a second strip to the design. My plan was to create a watered down version of Pong where you’d play against a computer and use the button to switch LED strips. A ball (represented by a blue LED) would bounce back and forth on the length of the strips, randomly switching strips after every bounce. You would win if you held out long enough against the computer, or lose if the ball got past you. To make the game easier, and add an interaction for the SPDT switch, I made it so it runs a cheat code that beats the game for you every time.

There were some hiccups with my initial code which made the interaction a little less smooth than I would have liked. Here is a video of the first stage of the coding —

As you can see it’s a bit unresponsive and not quite as polished as I would have liked- I frequently click the button and the game doesn’t respond. I tracked this down to a coding issue- the timer that runs the the Pong ball moving back and forth is also the one that detects user input. If the user interacts in the gaps between the pong ball, that input is completely dropped. I rewrote the code to add better input detection and a bit more visual flare. Here is the code and final video attached, along with the schematic for building this circuit yourself!

Hope you enjoy! Stay tuned for the next project in 2 weeks time.

Signing out,