Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LED Matrix Slot machine game
/*
* Program name: This program is part of the project Pen Stand: LED Enabled
* Author: Aditya K
*
* Discription: This program imitates Slot Machine game. This game has 3 rounds.
* A 'round' starts when the user removes the pen from pen stand causing an interrupt.
* For each round, the reel is spun 5 times displaying different symbols;
* this is done by generating random indexes and passing it to the default Font table displaying the spinning animation.
* The last index generated (5th spin) is stored in a list.
* After 3 rounds( 3 rounds because we have 3 LED matrix modules),indexes in the list are compared, if same, the user WINS else user LOSES.
*
*
* LICENSE: MIT
* Copyright (c) 2017 Aditya K.
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>
#define PEN_PIN 2
#define MAX_DEVICES 3
#define CLK_PIN 13
#define DATA_PIN 11
#define CS_PIN 10
#define SCROLL_SPEED 70
#define PAUSE 100
#define ROLL_SPEED 50
#define ROLL_PAUSE 110
#define MAX_ZONES 3
#define MAX_SPINS 5
#define RUN_FOR_N_MILLISECONDS(N) for(uint32_t start = millis(); (millis()- start) < N; )
volatile bool int_flag = false;
uint8_t win_reel[3] = {0};
uint8_t curr_zone = 0;
MD_Parola P = MD_Parola(CS_PIN, MAX_DEVICES);
void setup() {
pinMode(PEN_PIN, INPUT_PULLUP);
power_twi_disable();
power_usart0_disable();
P.begin(MAX_ZONES);
setup_reels();
attachInterrupt(digitalPinToInterrupt(PEN_PIN), animation_ISR, CHANGE);
}
void loop() {
deep_sleep();
randomSeed(analogRead(A0));
if (int_flag) {
bool pin_status = digitalRead(PEN_PIN);
delay(30);
pin_status = digitalRead(PEN_PIN);
/* Pen is removed */
if (pin_status == HIGH) {
win_reel[curr_zone] = spin_reel(curr_zone);
if (curr_zone == MAX_ZONES-1) {
result();
setup_reels();
} else {
curr_zone++;
}
int_flag = false;
}
/* Pen is inserted */
else if (pin_status == LOW) {
int_flag = false;
}
}
attachInterrupt(digitalPinToInterrupt(PEN_PIN), animation_ISR, CHANGE);
delay(50);
}
void result() {
if ((win_reel[0] == win_reel[1]) && (win_reel[1] == win_reel[2])) {
P.displaySuspend(false);
P.displayShutdown(false);
P.setZone(0, 0, 2);
P.displayClear();
P.displayReset(0);
P.displayZoneText(0, "YOU WIN!", PA_CENTER, SCROLL_SPEED, PAUSE, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
RUN_FOR_N_MILLISECONDS(4600) {
P.displayAnimate();
}
} else {
P.displaySuspend(false);
P.displayShutdown(false);
P.setZone(0, 0, 2);
P.displayClear();
P.displayReset(0);
P.displayZoneText(0, "TRY AGAIN", PA_CENTER, SCROLL_SPEED, PAUSE, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
RUN_FOR_N_MILLISECONDS(4600) {
P.displayAnimate();
}
}
}
uint8_t spin_reel(uint8_t curr_zone) {
uint8_t index = 0;
char str[2] = {0};
P.displayShutdown(false);
P.displaySuspend(false);
for(uint8_t i=0; i<MAX_SPINS; i++) {
randomSeed(analogRead(0));
/* For displaying symbols,default Font table is used.
* For each spin generate a random table index to print different characters or symbols from Font table.
*/
index = (uint8_t)random(1,128);
str[0] = index; str[1] = '\0';
if(i == MAX_SPINS-1) {
P.displayZoneText(curr_zone, str, PA_CENTER, 55, 110, PA_SCROLL_DOWN, PA_NO_EFFECT);
RUN_FOR_N_MILLISECONDS(1000) {
P.displayAnimate();
}
} else {
P.displayZoneText(curr_zone, str, PA_CENTER, 55, 110, PA_SCROLL_DOWN, PA_SCROLL_DOWN);
RUN_FOR_N_MILLISECONDS(1000) {
P.displayAnimate();
}
}
}
P.displaySuspend(true);
P.displayShutdown(true);
return index;
}
void setup_reels() {
/*
* We have 3 Matrix modules numbered 0,1 and 2 each mapped to its
* Zone. Each 'Zone' module can be controlled individually.
*
* Module 0 --> Zone 2 (Arduino is connected to this module)
* Module 1 --> Zone 1
* Module 2 --> Zone 0
*/
P.setZone(0, 2, 2);
P.setZone(1, 1, 1);
P.setZone(2, 0, 0);
P.setIntensity(0, 10);
P.setIntensity(1, 10);
P.setIntensity(2, 10);
P.displayClear();
P.displayShutdown(true);
curr_zone = 0;
/* clear the reel for new game */
win_reel[0] = win_reel[1] = win_reel[2] = 0;
}
void deep_sleep() {
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
sleep_cpu();
}
void animation_ISR() {
sleep_disable();
int_flag = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.