Dan Murphy
Published © GPL3+

ATmega Alien Themed Slot Machine

This is a three-reeled slot machine with 25 symbols on each reel. The game is configurable.

IntermediateFull instructions provided8 hours5,830

Things used in this project

Hardware components

ATmega328
Microchip ATmega328
ATmega328P-PU, to be precise. $3.00 One for the SlotMachine, one for the I2C credit LED display slave.
×2
Seven Segment display with 8 digits
$1.20 To display the player's credit balance.
×1
8x8 matrix, 4 segments, MAX7219
$3.78 To simulate spinning reels and display the symbols. Only three of the four segments are used.
×1
I2C 2004 Serial Blue Backlight LCD Module 20 X 4 2004
$3.00 For displaying the menu of options. Shop around on aliexpress. Don't pay for shipping!
×1
Breadboard (generic)
Breadboard (generic)
830 point $4.00
×2
Momentary Contact Buttons
$1.00 for 50. One controls spinning the reels, three for navigating the menu, two for grounding pin 1 of the ATmegas.
×6
RGB Diffused Common Cathode
RGB Diffused Common Cathode
Used to signal various things.
×1
LED (generic)
LED (generic)
Indicates if power is being supplied to the boards.
×1
Resistor 10k ohm
Resistor 10k ohm
4 to pull up each of the buttons, 2 for pin 1 of the ATmegas.
×6
Resistor 1k ohm
Resistor 1k ohm
Between each of the buttons and the ATmega input pins.
×4
Resistor 330 ohm
Resistor 330 ohm
For the red, green and blue leads of the RGB LED.
×3
16 MHz Crystal
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
Slide Switch
Slide Switch
For the power supply.
×1
Buzzer
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.
×2
Capacitor 22 pF
Capacitor 22 pF
×4
0.10 uF Capacitor
×6
Capacitor 100 nF
Capacitor 100 nF
This is optional and only needed if you're using the Arduino Mini USB serial adapter for programming the SlotMachine chip, as I have.
×1
Capacitor 10 µF
Capacitor 10 µF
To help smooth out the supply voltage.
×2
Linear Regulator (7805)
Linear Regulator (7805)
To regulate the voltage supply, 5V.
×1
Jumper wires (generic)
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.
×1
A 5v power supply
×1
Arduino Mini USB serial adapter
Arduino Mini USB serial adapter
$13.20 This is optional, you can use your Arduino Uno to program the ATmega 328p-pu chips.
×1
FTDI USB to TTL Serial Adapter
$1.66 x 2 = $3.32 For programming the ATmega328P-PUs in place. Not depicted in the schematic.
×1
Solder-able Breadboard
A full sized solder-able breadboard.
×1
SparkFun Solder-able Breadboard - Mini
SparkFun Solder-able Breadboard - Mini
×1
Pocket Solder- 60/40 Rosin Core 0.031" diameter
Pocket Solder- 60/40 Rosin Core 0.031" diameter
×1
Clear Plastic Waterproof Electronic Project Box Enclosure
$13.00 This is the enclosure.
×1

Software apps and online services

Arduino IDE
Arduino IDE
Timer Free Tone Library
LED Control Library
LiquidCrystal/LCD Library
LiquidCrystal I2C Library

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Helping Hands

Story

Read more

Schematics

Fritzing File

Fritzing File (.fzz)

Excel Spreadsheet

This spreadsheet was used to prove that the payout table is correct. Sheet password is "password".

Close Encounters Slot Machine

link to files on Fritzing.org

Fritzing Schematic

The Fritzing Schematic

Code

SlotMachine.ino

Arduino
/*SlotMachine.ino

  Version:   1.0
  Date:      2018/07/01 - 2018/08/29
  Device:    ATMega328P-PU @ 16mHz
  Language:  C

  Purpose
  =======
  A slot machine for entertainment and educational purposes only, 
  with the following features:
  - AtMega328P microcontroller running at 16mHz
  - Custom I2C seven segment display for displaying credit balance,
    also built with an ATMega328P running at 16mHz.  That program is
    supplied in a seperate file.
  - Three 8x8 LED matricies for displaying symbols driven by MAX7219. 
  - I2C LCD display 20x4, to show menus
  - various buzzers, buttons and an RGB LED.
  - the ability to update various settings via the LCD menu to 
    influence the machine's behavior.
  - the ability to change the amount of the wager.
  
  Known Defects
  =============  
  - Sometimes one or two of the reels won't spin, not really a defect.
  - crash as soon as payed out exceeds 1,000,000.
  
  TODO
  ====
  - add brown out detection
  - add watch dog protection (wdt_enable(value), wdt_reset(), WDTO_1S, WDTO_250MS)

  Warnings
  ========
  - Beware of turning on too much debugging, it's easy to use all 
    of the data memory, and in general this makes the microcontroller
    unstable.
  - Gambling is a tax on people who are bad at math.  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.
  - Why not control the LED that displays the credits with the LedControl 
    library?  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.  

  Suggestions
  ===========
  - Best viewed in an editor w/ 160 columns, most comments are at column 80
  - Please submit defects you find so I can improve the quality of the program
    and learn more about embedded programming.

  Author
  ======
  - Copyright 2018, Daniel Murphy <dan-murphy@comcast.net>
  - Contributors: Source code has been pulled from all over the internet,
    it would be impossible for me to cite all contributors.
    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.

  License
  =======
  Daniel J. Murphy hereby disclaims all copyright interest in this
  program written by Daniel J. Murphy.

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  any later version.

  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.  If not, see <http://www.gnu.org/licenses/>.

  Libraries 
  =========
  - https://github.com/wayoda/LedControl
  - https://bitbucket.org/teckel12/arduino-timer-free-tone/wiki/Home
  - https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
  - https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home

  The Program
  ===========
  - Includes                                                                    */
#include <avr/io.h>
#include <avr/eeprom.h>
#include <stdlib.h>                                                             // for the abs function
#include "LedControl.h"                                                         // https://github.com/wayoda/LedControl
#include "SlotMachine.h"
#include <TimerFreeTone.h>                                                      // https://bitbucket.org/teckel12/arduino-timer-free-tone/wiki/Home
#include <Wire.h>
#include <LCD.h>                                                                // https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home
#include <LiquidCrystal_I2C.h>                                                  // https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library

//- Payout Table
/*  Probabilities based on a 1 credit wager
    Three spaceships:     1 / (25 * 25 * 25)    = 0.000064
    Any three symbols:            24 / 15625    = 0.001536
    Two spaceships:         (24 * 3) / 15625    = 0.004608
    One spaceship:      (24 * 24 * 3)/ 15625    = 0.110592
    Two symbols match: (23 * 3 * 24) / 15625    = 0.105984
    House win, 1 minus sum of all probabilities = 0.777216
    _
    Use the spreadsheet to work out the payout table remembering to keep the 
    volatility resonable i.e. < 20.
                                                   P   R   O   O   F
                                                   Actual    Actual    
        Winning Combination Payout   Probablility  Count     Probability
        =================== ======   ============  ========  ===========*/
