#include <LiquidCrystal.h>
// Pin assignments for the LCD
const int rs = 16, en = 17, d4 = 18, d5 = 19, d6 = 21, d7 = 22;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// Pin assignments for the Button and LEDs
const int buttonPin = 14;
const int greenLedPin = 23; // Anode of the green LED
const int redLedPin = 25; // Anode of the red LED
const int yellowLedPin = 27; // Anode of the yellow LED
int buttonState = 0;
// Custom characters for the slot machine
byte cherry[8] = {0b00000, 0b00100, 0b01110, 0b01110, 0b01110, 0b00100, 0b11111, 0b01110}; // 🍒
byte lemon[8] = {0b01110, 0b10001, 0b10001, 0b11111, 0b11111, 0b10001, 0b01110, 0b00000}; // 🍋
byte seven[8] = {0b11111, 0b00001, 0b00010, 0b00100, 0b01000, 0b01000, 0b01000, 0b01000}; // 7
byte money[8] = {0b00100, 0b01110, 0b10100, 0b01110, 0b00101, 0b01110, 0b00100, 0b00000}; // 💰
byte bell[8] = {0b00100, 0b01110, 0b01110, 0b01110, 0b11111, 0b00100, 0b01110, 0b00000}; // 🔔
byte* symbols[] = {cherry, lemon, seven, money, bell};
// To store the randomly generated symbols for the current round
int results[3];
// Setup
void setup() {
// Initialize the LCD
lcd.begin(16, 2); // LCD with 16 columns and 2 rows
lcd.print("Slot Maschine");
lcd.setCursor(0, 1);
lcd.print("Press Button!!");
// Save custom characters to the LCD
lcd.createChar(0, cherry);
lcd.createChar(1, lemon);
lcd.createChar(2, seven);
lcd.createChar(3, money);
lcd.createChar(4, bell);
// Configure Button and LEDs
pinMode(buttonPin, INPUT_PULLUP);
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT); // Set yellow LED pin as output
}
// Function to display random slot machine symbols
void spinSlotMachine() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Result:");
// Generate 3 random symbols and store them in the 'results' array
for (int i = 0; i < 3; i++) {
results[i] = random(0, 5); // Random index for symbols
lcd.setCursor(i * 5, 1); // Display symbols on the second row
lcd.write(results[i]); // Show custom symbols
}
}
// Function to check for win
bool checkForWin() {
// Check if the first two symbols are the same
if (results[0] == results[1]) {
// If the third symbol is also the same, it's a 3-symbol win
if (results[1] == results[2]) {
return true; // 3 identical symbols
}
return true; // 2 identical symbols
}
return false; // No win
}
// Function to blink LEDs
void blinkLed(int ledPin, int times, int delayTime) {
for (int i = 0; i < times; i++) {
digitalWrite(ledPin, HIGH);
delay(250); // LED on
digitalWrite(ledPin, LOW);
delay(250); // LED off
}
delay(delayTime); // Wait time after blinking
}
// Function to turn on the yellow LED for a specified time
void turnOnYellowLed(int duration) {
digitalWrite(yellowLedPin, HIGH);
delay(duration); // Keep LED on for the specified duration
digitalWrite(yellowLedPin, LOW);
}
// Loop
void loop() {
buttonState = digitalRead(buttonPin);
// If the button is pressed
if (buttonState == LOW) {
// Spin the slot machine
spinSlotMachine();
// Check if it's a win or a loss
bool win = checkForWin();
if (results[0] == results[1] && results[1] == results[2]) {
// 3 identical symbols: Turn on the yellow LED
turnOnYellowLed(5000); // Light up for 5 second
} else if (win) {
// 2 identical symbols: Blink the green LED
digitalWrite(greenLedPin, HIGH);
blinkLed(greenLedPin, 4, 0); // Blink 4 times
digitalWrite(greenLedPin, LOW);
} else {
// Loss: Blink the red LED
blinkLed(redLedPin, 4, 1000); // Blink 4 times and wait 1 second
}
// Wait a second to prevent multiple presses
delay(1000);
}
}