Smaller screens (1100px<) might have website design issues.
The slot machine requires users to "spin" by using a joystick, triggering the generation of three random numbers displayed on an LCD screen. If all three numbers match, a buzzer plays a sound, and a servo motor opens a small box containing coins.
// LIBRARIES
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// DIGITAL PINS
#define LIGHTS_PIN 12
#define BUZZER_PIN 11
#define SERVO_PIN 3
// ANALOG PINS
#define X_PIN 1
#define Y_PIN 0
// OBJECTS
Servo myServo;
LiquidCrystal_I2C lcd(0x27, 20, 4);
// VARIABLES AND THE SINGULAR ARRAY
int xCoord;
int yCoord;
int randNums[3];
boolean isJoystickPulled;
void setup() {
Serial.begin(9600);
randomSeed(analogRead(2)); // A2 is unconnected, good for randomness
pinMode(LIGHTS_PIN, OUTPUT);
myServo.attach(SERVO_PIN);
myServo.write(0);
lcd.init();
lcd.clear();
lcd.backlight();
displayPullToPlay();
}
void loop() {
xCoord = analogRead(X_PIN);
yCoord = analogRead(Y_PIN);
isJoystickPulled = (xCoord > 855 && xCoord < 1030) && (yCoord > 470 && yCoord < 610);
if (isJoystickPulled) {
// Generate random numbers
for (int i = 0; i < 3; i++) {
randNums[i] = random(1, 4); // random 1–3
//randNums[i] = 1;
}
// Print numbers, centered-ish
lcd.clear();
lcd.setCursor(4, 1); // approximate centering
for (int i = 0; i < 3; i++) {
lcd.print(randNums[i]);
lcd.print(" ");
}
delay(500);
// Win or lose?
if (randNums[0] == randNums[1] && randNums[1] == randNums[2]) {
lcd.setCursor(0, 3);
lcd.print(" YOU WIN! WOOOOOOO!");
tone(BUZZER_PIN, 1000, 1000); //play sound
//no text effect cus classic arduino delay problem: cant do lights and text effect same time
//lights
for (int i = 0; i < 4; i++) {
digitalWrite(LIGHTS_PIN, HIGH);
delay(300);
digitalWrite(LIGHTS_PIN, LOW);
delay(300);
}
//coins
myServo.write(25);
delay(1500);
myServo.write(0);
delay(3000); //pause
} else {
lcd.setCursor(0, 3);
lcd.print("you lost :( spin...");
tone(BUZZER_PIN, 300, 1000); //play lose sound
delay(2000); //pause
}
displayPullToPlay(); // Show pull message again
}
}
// METHOD(S)
void displayPullToPlay() {
lcd.clear();
lcd.setCursor(0, 1);
String message = " Pull to play :)";
for (int i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(90); //make longer?
}
}