limchengwei
Published © CC BY

Slot machine game

A simple and enjoyable slot machine.

IntermediateFull instructions provided489
Slot machine game

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
ST7789 240*240 display
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Slot machine game

Arduino
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>             // Arduino SPI library
 
// ST7789 TFT module connections
#define TFT_CS    10  // define chip select pin
#define TFT_DC     8  // define data/command pin
#define TFT_RST    9  // define reset pin, or set to -1 and connect to Arduino RESET pin

Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);

int button = 2;
int num1, num2, num3, num4;

void setup(void)
{
  // if the display has CS pin try with SPI_MODE0
  tft.init(240, 240, SPI_MODE2);    // Init ST7789 display 240x240 pixel
  // if the screen is flipped, remove this command
  tft.setRotation(2);
  tft.fillScreen(ST77XX_BLACK);
  randomSeed(analogRead(A0));
  attachInterrupt(digitalPinToInterrupt(button), next, RISING);
}
 
void loop() 
{
  tft.fillScreen (ST77XX_BLACK);
  tft.setTextColor(ST77XX_YELLOW);
  tft.setTextSize(10);
  tft.setCursor(0, 0);
  tft.println(num1);
  tft.setCursor(60, 0);
  tft.println(num2);
  tft.setCursor(120, 0);
  tft.println(num3);
  tft.setCursor(180, 0);
  tft.println(num4);
  if (num1 == num2 && num2 == num3 && num3 == num4)
  {
    tft.setTextColor(ST77XX_RED);
    tft.setTextSize(10);
    tft.setCursor(20, 120);
    tft.println("WIN");
  }
  delay (1000);
}

void next()
{
  num1 = random(2);
  num2 = random(2);
  num3 = random(2);
  num4 = random(2);
}

Credits

limchengwei

limchengwei

24 projects • 4 followers

Comments