#define THREE_SPACESHIP_PAYOUT 600 //    0.000064            0.00006860   see the excel spreadsheet  
#define THREE_SYMBOL_PAYOUT    122 //    0.001536            0.00151760   that accompanies this program.
#define TWO_SPACESHIP_PAYOUT    50 //    0.004608            0.00468740
#define ONE_SPACESHIP_PAYOUT     3 //    0.110592            0.11064389
#define TWO_SYMBOL_PAYOUT        2 //    0.105984            0.10575249
//
// With these payouts the Volatility Index is 16.43
//
//- Macros
#define ClearBit(x,y) x &= ~y
#define SetBit(x,y) x |= y
#define ClearBitNo(x,y) x &= ~_BV(y)                                            
#define SetState(x) SetBit(machineState, x)

//- Defines
#define DEBUG                   1                                               // turns on (1) and off (0) output from debug* functions
#define BAUD_RATE               38400                                           // Baud rate for the Serial monitor
                                
#define NUMFRAMES               25                                              // Number of symbols in each "reel" or "slot". e.g three reels: |7|7|7|
#define LINESPERFRAME           8                                               // each line corresponds to one row on the 8x8 dot matrix LED
#define FRAME_DELAY             100                                             // milliseconds, controls the speed of the spinning reels
#define NUMREELS                3                                               // the hardware (8x8 matricies) accomodates 4 reels, we're only using three now
                                
#define DEBOUNCE_TIME           1000                                            // microseconds (changed from 500 to 1000 to cut down on double press problem)
                                
#define BUTTON_DDR              DDRD                                            // this accomodates the button that starts the reels spinning
#define BUTTON_PORT             PORTD
#define BUTTON_PIN              PIND
#define PCMSK_BUTTON            PCMSK2
#define PCIE_BUTTON             PCIE2
                                
#define BUTTON_SPIN_PIN         DDD2                                            // the actual spin button
#define BUTTON_SPIN_INT         PCINT18
#define BUTTON_SPIN_PORT        PORTD2
                                
#define NAV_DDR                 DDRC                                            // this is for the buttons that control menu navigation on the 20x4 LCD
#define NAV_PORT                PORTC
#define NAV_PIN                 PINC
#define PCMSK_NAV               PCMSK1
#define PCIE_NAV                PCIE1
                                
#define NAV_UP_PIN              DDC1                                            // Navigate up button
#define NAV_UP_INT              PCINT9
#define NAV_UP_PORT             PORTC1
                                
#define NAV_DOWN_PIN            DDC2                                            // Navigate down button
#define NAV_DOWN_INT            PCINT10
#define NAV_DOWN_PORT           PORTC2
                                
#define SELECT_PIN              DDC3                                            // Select current menu item button
#define SELECT_INT              PCINT11
#define SELECT_PORT             PORTC3
                                
#define BUZZER_DDR              DDRB                                            // This is for the slot machines piezo buzzer
#define BUZZER_PORT             PORTB
#define BUZZER_PIN              DDB3
#define TONE_PIN                11                                              // Pin you have speaker/piezo connected to (TODO: be sure to include a 100ohm resistor).

#define EVENT_NONE              0                                               // These are all of the various events that can occur in the machine
#define EVENT_SPIN              1
#define EVENT_SHOW_MENU         2  
#define EVENT_SELECT            3
#define EVENT_NAV_UP            4
#define EVENT_NAV_DOWN          5
#define EVENT_BACK              6
#define EVENT_PLAY              10
#define EVENT_BET               11
#define EVENT_SETTINGS          12
#define EVENT_VIEW_METRICS      13
#define EVENT_RESET             14
#define EVENT_HOLD              15

#define STATE_IDLE              B00000001                                       // These are the various states the machine can be in, not all are
#define STATE_SPINNING          B00000010                                       // mutually exclusive.
#define STATE_AUTO              B00000100                                       // This state is for automatically running the program to gather metrics.
#define STATE_SHOW_MENU         B00001000                                       // State we're in when showing the menu.  Note you can spin and show menu 
                                                                                // concurrently.
#define MINIMUM_WAGER           5                                               // TODO: consider this something that can be changed via settings
#define WAGER_INCREMENT         5                                               // TODO: consider this something that can be changed via settings

#define ONE_SECOND              1000                                            // # milliseconds in one second. Used to control how long the siren sounds. 

#define SHIP_LOC                144                                             // Location of various symbols in the array of symbols maintained in SlotMachine.h
#define ALIEN_1_LOC             152                                             // needed for animation
#define ALIEN_2_LOC             160

#define EEPROM_FREQ             10000                                           // Write to EEPROM every Nth play
#define AUTO_MODE_MAX           1000000                                           // stop after this many plays in auto mode

#define RED                     1                                               // TODO: should we use an enum here?  Must be a better way...
#define GREEN                   2
#define BLUE                    3
#define PURPLE                  4
#define WHITE                   5
#define OFF                     6

#define MAX_NOTE                4978                                            // Maximum high tone in hertz. Used for siren.
#define MIN_NOTE                31                                              // Minimum low tone in hertz. Used for siren.

#define STARTING_CREDIT_BALANCE 500                                             // Number of credits you have at "factory reset".
#define DEFAULT_HOLD            0                                               // default hold is zero, over time the machine pays out whatever is wagered

#define NUM_LED_DATAIN          7
#define NUM_LED_CLK             6
#define NUM_LED_LOAD            5
#define NUM_CHIP_COUNT          1

#define MATRIX_LED_DATAIN       8
#define MATRIX_LED_CLK          13
#define MATRIX_LED_LOAD         12
#define MATRIX_CHIP_COUNT       4

#define LOW_INTENSITY           1                                               // dim
#define HIGH_INTENSITY          10                                              // bright

#define SIREN_FLASHES           1

#define LCD_SCREEN_WIDTH        20
#define LCD_SCREEN_HEIGHT       4

#define CREDITS_I2C_SLAVE_ADDR  0x10                                            // I2C addresses
#define LCD_I2C_ADDR            0x3F                                            // LCD display w/ 4 lines

#define BACKLIGHT_PIN           3
#define En_pin                  2
#define Rw_pin                  1
#define Rs_pin                  0
#define D4_pin                  4
#define D5_pin                  5
#define D6_pin                  6
#define D7_pin                  7

#define MENU_SIZE               17

#define MAIN_MENU_NUMBER        0
#define MAIN_MENU_ELEMENTS      6
char *mainMenu[] =       {                       "Play",
                                                 "Bet",
                                                 "Settings",
                                                 "Metrics",
                                                 "Reset",
                                                 "Hold",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " "    };

#define BET_MENU_NUMBER 1
#define BET_MENU_ELEMENTS       3
char *betMenu[] =        {                       "+5 credits: ",                // TODO: make this dynamic based on WAGER_INCREMENT
                                                 "-5 credits: ",
                                                 "Back",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " "    };


