#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(51, 52, 53, 1);
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 = 0;
int missCount = 0;
unsigned long maxTime;
bool miss;
// {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+9)) {
lcd.clear();
delay(300);
playsong();
songPos = 0;
score = 0;
combo = 0;
missCount = -1;
break;
}
}
}
void playsong() {
while (sizeof(song) > songPos) {
if (digitalRead(7)) { //RESET
break;
}
if (millis()%songSpeed < 10) {
for (int i=0; i < 4; i++) {
if (!pointGiven[i] && (song[songPos-1]/laneCheck[i] % 2 != 0)) {
miss = true;
Serial.println("miss did not press");
missCount += 1;
combo = 0;
lcd.setCursor(6, 0);
lcd.print(" ");
}
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+9);
if (!pointGiven[i]) {
if (song[songPos-1]/laneCheck[i] % 2 == 1 && song[songPos-1] != B00000000) {
if (readButton) {
if (millis() > maxTime-50) { // Late Timing
Serial.print("point! Late\n");
score += scoreList[0];
pointGiven[i] = true;
combo++;
} else if (millis() > (maxTime-songSpeed)+100) { // Perfect Timing
Serial.print("point! Perfect\n");
score += scoreList[2];
pointGiven[i] = true;
combo++;
} else { // Early Timing
Serial.print("point! Early\n");
score += scoreList[1];
pointGiven[i] = true;
combo++;
}
}
} else if (readButton) {
Serial.println("miss out of bounds");
pointGiven[i] = true;
missCount += 1;
combo = 0;
lcd.setCursor(6, 0);
lcd.print(" ");
}
}
}
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);
lcd.clear();
}