Slot Machine [C++] [Casey Rodman].avi
Add the following snippet to your HTML: Published August 29, 2018 © ATmega Alien Themed Slot Machine This is a three-reeled slot machine with 25 symbols on each reel.
The game is configurable.
Hardware components ATmega328P-PU, to be precise.
Only three of the four segments are used.
Shop around on aliexpress.
Don't pay for shipping!
One controls spinning the reels, three for navigating the menu, two for grounding pin 1 of the ATmegas.
× 6 16 MHz Crystal One for the SlotMachine's ATmega328P-PU, and one for the LED display slave's ATmega328P-PU.
Both run at 16MHz.
× 2 Buzzer Two are necessary, one for the SlotMachine chip, and one for the display slave chip.
It would be nice to modify the circuit so that only one of these is necessary and can be shared by both micro-controllers.
× 1 Jumper wires generic You'll need a good amount of this.
For the most part I make my own, but i use the jumper wires too.
Not depicted in the schematic.
I was inspired by Cory Potter's and I wanted to expand on that idea.
The slot machine is for entertainment and educational purposes only.
I tried my best to make the game simulate a real slot machine as closely as possible.
The project is currently bread boarded.
An enclosure will be added as soon as the parts arrive from China and I've had a chance to solder everything up.
The project took about two months for me to build in my spare time.
The most difficult part of the build for me was understanding all of the math involved in making the game behave the way the casino industry would expect a simple slot machine it to behave after a half billion or so simulations.
A video of the final product!
How the Game Works The game has three reels with the same unique 25 symbols appearing on each reel one of the 8x8 matrices on the component with 4 8x8 matrices isn't used.
There are five different ways to win.
If you get three spaceships, you win the jackpot.
If you get one or two spaceships you also win some credits.
If you get two or three symbols to match you also win.
This kept the programming a little simpler.
There were plenty of other challenges for me.
Features The slot machine has several interesting features which are accessed via the 20 x 4 I2C capable LCD display using two navigation buttons and a select button.
The buttons use a fairly sophisticated de-bouncing algorithm that takes advantage of the micro-controller's external interrupt capability.
This is the main menu.
Since there are six lines in the menu you have to scroll down using the 'navigate down' button to see the entire menu.
There is a button dedicated to 'spinning' the reels.
In addition to that you can also select 'Play' from the main menu.
You can change your bet at any time.
The most exciting feature is that the game can be played in 'auto' mode; i.
This is a critical function for testing the game.
You can also disable the sound here.
Via the menu on the LCD it's also possible to view all of the metrics generated from the simulation.
These metrics are also output and may be viewed in the serial monitor if you connect your micro-controller to the monitor via the RX and TX pins using a USB cable.
The list of displayed metrics includes your credit balance, the number of times you hit the jackpot, and the number of times you won credits by any other means.
This allowed me to run simulations based on the various payouts, and was useful for establishing and proving out the payout table.
The payout table itself is not configurable; once it is set it should stay the same.
I suppose it would be possible to make the volatility index configurable by using it to drive the payout tables, but that would require a lot more work.
The Reset option allows you to reset all of the metrics except EEprom writesback to zero.
The chip will work for about 100, 000 writes to EEprom.
Since there's 512k of EEprom available on the chip, and we're only using a fraction of that, it would be possible to actually move the location of the metrics in EEprom as we approach 100, 000 writes.
I have not implemented this feature but it would be a means by which to extend the life of the chip.
Finally, the hold, or the percentage of each wager kept by the house over timeis configurable.
Remember that after performing a Reset operation the hold needs to be set again.
The Math A lot of work went into making sure that the game was realistic.
The probabilities were calculated and the payout table was designed so that the game has an acceptable Volatility Index VI.
This index measures how predictable the machine's behavior is.
A c code for slot machine with a higher VI is more likely to make the player or the house more money.
It is less predictable than a machine with a lower VI.
It's true that the same exact game will exist in different casinos or even the same casino with different VIs.
The VI is changed by manipulating the payout schedule.
For our game, here are the probabilities and payouts for each kind of win.
Note that the odds far right and the payout far left are dramatically different.
If this game was programmed so that the payout table matched or closely followed the odds, it's VI would be unacceptably high.
As stated you can set the hold via the LCD menu.
Bear in mind that different jurisdictions have different regulations that govern the maximum hold for slot machines in that jurisdiction.
A typical maximum hold is 15%.
Understand that setting the hold to the maximum allowed by law doesn't necessarily maximize the profit generated by that machine, because a higher hold might discourage players from using the machine.
I suspect, however, that many players ignore the hold, which is typically buried in fine print, and that the demand curve for a machine is relatively vertical meaning that the cost of using the machine, the hold, is largely ignoredand that profit generated by the machine is far more dependent on the location or placement of the machine as well as the design of the game itself.
But that's just speculation.
I'm sure there are some savvy gamblers out there who are sensitive to the hold.
The spreadsheet, available with the code, with three tables was built to prove that the game is working correctly the first table appears above.
The first step in building the spreadsheet was to accurately calculate the odds of each type of win the Calculated Probability columns.
Three Spaceships The probability that three spaceships will appear is the inverse of the total number of possible combinations.
The number of winning combinations, one, over the total number of possible combinations, 15625.
I learned how to calculate the odds from the probability.
Three Symbols Match except spaceships The probability that three symbols, other than the spaceships, will match is 24 the number of unique symbols on each reel minus the spaceships divided by the number of possible combinations.
That makes the odds about 1 to 650.
Two Spaceships There are 24 x 3 total combinations of two spaceships matching.
That's because there are see more ways to make two matches of a spaceship.
There are 24 possible values for Y.
The odds are 1 to 216.
One Spaceship Appears For each reel there are 24 x 24 combinations possible for a single spaceship appearing.
A spaceship can appear on any reel, so you need to multiply the number of combinations available on a single reel by three reels.
Odds are 1 to 8.
Two Symbols Match For any given two symbols, except the spaceships, there are 23 25 minus one spaceship minus one symbol that would make it a three symbol match x 3 reels x 24 symbols that are not spaceships.
Odds are 1 to 8.
To understand how to do this I relied heavily on post.
I entered values in the House Income column of the first table, using a process of trial and error, until the VI was under 20 and the Total in cell J10 was as quick tips for craps to zero as I could get it.
Then, first by using a hold of zero percent, I ran five simulations of 1, 000, 001 plays, and entered the values from the metrics menu into the appropriate rows and columns in the Actual Results table the third table.
I observed that the Probabilities Actual closely tracked with the Calculated probabilities, and that the Pct Diff Prob column was reasonable.
I also matched the values in the House Pays row up with the range of values from the Income High and Income Low columns of the 1, 000, 000 row of the Understanding Potential Income table the second tableand observed that the values from the Actual Results table were inside of the range specified by the Income High and Income Low columns.
The Understanding Potential Income table defines the expected range of income for a given hold value with a 90% confidence interval.
In the example below the hold is set to 0, so the likelihood of winning match the likelihood of losing.
If you play the game 1 million times there is a 90% likelihood that the Income will be between 16, 432 and - 16, 432.
Finally I changed the hold to 15% and ran another set of 5 simulations to verify that the game's income is in line with expectations if it were to be deplyed in a real world situation Here is the income table for a 15% hold.
If you want to really understand all of the math behind setting the payout values I encourage you to examine the formulas in the spreadsheet.
If you find any errors kindly point them out to me; I am not a mathematician or a C programmer by trade, so the standard disclaimer applies.
The Code I will not be taking you through the code line by line.
It's extensively commented and there's nothing tricky going on anywhere.
So use the Force, read the source.
If you're not familiar with the manipulation of registers on the ATmega386 and would like to understand more about how to write code for the AVR micro-controller without relying on the Arduino library, I'd encourage you to get a copy of Elliott William's excellent book, "Make: AVR Programming".
If you happen to have a subscription toyou'll find it.
Otherwise it's available on Amazon.
In these programs I use the Arduino functions in some places, and in other places I manipulate the registers directly.
The first thing you might notice is that the program makes extensive use of global variables.
There's a good discussion on this topic at.
I'm not going to promote or defend heavy use of global variables here, but Link would encourage you to understand all perspectives on the topic and recognize that there's a strong argument for using them on an embedded application project with a single programmer and limited resources.
I do make use of some libraries, without which this project would have been impossible for me.
The Timer Free Tone Library is used to drive various frequencies through c code for slot machine passive piezo speaker.
You can use that to put together any melody you wish.
I only use a handful of them to play part of the theme from "Close Encounters of the Third Kind" when the SlotMachine's micro-controller starts and the setup function runs.
I selected the timer free library because I thought I was going to need the timer for something, but I ended up not using the timer at all.
It's available if you need it.
The LED Control Library is used in both SlotMachine.
In the former it's used to control the three 8 x 8 LED matrices that serve as the slot machines reels.
This would be a good time to mention that I tried to avoid using another AVR chip ATmega328 just to serve up the credit balance, but I could not find a way to control the 8 x 8 matrices and the 8 digit seven segment display from a single micro-controller.
So in the end I had to create an I2C slave to serve that purpose.
It's definitely the case that you could use a less expensive AVR to do the job of displaying the credit balance, but to keep things simple for this article I elected to use another ATmega328P-PU chip.
On the bright side, when you win a large jackpot the credits continue to count up on the credit display slave while you can go ahead and spin again.
Be sure that the LCD display you acquire for this project is I2C capable.
If it isn't you'll need to acquire anpart number PCF8574, depicted below, and solder it to your LCD1602 display.
The game can be in a number of different states at the same time, and the machineState variable tracks the states.
For example, it can be 'spinning' and in 'auto mode' at the same time.
I don't really make heavy use of this concept inside of the program; not as much as I have in other programs, anyhow.
But there is some conditional branching based on the c code for slot machine />There is also the concept of events, and events are dispatched and handled in the ProcessEvents function.
It would probably be better if there was an event queue, but I didn't go that far.
There's a list of known defects and 'to dos' in the comments section of SlotMachine.
Sometimes when you 'spin' the reels by pressing the spin button or selecting the 'Play' option from the LCD menu one or even two of the reels don't move.
That's because the random number generator behind the scenes picked symbol that's already displaying for that reel.
This could be fixed to make the game appear more realistic, but it's not really a defect.
The reels don't finish spinning left to right, as they do on most slot machines.
This is done by design, to keep things simple.
It would be possible to have the reels finish spinning from left to right by sorting the three random numbers that are generated for each spin in ascending order before the reels actually spin, and I didn't bother.
As far as 'todos', I would at some point like to add brown out protection and watch dog protection, just to go through the exercise and learn how to do it.
Note that 80% of the space allocated for global variables is already consumed.
This is the point at which things can start to become unstable with the ATmega386 and Arduino programs.
We're at that point with this program.
I've had to do some budgeting the keep things working, and I wouldn't recommend adding any more globals to the program.
This would make it difficult to add more functionality to the Settings portion of the menu, for example, because the menu consumes a lot of global variable space.
I did try to solve the global variable problem by moving the menus into program memory, but I couldn't c code for slot machine that to reduce the space used by globals, I think because the compiler needs to pre-allocate all of the space for the menus anyhow.
More work c code for slot machine be done to spice up the game a bit; I could make more use of the RGB LED and the piezo buzzer, celebrate a win a little more, maybe make a better sound when money is lost, but I'll leave that to anyone who wants continue reading play with it.
I had to design all of the symbols for the game.
Some of them will remind you of the classic arcade game 'Space Invaders', and I may have borrowed those from somewhere.
The rest of them I designed by hand, and some of them are less than professional looking.
I used site to help design the symbols.
If you more info to adjust the symbols you can do that in SlotMachine.
It won't affect the program logic.
The code is available on GitHub.
Building the Slot Machine I used an FTDI USB to serial board to program both ATmega328P-PU micro-controllers in-place.
These connections are not depicted in the Fritzing schematic.
For instructions on setting up the FTDI break out board on your solder-less breadboard follow link.
You may need to google around a bit to nail the setup.
I believe post also helped me troubleshoot an issue I was having trying to get the micro-controller to automatically reset at the start of programming via the FTDI breakout board.
If you elect to use your Arduino Uno to program the chips, instructions are found.
If you're just going to program the chips once with the supplied code it's probably quickest and easiest to just program them from the Arduino Uno.
Both mico-controllers are set up with the 'Arduino' chip the China shores slots for iphone on the breadboard.
If you're planning on ultimately building this project by soldering the components together, or if you just want to copy what I've done here when you breadboard the project, you'll million fish free casino coins for gold to understand how to set up the Arduino on a breadboard.
Follow the excellent instructions for doing that.
To do that you'll need an AVR programmer like the AVRISP mk II or the USBTiny ISP.
You can also just use your Arduino, if you have one, to burn the bootloader.
All of your options are explained when you follow the link above.
I've tried to include the supplier I used for all of the pieces.
That's a little on the pricey side in my view.
I'm hoping that everything will fit and that the top is very transparent so that I don't have to cut holes in it to see the reels and the credit balance display.
We'll see how it goes when it gets here!
Software All of these libraries listed in the parts section will need to be installed into your Arduino development environment if you wish to compile the code so that you can upload it onto your ATmega chip.
Below I've included some directions on wiring the micro-controllers, because the Fritzing diagram is crowded.
This doesn't represent all of the connections necessary, but it should clear up any confusion.
I haven't grounded all of the unused pins, but I am probably going to do that in the final product.
If you're having trouble following the Fritzing diagram with respect to setting up the circuitry for the power supply, remember to lookunder Adding circuitry for a power supply.
Remember to add the switch between the breadboard ground rail and the power supply circuit so that you can power the circuit off and on without having to unplug or disconnect the power supply.
That will be important when we put everything into an enclosure.
The most challenging part was understanding all of the math necessary to create a payout table that works.
I hope you can have fun with this project too, if you decide to build it.
If you have any problems, questions, or, most importantly, discover any defects in the code or with the math, please contact me so I can fix any problems!
My email address is dan-murphy comcast.
I'll be creating part II of this article when I enclose all of the components.
That program is supplied in a seperate file.
This is for entertainment only.
It was the intent of the author to program this game to return ~%hold of every wager to the house, similar to many slot machines.
I tried that and couldn't get more than one LedControl object to work at a time.
So I had to create an I2C slave instead and use another AVR.
Special thanks to Elliott Williams for his essential book "Make: AVR Programming", which is highly recommended.
Thanks also to Cory Potter, who gave me the idea to do this.
Murphy hereby disclaims all copyright interest in this program written by Daniel J.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
Used to control how long the siren sounds.
Must be a better way.
Set the baud rate in setup.
I've never seen it run for a three ship match.
This file has been truncated, please download it to see its full contents.
Murphy hereby disclaims all copyright interest in this program written by Daniel T nuts for aluminum />This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
The function is registered as an event see setup.
Visual Basic Project: Simple Slot Machine
Once seen as a harmless diversion, hi-tech slot machines now bring in. would be issued in what she terms "the universally pleasant tone of C"... to a much larger set of virtual reels, existing as code within the machine.
Rather useful message
Willingly I accept. The theme is interesting, I will take part in discussion. Together we can come to a right answer.
It agree, it is a remarkable phrase
It was and with me. Let's discuss this question.
The matchless theme, very much is pleasant to me :)
It agree, the helpful information
In my opinion it is very interesting theme. Give with you we will communicate in PM.
I am am excited too with this question. Prompt, where I can read about it?
Willingly I accept. An interesting theme, I will take part.