#define SETTINGS_MENU_NUMBER    2
#define SETTINGS_MENU_ELEMENTS  3
#define SETTINGS_BACK_ITEM      2
char *settingsMenu[] =        {                  "Auto/Manual",                 // TODO: fill out this menu with more cool options
                                                 "Toggle Sound ",
                                                 "Back ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " "    };


#define METRICS_MENU_NUMBER     3
#define METRICS_MENU_ELEMENTS   15
char *metricsMenu[METRICS_MENU_ELEMENTS];

#define HOLD_MENU_NUMBER        4
#define HOLD_MENU_ELEMENTS      3
char *holdMenu[] =        {                      "+1 percent: ",                
                                                 "-1 percent: ",
                                                 "Back",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " ",    
                                                 " "    };

int selectPos = 0;
int menuNumber = MAIN_MENU_NUMBER;
int elements = MAIN_MENU_ELEMENTS;

char *currentMenu[MENU_SIZE];

LiquidCrystal_I2C  lcd(     LCD_I2C_ADDR,                                       // Create the LCD display object for the 20x4 display
                            En_pin,
                            Rw_pin,
                            Rs_pin,
                            D4_pin,
                            D5_pin,
                            D6_pin,
                            D7_pin              );

LedControl lc=LedControl(   MATRIX_LED_DATAIN,                                  // Create the LED display object for the 8x8 matrix
                            MATRIX_LED_CLK,
                            MATRIX_LED_LOAD,
                            MATRIX_CHIP_COUNT   );                              // Pins: DIN,CLK,CS, # of chips connected

volatile int reelArrayPos[NUMREELS];
volatile byte machineState;
volatile byte event = EVENT_NONE;
volatile byte color = RED;

#define ADC_READ_PIN            0                                               // we read the voltage from this floating pin to seed the random number generator
#define RED_PIN                 9                                               // Pin locations for the RGB LED
#define GREEN_PIN               10
#define BLUE_PIN                3

#define NUM_NOTES               5                                               // The number of notes in the melody
                                                                                // EEProm address locations
#define PAYEDOUT_ADDR           0x00                                            // 4 bytes
#define WAGERED_ADDR            0x04                                            // 4 bytes
#define PLAYED_ADDR             0x08                                            // 4 bytes
#define TWO_MATCH_ADDR          0x12                                            // 4 bytes
#define THREE_MATCH_ADDR        0x16                                            // 2 bytes
#define SHIP_ONE_MATCH_ADDR     0x18                                            // 4 bytes
#define SHIP_TWO_MATCH_ADDR     0x22                                            // 2 bytes
#define SHIP_THREE_MATCH_ADDR   0x24                                            // 2 bytes
#define EEPROM_WRITES_ADDR      0x34                                            // 4 bytes
#define RESET_FLAG_ADDR         0x38                                            // 4 bytes
#define CREDIT_BALANCE_ADDR     0x42                                            // 4 bytes
#define HOLD_ADDR               0x46                                            // 2 bytes

boolean sound = true;
byte reelMatches = 0;                                                           // per play variables
byte shipMatches = 0;

unsigned long wagered = 0;                                                      // amount wagered on a single spin
double owedExcess = 0;                                                          // change, need to track this so hold is accurate
unsigned long twoMatchCount = 0;                                                // 1 if two symbols match
unsigned int threeMatchCount = 0;                                               // 1 if three symbols match
unsigned long shipOneMatchCount = 0;                                            // 1 if there's one ship present
unsigned int shipTwoMatchCount = 0;                                             // 1 if there are two ships present
unsigned int shipThreeMatchCount = 0;                                           // 1 if there are three ships present (Jackpot!)
unsigned long totalCalcs = 0;                                                   // total plays only relavent in auto mode
signed long startingCreditBalance;                                              // the credit balance before spinning
int increment = WAGER_INCREMENT;
#define DISP_CREDIT_INCREMENT  1                                                // on the seven segment display, increment/decrement the balance by this value until the final value is reached.
                                                                                // lifetime variables (stored in EEprom) Reset sets most back to zero
unsigned long storedPayedOut;                                                   // sum of all payouts
unsigned long storedWagered;                                                    // sum of all wagers  (profit = payouts - wagers)
unsigned long storedPlays;                                                      // the number of spins
unsigned long  storedTwoMatchCount;                                             // number of times two symbols have matched
unsigned int  storedThreeMatchCount;                                            // number of times three symbols have matched
unsigned long  storedShipOneMatchCount;                                         // number of times one ship has appeared
unsigned int  storedShipTwoMatchCount;                                          // number of time two ships have appeared
unsigned int  storedShipThreeMatchCount;                                        // number of times three ships have appeared (Jackpot!)
unsigned long storedEEpromWrites;                                               // number of times we've written to EEprom.  100,000 is the approximate maximum
signed long storedCreditBalance;                                                // the credit balance.
int storedHold = DEFAULT_HOLD;                                                  // the house advantage, in percent, usually between 1 and 15, 2 bytes  

volatile byte portdhistory = 0b00000100;                                        // default is high because of the pull-up, correct setting
volatile byte portchistory = 0b00001110;                                        // default is high because of the pull-up, correct setting
 
//- Debugging Routines                                                          // These routines are helpful for debugging, I will leave them in for your use.
                                                                                // For sending output to the serial monitor. Set the baud rate in setup.
void debug(String text) {
  if (DEBUG) {
    Serial.println(text);
  }
}

void debugNoLF(String text) {
  if (DEBUG) {
    Serial.print(text);
  }
}

void debugInt(signed int anInt) {
  if (DEBUG) {
    char myInt[10];
    itoa(anInt,myInt,10);
    debug(myInt);
  }
}

void debugLong(signed long aLong) {
  if (DEBUG) {
    char myLong[10];
    ltoa(aLong,myLong,10);
    debug(myLong);
  }
}

void debugDouble(double aDouble) {
  if (DEBUG) {
    char *myDouble = ftoa(aDouble);
    debug(myDouble);
  }
}

void debugMetric(const char myString[], signed int anInt) {
  if (DEBUG) {
    debugNoLF(myString);debugNoLF(F(": "));
    debugInt(anInt);
    Serial.print(F("\r\n"));
  }
}

void debugMetricLong(const char myString[], signed long aLong) {
  if (DEBUG) {
    debugNoLF(myString);debugNoLF(F(": "));
    debugLong(aLong);
    Serial.print(F("\r\n"));
  }
}

void debugStoredMetrics() {
  for (int i = 0; i < 11; i++) {
    debug(metricsMenu[i]);
  }
}

void debugMetricDouble(const char myString[], double aDouble) {
  if (DEBUG) {
    debugNoLF(myString);debugNoLF(F(": "));
    debugDouble(aDouble);
    Serial.print(F("\r\n"));
  }
}

                                                                                // quick and dirty ftoa for legacy code
char *ftoa(double f)                                                            // from https://www.microchip.com/forums/m1020134.aspx
{
    static char        buf[17];
    char *            cp = buf;
    unsigned long    l, rem;

    if(f < 0) {
        *cp++ = '-';
        f = -f;
    }
    l = (unsigned long)f;
    f -= (double)l;
    rem = (unsigned long)(f * 1e6);
    sprintf(cp, "%lu.%10.10lu", l, rem);
    return buf;
}

//- All Other Functions

void beep() {                                                                   // Beep and flash LED green unless STATE_AUTO
  setGreen();
  if (sound) {
    BUZZER_PORT |= (1 << BUZZER_PIN);                                           // turn on buzzer
    if (!(STATE_AUTO == (machineState & STATE_AUTO))) {
      delay(100);
    }
    BUZZER_PORT &= ~(1 << BUZZER_PIN);                                          // turn off the buzzer
  }
  setOff();
}

void beepAuto() {                                                               // Beep even during STATE_AUTO, flash LED blue
  setBlue();
  if (sound) {
    BUZZER_PORT |= (1 << BUZZER_PIN);                                           // turn on buzzer
    delay(100);
    BUZZER_PORT &= ~(1 << BUZZER_PIN);                                          // turn off the buzzer
  }
  setOff();
}


void beepPurple() {                                                             // Beep and flash LED purple unless STATE_AUTO
  if (!(STATE_AUTO == (machineState & STATE_AUTO))) {
    setPurple();
    if (sound) {
      BUZZER_PORT |= (1 << BUZZER_PIN);                                         // turn on buzzer
      delay(100);
      BUZZER_PORT &= ~(1 << BUZZER_PIN);                                        // turn off the buzzer
    }
    setOff();
  }
}

void InitInturrupts()                                                           // Initialize interrupts for buttons and switches
{                                                                               
  PCICR |= (1 << PCIE_BUTTON);                                                  // Pin Change Interrupt Control Register, set PCIE2 to enable PCMSK2 scan
  PCICR |= (1 << PCIE_NAV);                                                     // Pin Change Interrupt Control Register, set PCIE1 to enable PCMSK1 scan
                                                                                // Pin Change Mask Register 2 for port D
  PCMSK_BUTTON|=(1<<BUTTON_SPIN_INT);                                           // Set PCINT2 to trigger an interrupt on state change
  PCMSK_NAV|=((1<<NAV_UP_INT)|(1<<NAV_DOWN_INT)|(1<<SELECT_INT));               // Set PCINT1 to trigger an interrupt on state change
  sei();                                                                        // enable interrupts
}

ISR (PCINT1_vect)
{
  byte changeddbits;
  changeddbits = NAV_PIN ^ portchistory;

  ClearBitNo(changeddbits,PORTC0);                                              // not a switch, ignore it
  ClearBitNo(changeddbits,PORTC4);                                              // not a switch, ignore it
  ClearBitNo(changeddbits,PORTC5);                                              // not a switch, ignore it
  ClearBitNo(changeddbits,PORTC6);                                              // not a switch, ignore it

  portchistory = NAV_PIN;

  ClearBitNo(portchistory,PORTC0);                                              // not a switch, ignore it
  ClearBitNo(portchistory,PORTC4);                                              // not a switch, ignore it
  ClearBitNo(portchistory,PORTC5);                                              // not a switch, ignore it
  ClearBitNo(portchistory,PORTC6);                                              // not a switch, ignore it

  if(changeddbits & (1 << NAV_UP_PIN)) 
  {
    if( (portchistory & (1 << NAV_UP_PIN)) == (1 << NAV_UP_PIN) )               // TODO: test using this instead of 16
    {
      _delay_us(DEBOUNCE_TIME);
      if (bit_is_set(NAV_PIN, NAV_UP_PIN)) {                                    // LOW to HIGH pin change (button released)
        // ADD CODE HERE
        int x = 0;
      }
    }
    else
    {
      _delay_us(DEBOUNCE_TIME);
      if (bit_is_clear(NAV_PIN, NAV_UP_PIN)) {                                  // button pressed
                                                                                // HIGH to LOW pin change (spin switch button pressed)
        event = EVENT_NAV_UP;
      }
    }
  }

  if(changeddbits & (1 << NAV_DOWN_PIN)) 
  {
    if( (portchistory & (1 << NAV_DOWN_PIN)) == (1 << NAV_DOWN_PIN) )           // TODO: test using this instead of 16
    {
      _delay_us(DEBOUNCE_TIME);
      if (bit_is_set(NAV_PIN, NAV_DOWN_PIN)) {                                  // LOW to HIGH pin change (button released)
        // ADD CODE HERE
        int x = 0;
      }
    }
    else
    {
      _delay_us(DEBOUNCE_TIME);
      if (bit_is_clear(NAV_PIN, NAV_DOWN_PIN)) {                                // button pressed
                                                                                // HIGH to LOW pin change (spin switch button pressed)
        event = EVENT_NAV_DOWN;
      }
    }
  }

  if(changeddbits & (1 << SELECT_PIN)) 
  {
    if( (portchistory & (1 << SELECT_PIN)) == (1 << SELECT_PIN) )               // TODO: test using this instead of 16
    {
      _delay_us(DEBOUNCE_TIME);
      if (bit_is_set(NAV_PIN, SELECT_PIN)) {                                    // LOW to HIGH pin change (button released)
        int x = 0;
      }
    }
    else
    {
      _delay_us(DEBOUNCE_TIME);
      if (bit_is_clear(NAV_PIN, SELECT_PIN)) {                                  // button pressed
                                                                                // HIGH to LOW pin change (spin switch button pressed)
        event = EVENT_SELECT;
      }
    }
  }
}

ISR (PCINT2_vect)
{
  byte changeddbits;                                                            // Will have bit corresponding to button pressed flipped on

  changeddbits = BUTTON_PIN ^ portdhistory;                                     // flip the bit corresponding to the button that was pressed

  ClearBitNo(changeddbits,PORTD0);                                              // not a switch, ignore it
  ClearBitNo(changeddbits,PORTD1);                                              // not a switch, ignore it
  ClearBitNo(changeddbits,PORTD3);                                              // not a switch, ignore it
  ClearBitNo(changeddbits,PORTD4);                                              // not a switch, ignore it
  ClearBitNo(changeddbits,PORTD5);                                              // not a switch, ignore it
  ClearBitNo(changeddbits,PORTD6);                                              // not a switch, ignore it
  ClearBitNo(changeddbits,PORTD7);                                              // not a switch, ignore it

  portdhistory = BUTTON_PIN;                                                    // set history = to the current state of input

  ClearBitNo(portdhistory,PORTD0);                                              // not a switch, ignore it
  ClearBitNo(portdhistory,PORTD1);                                              // not a switch, ignore it
  ClearBitNo(portdhistory,PORTD3);                                              // not a switch, ignore it
  ClearBitNo(portdhistory,PORTD4);                                              // not a switch, ignore it
  ClearBitNo(portdhistory,PORTD5);                                              // not a switch, ignore it
  ClearBitNo(portdhistory,PORTD6);                                              // not a switch, ignore it
  ClearBitNo(portdhistory,PORTD7);                                              // not a switch, ignore it

  if(changeddbits & (1 << BUTTON_SPIN_PIN)) 
  {
    if( (portdhistory & (1 << BUTTON_SPIN_PIN)) == (1 << BUTTON_SPIN_PIN) )     // TODO: test using this instead of 16
    {
      _delay_us(DEBOUNCE_TIME);
      if (bit_is_set(BUTTON_PIN, BUTTON_SPIN_PIN)) {                            // LOW to HIGH pin change (button released)
      }
    }
    else
    {
      _delay_us(DEBOUNCE_TIME);
      if (bit_is_clear(BUTTON_PIN, BUTTON_SPIN_PIN)){                           // button pressed
        // HIGH to LOW pin change (spin switch button pressed)
        if(STATE_SPINNING == (machineState & STATE_SPINNING)) {
          SetState(STATE_IDLE);
          event = EVENT_NONE;
        } else if (STATE_IDLE == (machineState & STATE_IDLE)) {
          if (STATE_AUTO == (machineState & STATE_AUTO)) {
            ClearBit(machineState, STATE_AUTO);
          }
          event = EVENT_SPIN;
        }
      }
    }
  }
}

void spinAndEvaluate() {                                                        // runs when the spin button is pressed or we 'Play' from the main menu
//debug("spinAndEvaluate()");
  spin();
  checkForWin();
  signed long winnings = calcWinnings();
  calcStored(winnings);
  if (!(STATE_AUTO == (machineState & STATE_AUTO))) {                           // if we're not in auto mode display the credits
    storeMetrics();
    displayCredits();
    if (reelMatches > 0) {
      celebrateWin(reelMatches);
    }
    setupMetricsMenu(); 
  } else if ((totalCalcs++%EEPROM_FREQ) == 0) {                                 //  EEPROM can be written ~100,000 times,
    storeMetrics();
    displayCredits();                                                           // displayCredits takes care of the sign on increment
    setupMetricsMenu(); 
    debugStoredMetrics();   
    debugMetricDouble("owedExcess",owedExcess);                                 // don't want to put owedExcess in metricsMenu because of global var space shortage                                                     
    if (totalCalcs >= AUTO_MODE_MAX) {                                          // drop out of auto mode when threshold exceeded
      ClearBit(machineState, STATE_AUTO);
      SetState(STATE_IDLE); 
      event = EVENT_NONE;
    }
  }
  ClearBit(machineState, STATE_SPINNING);
}

void spin() {
//debug("spin()");
  SetState(STATE_SPINNING);
  if (!(STATE_AUTO == (machineState & STATE_AUTO))) {
    beep();
  }

  zeroAllBalances();
  
  byte reelsStopped[NUMREELS] = {0,0,0};
  byte stopArrayPos[NUMREELS];
  for (int reelNum = 0; reelNum < NUMREELS; reelNum++) {
    if (!(STATE_AUTO == (machineState & STATE_AUTO))) {
      lc.setIntensity(reelNum,LOW_INTENSITY);                                   // Set intensity levels
    }
    stopArrayPos[reelNum] = random(0,(NUMFRAMES)) * LINESPERFRAME;      
    while (stopArrayPos[reelNum] == reelArrayPos[reelNum]) {                    // keep picking a stop array position until it's not equal to the current position
      stopArrayPos[reelNum] = random(0,(NUMFRAMES)) * LINESPERFRAME;
    }
  }
    while (!allReelsStopped(reelsStopped)) {
    for (int reelNum = 0; reelNum < NUMREELS; reelNum++) {
      if (reelArrayPos[reelNum] == ((NUMFRAMES * LINESPERFRAME) + 1)) {      
        reelArrayPos[reelNum] = 0;                                              // go back to top of reel
      }
      if(reelArrayPos[reelNum] != (stopArrayPos[reelNum]+1)) {
        if (!(STATE_AUTO == (machineState & STATE_AUTO))) {
          for (int row = 0; row < LINESPERFRAME; row++) {                       // simulate a spinning reel
            lc.setRow(reelNum,row,reel[reelArrayPos[reelNum] + row]);           // output to 8x8x3 matrix
          }
        }
        //delay(FRAME_DELAY);
        //reelArrayPos[reelNum] += LINESPERFRAME;                               // uncomment for fast play
        reelArrayPos[reelNum] += 1;
      } else {
        if (!(STATE_AUTO == (machineState & STATE_AUTO))) {
          lc.setIntensity(reelNum,HIGH_INTENSITY);                              // Set intensity levels
        }
        reelsStopped[reelNum] = 1;
      }
    }
  } 
}

void checkForWin() {                                                            // this only works if NUMREELS == 3 ! If you change NUMREELS you must do so programming! 
//debug("checkForWin()");
  
  for (int reelNum=0; reelNum < NUMREELS; reelNum++) {                          // see if ships appeared
    if ((reelArrayPos[reelNum] - 1) == SHIP_LOC) {
      shipMatches += 1;
    }
  }

  for (int i = 0; i < NUMREELS; i++) {                                          // check to see if other symbols matched   
    for (int j = 0; j < NUMREELS; j++) {
      if (reelArrayPos[i]  - 1 == reelArrayPos[j] - 1) {
        reelMatches += 1;
      }
    }
  }

  if (reelMatches == 9) {                                                       // code from the block above sets reelMatches to 9 if 3 symbols match
    reelMatches = 3;
    threeMatchCount++;
  } else if (reelMatches == 5) {                                                // etc...
    reelMatches = 2;
    twoMatchCount++;
  } else if (reelMatches == 3) {
    reelMatches = 0;
  } else {
    reelMatches = -1;                                                           // never used
  }

  if (shipMatches == 3) {
      shipThreeMatchCount++;
  } else if (shipMatches == 2) {
      shipTwoMatchCount++;
  } else if (shipMatches == 1) {
      shipOneMatchCount++;
  }

  if (shipThreeMatchCount) {                                    // Wins are mutually exclusive, subsequent code assumes that!
    threeMatchCount = 0;                                        // TODO: make this a switch statement
    shipTwoMatchCount = 0;
    shipOneMatchCount = 0;
    twoMatchCount = 0;
    reelMatches = 0;
  } else if (threeMatchCount) {
    shipTwoMatchCount = 0;
    shipOneMatchCount = 0;
    twoMatchCount = 0;
    reelMatches = 0;
  } else if (shipTwoMatchCount) {
    shipOneMatchCount = 0;
    twoMatchCount = 0;
    reelMatches = 0;
  } else if (shipOneMatchCount) {
    twoMatchCount = 0;
    reelMatches = 0;
  } else if (twoMatchCount) {
    reelMatches = 0;
  }
}

signed long calcWinnings() {
  double winnings = 0;
  //debugMetric("storedHold",storedHold);
  if(shipThreeMatchCount > 0) {
    winnings = wagered * (THREE_SPACESHIP_PAYOUT - (THREE_SPACESHIP_PAYOUT * (storedHold/100.0))); // winnings are the amount wagered times the payout minus the hold.
  } else if (threeMatchCount > 0) {
    winnings = wagered * (THREE_SYMBOL_PAYOUT - (THREE_SYMBOL_PAYOUT * (storedHold/100.0)));
  } else if (shipTwoMatchCount > 0) {
    winnings = wagered * (TWO_SPACESHIP_PAYOUT - (TWO_SPACESHIP_PAYOUT * (storedHold/100.0)));
  } else if (shipOneMatchCount > 0) {
    winnings = wagered * (ONE_SPACESHIP_PAYOUT - (ONE_SPACESHIP_PAYOUT * (storedHold/100.0)));
  } else if (twoMatchCount > 0) {
    winnings = wagered * (TWO_SYMBOL_PAYOUT - (TWO_SYMBOL_PAYOUT * (storedHold/100.0)));
  } else {
    winnings = 0;
  }
  signed long roundWinnings = (signed long) round(winnings);
  owedExcess += winnings - roundWinnings;                                       // owedExcess is the change; credits between -1 and 1.
  if (owedExcess >= 1 || owedExcess <= -1) {                                    // if we can pay out some excess
    int roundOwedExcess = (int) round(owedExcess);
    roundWinnings += roundOwedExcess;                                           // add the rounded portion to the winnings
    owedExcess -= roundOwedExcess;                                              // subtract out what we added to continue to track the excess
  } 
  roundWinnings -= wagered;                                                     // you pay for your bet whether you won or not!  
//  winnings -= wagered;
  return roundWinnings;
//  return((signed long) round(winnings));
}

void calcStored(signed long winnings) {
    storedPayedOut += winnings;
    storedWagered += wagered;
    startingCreditBalance = storedCreditBalance;
    storedCreditBalance += winnings;
    storedPlays += 1;                                                           // calcStored is called one time per play     
    storedTwoMatchCount += twoMatchCount;       
    storedThreeMatchCount += threeMatchCount;     
    storedShipOneMatchCount += shipOneMatchCount; 
    storedShipTwoMatchCount += shipTwoMatchCount; 
    storedShipThreeMatchCount += shipThreeMatchCount;
}

void storeMetrics() {
    beepAuto();                                                                 // so we know we're not hung in auto mode.
    updateStoredPayedOut();
    updateStoredWagered();
    updateStoredPlays();                
    updateStoredTwoMatchCount();        
    updateStoredThreeMatchCount();      
    updateStoredShipOneMatchCount();   
    updateStoredShipTwoMatchCount();   
    updateStoredShipThreeMatchCount(); 
    storedEEpromWrites++;
    updateStoredEEpromWrites();      
    updateStoredCreditBalance();   
    updateStoredHold();
}

void displayCredits() {
//debug("displayCredits()");
  int xmitIncrement;
  if ((STATE_AUTO == (machineState & STATE_AUTO))) {                            // display the credits here if we're in auto mode.
    xmitIncrement = abs(startingCreditBalance - storedCreditBalance);           // we don't want the display slave to count up/down
  } else {
    xmitIncrement = DISP_CREDIT_INCREMENT;                                      // set increment back to what it should be during manual play
  }
  
  Wire.beginTransmission(CREDITS_I2C_SLAVE_ADDR);

  Wire.write( startingCreditBalance & 0xFF); 
  Wire.write((startingCreditBalance & 0xFF00) >> 8);
  Wire.write((startingCreditBalance & 0xFF0000) >> 16);
  Wire.write((startingCreditBalance & 0xFF000000) >> 24);                       // most sigificant byte sent last

  if (startingCreditBalance > storedCreditBalance) {                            // if the player lost,
    xmitIncrement *= -1;                                                        // flip the sign on increment so we count down
  }
  Wire.write( xmitIncrement & 0xFF); 
  Wire.write((xmitIncrement & 0xFF00) >> 8);

  Wire.write( storedCreditBalance & 0xFF); 
  Wire.write((storedCreditBalance & 0xFF00) >> 8);
  Wire.write((storedCreditBalance & 0xFF0000) >> 16);
  Wire.write((storedCreditBalance & 0xFF000000) >> 24);                         // most sigificant byte sent last

  byte error = Wire.endTransmission();
  if (error==4)
  {
    debug(F("Unknown error at address"));                                       // I've never seen this happen.
  }    

}

bool allReelsStopped(byte reelsStopped[]) {
  byte sumStopped = 0;
  for (int i; i < NUMREELS; i++) {
    if (reelsStopped[i] == 1) {
      sumStopped += 1;
    }
  }
  if (sumStopped == NUMREELS) {                                                 // all reels stopped
    return 1;
  }
  return 0;
}

void celebrateWin(byte matches) {                                               // we can probably do better than this.  I've never seen it run for a three ship match...
//debug("celebrateWin()");
  for (int i = 0; i < (matches - 1); i++) {
    playSiren();
    delay(ONE_SECOND);
  }
}

void playSiren() {                                                              // play siren and toggle the RGB LED blue and red
//debug("playSiren()");
  for (int j = 1; j <= SIREN_FLASHES; j++){
    setBlue();
    for (int note = MIN_NOTE; note <= MAX_NOTE; note+=5){                       // 5 = # notes to step over. Necessary only w/ TimerFreeTone library.
      if (note%1236==0) {                                                       // at the top of the range change RGB color.
        if (color == RED) {
          for (int reelNum = 0; reelNum < NUMREELS; reelNum++) {
            lc.setIntensity(reelNum, LOW_INTENSITY);                            // this doesn't seem to be working...
          }
          setBlue();
        }
        if (color == BLUE) {
          for (int reelNum = 0; reelNum < NUMREELS; reelNum++) {
            lc.setIntensity(reelNum, HIGH_INTENSITY);                           // this doesn't seem to be working...
          }
          setRed();
        }
      }
      if (sound) {
        TimerFreeTone(TONE_PIN, note, 1);                                       // third parameter is duration
      }
    }
  }
  setOff();
  for (int reelNum = 0; reelNum < NUMREELS; reelNum++) {
    lc.setIntensity(reelNum, HIGH_INTENSITY);
  }
}

void setPurple() {
  //debug("setPurple()");
  setColor(170, 0, 255);                                                        // Purple Color
  color = PURPLE;
}

void setRed(){
  //debug("setRed()");
  setColor(255, 0, 0);                                                          // Red Color
  color = RED;
}

void setGreen(){
  //debug("setGreen()");
  setColor(0, 255, 0);                                                          // Green Color
  color = GREEN;
}

void setBlue(){
  //debug("setBlue()");
  setColor(0, 0, 255);                                                          // Blue Color
  color = BLUE;
}

void setWhite(){
  //debug("setWhite()");
  setColor(255, 255, 255);                                                      // White Color
  color = WHITE;
}

void setOff(){
  //debug("setOff()");
  setColor(0,0,0);                                                              // Off
  color = OFF;
}

void setColor(int redValue, int greenValue, int blueValue) {
  //debug("setColor()");
  analogWrite(RED_PIN, redValue);
  analogWrite(GREEN_PIN, greenValue);
  analogWrite(BLUE_PIN, blueValue);
}

void showColor(int color) {                                                     // There's got to be a better way to do this...
  switch(color) {
     case RED  :
        setRed();
        break; 
     case GREEN  :
        setGreen();
        break; 
     case BLUE  :
        setBlue();
        break;
...

This file has been truncated, please download it to see its full contents.

SlotMachine.h

C Header File
const byte reel[] =
{																			   // 0	star
	B10011001,  															   //0
	B01011010,
	B00111100,
	B11111111,
	B11111111,
	B00111100,
	B01011010,
	B10011001,
				                                                               // 1	one spot on dice
	B00000000,  															   // 8
	B00000000,
	B00000000,
	B00011000,
	B00011000,
	B00000000,
	B00000000,
	B00000000,
																			   // 2	three bars
	B11111111,																   // 16
	B11111111,
	B00000000,
	B11111111,
	B11111111,
	B00000000,
	B11111111,
	B11111111,
																			   // 3	heart
	B01100110,																   // 24
	B11111111,
	B11111111,
	B11111111,
	B11111111,
	B01111110,
	B00111100,
	B00011000,
																			   // 4	two spots on dice
	B00000000,																   // 32
	B01100000,
	B01100000,
	B00000000,
	B00000000,
	B00000110,
	B00000110,
	B00000000,
																			   // 5	seven
	B00000000,	  															   // 40
	B01111110,
	B01111110,
	B00001100,
	B00011000,
	B00111000,
	B00111000,
	B00000000,
																			   // 6	dollar sign
	B00011000,																   // 48
	B00111100,
	B01011010,
	B00111000,
	B00011100,
	B01011010,
	B00111100,
	B00011000,
																			   // 7	three spots on dice
	B00000000,
	B01100000,
	B01100000,
	B00011000,
	B00011000,
	B00000110,
	B00000110,
	B00000000,
																			   // 8	inverse 9 spots, hashtag #
	B00100100,
	B00100100,
	B11111111,
	B00100100,
	B00100100,
	B11111111,
	B00100100,
	B00100100,
																			   // 9	one bar
	B00000000,
	B00000000,
	B00000000,
	B11111111,
	B11111111,
	B00000000,
	B00000000,
	B00000000,
																			   // 10	four on dice
	B00000000,
	B01100110,
	B01100110,
	B00000000,
	B00000000,
	B01100110,
	B01100110,
	B00000000,
																			   // 11	inverse seven
	B11111111,
	B10000001,
	B10000001,
	B11110011,
	B11100111,
	B11000111,
	B11000111,
	B11111111,
																			   // 12	9 spots
	B11011011,
	B11011011,
	B00000000,
	B11011011,
	B11011011,
	B00000000,
	B11011011,
	B11011011,
																			   // 13	five on dice
	B00000000,
	B01100110,
	B01100110,
	B00011000,
	B00011000,
	B01100110,
	B01100110,
	B00000000,
																			   // 14	two bars
	B00000000,
	B11111111,
	B11111111,
	B00000000,
	B00000000,
	B11111111,
	B11111111,
	B00000000,
																			   // 15 Alien 0 (120)
	B01000010, 
	B00100100,
	B01111110,
	B11011011,
	B11111111,
	B11111111,
	B10100101,
	B00100100,
																			   // 16	smile face (128)
	B00000000,
	B00100100,
	B00000000,
	B00011000,
	B01000010,
	B01000010,
	B00111100,
	B00011000,
																			   // 17 	6 on dice (136)
	B00000000,
	B11011011,
	B11011011,
	B00000000,
	B00000000,
	B11011011,
	B11011011,
	B00000000,
																			   // 18 SpaceShip (144)
	B00000000,
	B00000000,
	B00111100,
	B01111110,
	B10101011,
	B01111110,
	B00111100,
	B00000000,
																			   // 19 Alien 1 (152)
	B00011000,   
	B00111100,
	B01111110,
	B11011011,
	B11111111,
	B00100100,
	B01011010,
	B10100101,
																			   // 20 Alien 2 (160)
	B00011000, 
	B00111100,
	B01111110,
	B11011011,
	B11111111,
	B00100100,
	B01011010,
	B01000010,
																			   // 21 Alien 3 (168)
	B00000000, 
	B10000001,
	B11111111,
	B11011011,
	B11111111,
	B01111110,
	B00100100,
	B01000010,
																			   // 22	one
	B00010000,
	B00110000,
	B00010000,
	B00010000,
	B00010000,
	B00010000,
	B00010000,
	B00111000,
																			   // 23	two
	B00111000,
	B01000100,
	B10000010,
	B00000100,
	B00001000,
	B00010000,
	B00100000,
	B11111110,
																			   // 24	three
	B11111111,																   // 192
	B00000010,
	B00000100,
	B00011100,
	B00000010,
	B00000100,
	B00001000,
	B11100000
};

/*************************************************
 * Public Constants
 *************************************************/

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698  	
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047	
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397  	
#define NOTE_FS6 1480
#define NOTE_G6  1568 	
#define NOTE_GS6 1661
#define NOTE_A6  1760  	
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

slotCreditsDisplaySlave.ino

Arduino
/*slotCreditsDisplaySlave.ino

  Version:   1.0
  Date:      2018/07/01 - 2018/07/29
  Device:    ATMega328P-PU @ 16mHz
  Language:  C

  Purpose
  =======
 `The .purpose of this program is to function as an I2C slave
  responsible for displaying credits in a slot machine 
  
  Known Defects
  =============  
  - 

  TODO
  ====
  - is 38400 an efficient baud rate for arduino running at 16mhz?
  - include a 100 ohm resistor with the piezo buzzer
  - is 100kHz the fastest setting we can accomodate w/ Wire library?
  
  Warnings
  ========
  - 
  
  Suggestions
  ===========
  - 
  
  Author
  ======
  - Copyright 2018, Daniel Murphy <dan-murphy@comcast.net>

  License
  =======
  Daniel J. Murphy hereby disclaims all copyright interest in this
  program written by Daniel J. Murphy.

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  any later version.

  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.  If not, see <http://www.gnu.org/licenses/>.

  Libraries 
  =========
  - https://github.com/wayoda/LedControl

  The Program
  ===========
  - Includes                                                                    */
#include <Wire.h>
#include "LedControl.h"

#define BAUD_RATE           38400                                               
#define CREDITS_SLAVE_ADDR  16                                                  
#define DISPLAY_DELAY       5
#define DEBUG               1

#define BUZZER_DDR        DDRB
#define BUZZER_PORT       PORTB
#define BUZZER_PIN        DDB1
#define TONE_PIN          9                                                     // Pin you have speaker/piezo connected to (be sure to include a 100 ohm resistor).
#define BEEP_LENGTH       100
                                                                                // Now we need a LedControl to work with.
                                                                                // pin 12 is connected to the DataIn 
                                                                                // pin 11 is connected to the CLK 
                                                                                // pin 10 is connected to LOAD 
                                                                                // We have only a single MAX72XX.
LedControl lc=LedControl(12,11,10,1);

static const int slaveAddress = CREDITS_SLAVE_ADDR;  

long volatile theCredits[10] = {0L,0L,0L,0L,0L,0L,0L,0L,0L,0L};

signed long volatile displayedBalance = 0;
signed long volatile startingCreditBalance = 0;
signed long volatile endingCreditBalance;
signed int volatile increment;
boolean volatile updateDisplayFlag = false;

void debug(String text) {
  if (DEBUG) {
    Serial.println(text);
  }
}

void debugNoLF(String text) {
  if (DEBUG) {
    Serial.print(text);
  }
}

void debugInt(signed int anInt) {
  if (DEBUG) {
    char myInt[10];
    itoa(anInt,myInt,10);
    debug(myInt);
  }
}

void debugLong(signed long aLong) {
  if (DEBUG) {
    char myLong[10];
    ltoa(aLong,myLong,10);
    debug(myLong);
  }
}

void debugMetric(const char myString[], signed int anInt) {
  if (DEBUG) {
    debugNoLF(myString);debugNoLF(": ");
    debugInt(anInt);
    Serial.print("\r\n");
  }
}

void debugMetricLong(const char myString[], signed long aLong) {
  if (DEBUG) {
    debugNoLF(myString);debugNoLF(": ");
    debugLong(aLong);
    Serial.print("\r\n");
  }
}

void beep() {
  BUZZER_PORT |= (1 << BUZZER_PIN);                                               // turn on buzzer
  delay(BEEP_LENGTH);
  BUZZER_PORT &= ~(1 << BUZZER_PIN);                                              // turn off the buzzer
}

void beepLong() {
  BUZZER_PORT |= (1 << BUZZER_PIN);                                               // turn on buzzer
  delay(1000);
  BUZZER_PORT &= ~(1 << BUZZER_PIN);                                              // turn off the buzzer
}

void setup() {     
  Serial.begin(BAUD_RATE);
  debug("setup()");

  BUZZER_DDR |= (1 << BUZZER_PIN);                                                // set buzzer pin to output
  BUZZER_PORT &= ~(1 << BUZZER_PIN);                                              // turn off the buzzer

  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,8);
  /* and clear the display */
  lc.clearDisplay(0);

  Wire.begin(CREDITS_SLAVE_ADDR);                                                 // join i2c bus with address #32
  TWBR=32;                                                                        // == 100kHz SCL frequency  
  Wire.onReceive(receiveEvent);                                                   // register event

  scrollDigits();
  beepLong();
}

void loop() {
  //debug("loop()");
  if (updateDisplayFlag) {
    updateDisplay();
  }
}

void updateDisplay() {
  //debug("updateDisplay()");

  for ( signed long displayBalance = startingCreditBalance; 
        displayBalance != endingCreditBalance; 
        displayBalance += increment) {
    showBalance(displayBalance);
  }
  showBalance(endingCreditBalance);
  beep();
  updateDisplayFlag = false;
}

void showBalance(signed long showBalance) {                                       // display showBalance on the 7-segment LED
  boolean negative = false;
  byte displayDigit = 0;

  if (showBalance < 0) {                                                          // if the balance is negative make it positive
    showBalance *= -1L;                                                           // for display purposes and
    negative = true;                                                              // set the negative flag to true for use next...
  }

  displayDigit = showBalance / 10000000L;                                         // extract the leftmost digit to display, the digit that's in the ten million's place
  if ((negative) && (displayDigit == 0)) {                                        
    lc.setChar(0, 7, '-', false);                                                 // if showBalance was negative display the negative sign here
  } else {
    lc.setDigit(0,7,displayDigit,false);                                          // otherwise just display the digit
  }
  showBalance = showBalance - ((showBalance / 10000000L) * 10000000L);              

  displayDigit = showBalance / 1000000L;                                          // extract the digit to display in the million's place
  lc.setDigit(0,6,displayDigit,false);                                            //      "
  showBalance = showBalance - ((showBalance / 1000000L) * 1000000L);              //      "
     
  displayDigit = showBalance / 100000L;                                           // and so on...
  lc.setDigit(0,5,displayDigit,false);                                            //      "
  showBalance = showBalance - ((showBalance / 100000L) * 100000L);                //      "

  displayDigit = showBalance / 10000L;             
  lc.setDigit(0,4,displayDigit,false);
  showBalance = showBalance - ((showBalance / 10000L) * 10000L);

  displayDigit = showBalance / 1000L;    
  lc.setDigit(0,3,displayDigit,false);
  showBalance = showBalance - ((showBalance / 1000L) * 1000L);

  displayDigit = showBalance / 100L;    
  lc.setDigit(0,2,displayDigit,false);
  showBalance = showBalance - ((showBalance / 100L) * 100L);

  displayDigit = showBalance / 10L;    
  lc.setDigit(0,1,displayDigit,false);
  showBalance = showBalance - ((showBalance / 10L) * 10L);

  displayDigit = showBalance;                                                     // finally, only the last digit to display remains.
  lc.setDigit(0,0,displayDigit,false);

  beep();
}

                                                                                  // This function executes whenever data is received from The 
                                                                                  // master.  The function is registered as an event (see setup()). 
void receiveEvent(int howMany) {
  //debug("receiveEvent()");
  int i = 0;
  while (1 <= Wire.available()) { // loop through all
    theCredits[i] = Wire.read();
    i++;
  }
                                                                                  // transfer the array into startingCreditBalance
                                                                                  // Little endian, least significant byte stored first
  startingCreditBalance = theCredits[0];
  startingCreditBalance = startingCreditBalance | (theCredits[1] << 8 );
  startingCreditBalance = startingCreditBalance | (theCredits[2] << 16);
  startingCreditBalance = startingCreditBalance | (theCredits[3] << 24);

  increment = theCredits[4];
  increment = increment | (theCredits[5] << 8);

  char buffer[50];

  endingCreditBalance = theCredits[6];
  endingCreditBalance = endingCreditBalance | (theCredits[7] << 8 );
  endingCreditBalance = endingCreditBalance | (theCredits[8] << 16);  // when theCredits[8] == 255 value isn't appended to endingCreditBalance
  endingCreditBalance = endingCreditBalance | (theCredits[9] << 24);
  updateDisplayFlag = true;
}

void scrollDigits() {
  //debug("scrollDigits()");
  for(int i=0;i<13;i++) {
    lc.setDigit(0,7,i,false);
    lc.setDigit(0,6,i+1,false);
    lc.setDigit(0,5,i+2,false);
    lc.setDigit(0,4,i+3,false);
    lc.setDigit(0,3,i,false);
    lc.setDigit(0,2,i+1,false);
    lc.setDigit(0,1,i+2,false);
    lc.setDigit(0,0,i+3,false);
    delay(100);
  }
  lc.clearDisplay(0);
}

Credits

Dan Murphy

Dan Murphy

2 projects • 25 followers
Software engineer with 35 years of experience in many programming languages. Electronics hobbyist; experience w/ ATMEL microcontrollers.
Thanks to Cory Potter.

Comments