© 2018 The Programmator. Theme WPBoot by Just Free Themes.
In this sample we are going to create a simple and fun number roulette using 4 seven segment displays and an Arduino Uno! — Code available on GitHub.
A “seven segment display” is named this way simply because one digit display consists of seven LED’s that can be powered independently. Without going into much further detail, it is important to know that there are two types of displays; a “common anode”, and a “common cathode” display. The common anode displays use a powered common pin, while the common cathode use a common ground pin. The easiest way to create a display with multiple digits using an Arduino, I found, was using the common anode. That way, we can use a digital output pin to target a display. But don’t let the theory scare you off just yet, let’s go ahead and dive right into our project!
Normally, you’d buy one display with all four digits assembled into one component. But for our sample, we’re going to use four separate displays so we can see what’s really happening under the hood. It does require a bit more manual wiring than in the assembled version, where all that is done for us. Just make sure that if you’re following this sample, you’re using common anode displays.
Using the schematics above, connect display pins “a” – “g” to digital output pins 2 – 8, and pin “Vcc” to digital output pin 12.
Now, if we set the Vcc pin to HIGH, current will flow from the Arduino, through the Vcc pin, through all LED’s (segments), and back to the Arduino. This means that if we want a segment to be “off”, we will have to give it a HIGH pin state. Go ahead and run the following sketch.
What number did it display?
Now that we’ve got one display working, how will we show multiple digits? We don’t have enough pins on our Arduino to handle four displays. Or do we?
We do. With a little trickery of the eye, we can very quickly shift between displays using multiple Vcc pins. When the delay between the digits, and between the LED’s is so small, the human eye will see it as a still image. Hook up a second display and place all LED pins in parallel. Only the Vcc pins are going to separate digital output pins (12 and 11). Next, run the following sample code.
Did it work? Great! You now should be seeing the number 33! It didn’t work? Not so great. Leave your feedback below this article and maybe I can help you out.
Of course we don’t want to be showing the same number on both displays, but a number accross the displays. We can do this by selecting the first display, “outputting” the value devided by 10, and in the next digit the remainder, or modulo.
As you can see, I’ve used a bit of pseudo-code there. Adding the whole displaying method would make the code block on this page a bit too long. But feel free to check it out on GitHub.
Now that we have the displaying of multiple numbers in place, it’s easy to port it from 2 to 4 digits.
A key aspect of something being a “roulette”, is that one doesn’t know the outcome on beforehand. Now with an Arduino, that might become a little trickier than expected. The “random” method generates a pseudo-random number out of a fixed random value set. This means that every time you’d run a sketch, the identical random number will be picked by the Arduino! So, in order to shift the random value set in more randomly fashion, we need use the randomSeed method. This method takes a parameter at what index it should start returning the random numbers. We can use the input of a non-connected analog pin as input, as non-connected analog pins generate some sort of false value that fluctuates.
And that’s pretty much it! Do check out the GitHub repository for the entire code files, and don’t forget to leave your feedback about the code, the sample and this article.