Created
August 24, 2015 13:45
-
-
Save skylying/54b042a8de689895da40 to your computer and use it in GitHub Desktop.
Arduino DDR sharing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Drum Drum Revolution | |
* Author Tim Lin for Arduino sharing @ Trend micro | |
*/ | |
#include <Wire.h> | |
#include <LiquidCrystal_I2C.h> | |
LiquidCrystal_I2C lcd(0x27,16,2); | |
// LED variables | |
int LED = 13; | |
// Digital sound input pin and value | |
int digitalPin = 7; | |
int soundVal; | |
// Variables to filter digital input in too short time interval | |
long prevMills = 0; | |
long FInterval = 70; | |
bool FisBusy = false; | |
/** ======== Game Settings ======== **/ | |
// Difficulty settings | |
long QInterval = 1000; // Set this to 2000 or 3000 above if game is too hard | |
int maxDrumCount = 7; // Set this to 6 or 5 if game is too hard | |
int roundCount = 10; | |
// DO NOT CHANGE | |
int gameStatus = 100; | |
long QTimer = 0; | |
bool QWaiting = false; | |
int playerDrumCount = 0; | |
int targetDrumCount = 0; | |
bool isPerfect = true; | |
void setup() | |
{ | |
lcd.init(); // initialize the lcd | |
// Print a message to the LCD. | |
lcd.backlight(); | |
lcd.setCursor(3,0); | |
lcd.print("Welcome to"); | |
lcd.setCursor(5,1); | |
lcd.print("Drum it"); | |
delay(4000); | |
lcd.clear(); | |
lcd.print("Drum to start...."); | |
// LED setup | |
pinMode(LED, OUTPUT); | |
pinMode(digitalPin, INPUT); | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
soundVal = digitalRead(digitalPin); | |
//Serial.println(soundVal); // For debug | |
if (gameStatus == 100) { | |
if (soundVal == HIGH) { | |
startGame(); | |
} | |
} else if (gameStatus == 500) { | |
if (!QWaiting) { | |
lcd.clear(); | |
createQ(); // 印出題目 | |
QWaiting = true; | |
QTimer = millis(); // 開始計時 | |
} else { | |
// Game over | |
if (roundCount == 0) { | |
if (isPerfect == false) { | |
lcd.clear(); | |
lcd.print("Game over...."); | |
lcd.setCursor(0,1); | |
lcd.print("Drum to cont..."); | |
} else { | |
lcd.clear(); | |
lcd.print("Congratulations!!"); | |
lcd.setCursor(0,1); | |
lcd.print("Drum to cont..."); | |
} | |
gameStatus = 100; | |
roundCount = 10; | |
QWaiting = false; | |
} else { | |
// 你敲了幾下 | |
if (soundVal == HIGH) { | |
if (FisBusy == false) { | |
FisBusy = true; | |
delay(FInterval); | |
playerDrumCount++; | |
FisBusy = false; | |
} | |
} | |
// 敲太多下 = 失敗, 超過 2 秒 = 失敗 | |
if (millis() - QTimer < QInterval ) { | |
if (playerDrumCount > targetDrumCount) { | |
getRoundResult(false); | |
} | |
} else { | |
if (playerDrumCount == targetDrumCount) { | |
getRoundResult(true); | |
} else { | |
getRoundResult(false); | |
} | |
} | |
} | |
} | |
} | |
} | |
/** | |
* Method to print count down strings on LCD | |
*/ | |
void startGame() | |
{ | |
lcd.clear(); | |
lcd.print("3"); | |
delay(1000); | |
lcd.clear(); | |
lcd.print("2"); | |
delay(1000); | |
lcd.clear(); | |
lcd.print("1"); | |
delay(1000); | |
lcd.clear(); | |
lcd.print("GO!"); | |
delay(1000); | |
lcd.clear(); | |
gameStatus = 500; | |
} | |
/** | |
* Method to create question string and pring out on LCD | |
*/ | |
void createQ() | |
{ | |
int ran = random(2,maxDrumCount); | |
String QString = "O"; | |
targetDrumCount = ran; | |
for (int i = 1; i < ran; i++) { | |
QString += "-O"; | |
} | |
//Serial.println(QString); // for debug | |
lcd.print(QString); | |
} | |
/** | |
* When each round is finished, defind player success or not | |
*/ | |
void getRoundResult(bool isSuccess) { | |
// Pring lcd | |
lcd.clear(); | |
if (isSuccess) { | |
lcd.print("Good!!"); | |
} else { | |
lcd.print("Fail ~ T.T"); | |
isPerfect = false; | |
} | |
// Reset drum count | |
playerDrumCount = 0; | |
targetDrumCount = 0; | |
delay(1000); | |
// Set timer to waiting mode | |
QWaiting = false; | |
// Count ramain rounds | |
roundCount--; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment