#include "LedControl.h"
#include "binary.h"
#include <LiquidCrystal.h>
#include "song.h"
//Setting up pins for the LCD
const int rs = 23, en = 25, d4 = 33, d5 = 35, d6 = 37, d7 = 39;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//Setting up pins for DOT MARTIX
LedControl lc = LedControl(4, 3, 2, 2);
void setup() {
lc.shutdown(0, false);
lc.setIntensity(0, 100);
lc.clearDisplay(0);
pinMode(8, INPUT);
Serial.begin(9600);
lcd.begin(16, 2);
}
// Setup the variables for the game
int songPos = 0;
int songSpeed = 400;
int score = 0;
int combo;
int missCount = -1;
unsigned long maxTime;
// {Early, Perfect, Late}
int scoreList[] = {100, 300, 100};
bool pointGiven[4];
byte readButton;
byte laneCheck[] {
B11000000, B00110000, B00001100, B00000011
};
void loop() {
lcd.setCursor(0, 0);
lcd.print("Welcome To DDR");
lcd.setCursor(0,1);
lcd.print("Press Any Button")
for (int i = 0; i < 4; i++) {
if (digitalRead(i+8)) {
lcd.clear();
playsong();
}
}
}
void playsong() {
while (sizeof(song) > songPos) {
if (digitalRead(7)) { //RESET
break;
}
if (millis()%songSpeed < 10) {
for (int i=0; i < 4; i++) pointGiven[i] = false;
lc.clearDisplay(0);
for (int i = 0; i < 16; i++) lc.setColumn(1-i/8,7-i%8, song[songPos + i]);
songPos++;
maxTime = millis() + songSpeed;
}
for (int i= 0; i < 4; i++) {
readButton = digitalRead(i+8);
if (!pointGiven[i]) {
if (song[songPos-1]/laneCheck[i] % 2 == 1) {
if (readButton) {
if (millis() < (maxTime-songSpeed) + 10) { // Early Timing
Serial.print("point! Early\n");
score += scoreList[0];
pointGiven[i] = true;
combo++;
} else if (millis() > maxTime-(songSpeed/1.6)) { // Late Timing
Serial.print("point! Late\n");
score += scoreList[2];
pointGiven[i] = true;
combo++;
} else { // Perfect Timing
Serial.print("point! Perfect\n");
score += scoreList[1];
pointGiven[i] = true;
combo++;
}
} else {
Serial.println("miss");
pointGiven[i] = true;
missCount += 1;
combo = 0;
}
} else if (readButton) {
Serial.println("miss");
pointGiven[i] = true;
missCount += 1;
combo = 0;
}
}
}
lcd.setCursor(0, 0);
lcd.print("Combo:");
lcd.setCursor(6, 0);
lcd.print(combo);
lcd.setCursor(0, 1);
lcd.print("Score:");
lcd.setCursor(6, 1);
lcd.print(score);
lcd.setCursor(12,0);
lcd.print("Miss");
lcd.setCursor(12,1);
lcd.print(missCount);
}
lc.clearDisplay(0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("RESET");
delay(300);
songPos, songSpeed, score, combo, maxTime = 0;
missCount = -1;
lcd.clear();
}