#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;
unsigned long maxTime;
// {Early, Perfect, Late}
int scoreList[] = {100, 300, 100};
bool pointGiven[4];
byte readButton;
byte laneCheck[] {
B11000000, B00110000, B00001100, B00000011
};
void loop() {
while (sizeof(song) > songPos) {
if (millis()%songSpeed < 10) {
for (int i=0; i < 4; i++) {
pointGiven[i] = 0;
}
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 (readButton && song[songPos-1]/laneCheck[i] % 2 == 1 && !pointGiven[i]) {
// MISS (too early
// Early (1 pts)
if (millis() < (maxTime-songSpeed) + 25) {
Serial.print("point! Early\n");
score += scoreList[0];
pointGiven[i] = 1;
combo++;
}
// Late (1 pts)
else if (millis() > maxTime-(songSpeed/1.8)) {
Serial.print("point! Late\n");
score += scoreList[2];
pointGiven[i] = 1;
combo++;
}
// Perfect (3 pts)
else {
Serial.print("point! Perfect\n");
score += scoreList[1];
pointGiven[i] = 1;
combo++;
}
} else if (!readButton && song[songPos-1]/laneCheck[i] % 2 == 1 && !pointGiven[i]){
Serial.print("MISS OUT\n");
missCount += 1;
pointGiven[i] = 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);
Serial.println(score);
}