#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <Stepper.h>
#include <TimeLib.h>
const int buttonFR = 53;
const int buttonBK = 51;
const int buttonLH = 49;
const int buttonRH = 47;
const int buttonUP = 45;
const int buttonStepperRPMspeed = 43;
const int buttonCoin = 45;
const int buttonStart = 47;
const int servoPower = 8;
const int ledPin = 12;
const int ledPinCoin = 11;
// setup 3 speeds for the stepper motors
int StepperRPM = 60;
int StepperRPMFast = 90;
int StepperRPMSlow = 60;
// create a state of LED and turn it off
int ledStateStart = LOW;
int ledStateCoin = LOW;
// create a state for each button
int buttonStateFR = HIGH;
int buttonStateBK = HIGH;
int buttonStateLH = HIGH;
int buttonStateRH = HIGH;
int buttonStateUP = HIGH;
int buttonStateDN = HIGH;
int buttonStateSPEED = HIGH;
int buttonStateCOIN = HIGH;
int buttonStateSTART = HIGH;
// usually we use int for variables but sometimes the value for the variable can be very long, in such case we need to use (long)
long intervalStart = 500;
long intervalIdle = 2000;
long previousMillis = 0;
// coordination variables
int x = 0;
int y = 0;
// stepper setup
const int stepsPerRevolution = 200;
Stepper Xstepper(stepsPerRevolution, 30, 32, 34, 36);
Stepper Ystepper(stepsPerRevolution, 22, 24, 26, 28);
Stepper Zstepper(stepsPerRevolution, 38, 40, 42, 44);
//setup the servo and the analog POT
Servo myServo;
int potPin = 5;
//timing
unsigned int timelimit = 5;
//setup the LED
LiquidCrystal_12C LCD = LiquidCrystal_12C(0x27, 20, 4);
void setup() {
// put your setup code here, to run once:
LCD.begin(20, 4)
pinMode(buttonFR, INPUT_PULLUP);
pinMode(buttonBK, INPUT_PULLUP);
pinMode(buttonLH, INPUT_PULLUP);
pinMode(buttonRH, INPUT_PULLUP);
pinMode(buttonUP, INPUT_PULLUP);
pinMode(buttonDN, INPUT_PULLUP);
pinMode(buttonStepperRPMSpeed, INPUT_PULLUP);
pinMode(buttonCoin, INPUT_PULLUP);
pinMode(buttonStart, INPUT_PULLUP);
pinMode(servoPower, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPinCoin, OUTPUT);
//set all pins to LOW
digitalWrite(buttonFR, LOW)
digitalWrite(buttonBK, LOW)
digitalWrite(buttonLH, LOW)
digitalWrite(buttonRH, LOW)
digitalWrite(buttonUP, LOW)
digitalWrite(buttonDN, LOW)
digitalWrite(buttonStepperRPMSpeed, LOW)
digitalWrite(buttonCoin, LOW)
digitalWrite(buttonStart, LOW)
digitalWrite(servoPower, LOW)
digitalWrite(ledPin, LOW)
digitalWrite(ledPinCoin, LOW)
// assign pin name for the servo
myServo.attach(13);
}
void loop() {
// game idle (waiting state)
buttonStateCoin = digitalRead(buttonCoin);
unsigned long currentMillis = millis(); // used for refreshing the game states
if (currentMillis - previousMillis > intervalIdle){
previousMillis = currentMillis;
if (y == 0){
y = y+1;
x = x+1;
lcd.clear();
lcd.setCursor(5, y);
lcd.print("GameOver");
lcd.setCursor(0, y+1);
lcd.print("Insert a coin to play");
lcd.setCursor(0, y+1);
}
else{
lcd.clear();
lcd.setCursor(5, y+1);
lcd.print("GameOver");
lcd.setCursor(0, y);
lcd.print("Insert a coin to play");
lcd.setCursor(0, 3);
y = y=1;
x = x=1;
}
}
if(second() == timelimit){
lcd.clear();
lcd.setCursor(2, 1);
lcd.print("Time Expired");
myServo.write(0); // will release the claw
delay(500);
digitalWrite(servoPower, LOW); // turn off the servo power
}
// when coin been inserted
if(buttonStateCoin == LOW){ // this means a coin has been inserted
lcd.clear();
do {
unsigned long currentMillis = millis();
lcd.setCursor(2,0);
lcd.print("coin inserted");
lcd.setCursor(1, 1);
lcd.print("Push Start to play");
lcd.setCursor(5, 2);
lcd.print("Good Luck");
}
if (currentMillis - previousMillis > intervalIdle){
previousMillis = currentMillis;
if(ledStateStart == LOW){
ledStateStart = HIGH;
}
else{
ledStateStart = LOW;
digitalWrite(ledPin = ledStateState);
}
}
buttonStateStart = digitalWrite(buttonStart); // wait for player to push start
}
while(buttonStateStart == HIGH);
runGame(); // exit the loop and call custom function RUNGAME
}