/*跑馬燈效果及順序:
改成這樣比較順
右進 Who are you?
右進 Don't hurt me
閃現 H E L P
閃現 Press it!
右進 Eternally Greatful (救活)
閃現 x_x(死亡)*/
// button flag, end,run code
// put your setup code here, to run once:
#define TRIGGER_PIN 7
#define ECHO_PIN 6
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
unsigned long time_now = 0;
const int flashDuration = 1000; // Duration of each flash in milliseconds
int flashCount = 0;
const int buttonPin = 3;
// Variables for button state
int buttonState = HIGH; // Assume the button is not pressed initially
int lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 10;
boolean isFirstTime = true; // Flag to track the first button press
// Variables for game state
boolean gameStarted = false;
int successThreshold;
int failureThreshold;
int successCount = 0;
int failureCount = 0;
// Variable for button press count
int buttonPressCount = -2;
void setup() {
time_now = millis();
//Ultrasonic sensor
Serial.begin(9600);
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
//button
pinMode(3, INPUT);
// Initialize random seed
randomSeed(analogRead(0)); // Use an analog pin for better randomness
//I2C 16x02 lcd
lcd.init();
lcd.backlight();
}
void loop() {
// sensor get distance
int distance = getDistance();
Serial.println(distance);
//count the press
int reading = digitalRead(buttonPin);
// Perform debouncing
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
// Increment the button press count when the button is pressed
if (buttonState == LOW) {
buttonPressCount++;
lcdCondition();
}
}
}
// Save the current button state for the next iteration
lastButtonState = reading;
}
void lcdCondition(){
if (buttonPressCount <= -1) {
displayBasedOnDistance();
}
else if (buttonPressCount > -1){
//game starts with showing number, at buttonPressCount == 2 display other words
//if distance else
if (buttonPressCount == 0){
initializeGame();
}
else if (buttonPressCount > 0) {
handleGameEvent();
}
}
}
void displayBasedOnDistance(){
if (getDistance() <= 35) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Press the button");
lcd.setCursor(0,1);
lcd.print("to save me...");
}
else if (getDistance() > 35 && getDistance() <= 45) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("HELP, I'm dying XAX");
}
else if (getDistance() > 45 && getDistance() <=54){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Don't hurt me");
}
else {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Who are you?");
}
}
//謝瀞瑩
int getDistance() {
// Function to get distance from ultrasonic sensor
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
return pulseIn(ECHO_PIN, HIGH) * 0.034 / 2; // Convert pulse duration to centimeters
}
void initializeGame() {
// Perform game initialization tasks
successThreshold = random(1,10); // Adjust the range for success threshold
failureThreshold = random(1,10); // Adjust the range for failure threshold
successCount = 0;
failureCount = 0;
// Display initialization information on the LCD, if needed
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Success: " + String(successThreshold));
lcd.setCursor(0, 1);
lcd.print("Failure: " + String(failureThreshold));
}
void handleGameEvent() {
// Perform actions based on the outcome of the random event
bool success = random(0, 2); // 50% chance of success
if (success) {
successCount++;
// Display success message on the LCD, if needed
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("YES");
lcd.print(successCount);
} else {
failureCount++;
// Display failure message on the LCD, if needed
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("NO");
lcd.print(failureCount);
}
// Check for game end conditions
if (successCount >= successThreshold || failureCount >= failureThreshold) {
endGame();
gameStarted = false; // Reset the game state for the next round
}
// Additional actions for servo, MP3 player, and other components
}
void endGame() {
// Perform actions when the game reaches its end
if (successCount >= successThreshold) {
// Actions for a successful game end
// This is where you can trigger servo, MP3 player, etc.
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Success: " + String(successCount));
lcd.setCursor(0, 1);
lcd.print("You WIN!");
}
else {
// Actions for a failed game end
// This is where you can trigger different actions
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Failure: " + String(failureCount));
lcd.setCursor(0, 1);
lcd.print("You LOSE!");
}
// Additional actions for LCD, vibration motor, etc.
}
/* if button is not pressed, continue display
void loop{the display void}
if button is pressed, use game's distance to define
if distance < 50;
play game
if distance > 50. end game.
*/