#include "MD_MAX72xx.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11,12);
const int squareButton = A0;
int triButton = A1;
int circButton = A2;
const int isPushed = 0;
const int redLed = 3;
#define CS_PIN 53
#define CLK_PIN 52
#define DATA_PIN 51
int lvl = 0;
int score = 0;
float time;
float count;
boolean screenImage[4][8][8] = {
//Nothing Screen 0
{{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}},
//Screen 1 square
{{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 1, 1, 0, 0},
{0, 0, 1, 0, 0, 1, 0, 0},
{0, 0, 1, 0, 0, 1, 0, 0},
{0, 0, 1, 1, 1, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}},
//Screen 2 triangle
{{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 1, 0, 1, 0, 0, 0},
{0, 1, 0, 0, 0, 1, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0}},
// Screen 3 circle
{{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 1, 1, 0, 0},
{0, 1, 0, 0, 0, 0, 1, 0},
{0, 1, 0, 0, 0, 0, 1, 0},
{0, 1, 0, 0, 0, 0, 1, 0},
{0, 1, 0, 0, 0, 0, 1, 0},
{0, 0, 1, 1, 1, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}},
};
MD_MAX72XX ledMatrix = MD_MAX72XX(MD_MAX72XX::PAROLA_HW, CS_PIN, 53);
long randNum;
void setup() {
// put your setup code here, to run once:
ledMatrix.begin();
Serial.begin(9600);
pinMode(squareButton, INPUT_PULLUP);
pinMode(triButton, INPUT_PULLUP);
pinMode(circButton, INPUT_PULLUP);
pinMode(redLed, OUTPUT);
randomSeed(analogRead(A0));
lcd.begin(16, 2);
}
void loop() {
if(lvl == 0){
score = 0;
lcd.print("Welcome");
delay(5000);
lcd.print("Ready...");
delay(3000);
lcd.print("Set...");
delay(3000);
lcd.print("GO!");
delay(2000);
lvl++ ;
}
if(lvl == 1){
lcd.print("Score: ");
delay(1500);
lcd.print(score);
delay(3000);
lcd.print("Level 1");
delay(2000);
for(int x = 0; x <= 7; x++){
randNum = random(1, 4);
imageSelect(randNum);
time = millis();
checkFail(1.5);
imageSelect(0);
delay(250);
}
lvl ++;
}
if (lvl == 2){
lcd.print("Score: ");
delay(1500);
lcd.print(score);
delay(3000);
lcd.print("Level 2");
delay(2000);
for(int x = 0; x <= 9; x++){
randNum = random(1, 4);
imageSelect(randNum);
time = millis();
checkFail(1.25);
imageSelect(0);
delay(200);
}
lvl ++;
}
if (lvl == 3){
lcd.print("Score: ");
delay(1500);
lcd.print(score);
delay(3000);
lcd.print("Level 3");
delay(2000);
for(int x = 0; x <= 11; x++){
randNum = random(1, 4);
imageSelect(randNum);
time = millis();
checkFail(1);
imageSelect(0);
delay(150);
}
lvl ++;
}
if (lvl == 4){
lcd.print("Score: ");
delay(1500);
lcd.print(score);
delay(3000);
lcd.print("Level 4");
delay(2000);
for(int x = 0; x <= 13; x++){
randNum = random(1, 4);
imageSelect(randNum);
time = millis();
checkFail(0.8);
imageSelect(0);
delay(100);
}
lvl ++;
}
}
void imageSelect(int x){
for (int row = 0; row <= 7; row ++) {
for (int col = 0; col <= 7; col ++){
ledMatrix.setPoint(row, col, screenImage[x][row][7 - col]);
}
}
}
void printScore(){
do{
lcd.print(score);
}while(true);
}
void checkFail(float x){
do{
count = (millis() - time)/1000;
if((randNum == 1) && ((digitalRead(triButton) == isPushed) || (digitalRead(circButton) == isPushed))){
lcd.print("Final score: ");
delay(3000);
lcd.print(score);
delay(5000);
do{
lcd.print("Please Reset");
}while(true);
}
if((randNum == 2) && ((digitalRead(squareButton) == isPushed) || (digitalRead(circButton) == isPushed))){
lcd.print("Final score: ");
delay(3000);
lcd.print(score);
delay(5000);
do{
lcd.print("Please Reset");
}while(true);
}
if ((randNum == 3) && ((digitalRead(triButton) == isPushed) || (digitalRead(squareButton) == isPushed))){
lcd.print("Final score: ");
delay(3000);
lcd.print(score);
delay(5000);
do{
lcd.print("Please Reset");
}while(true);
}
if((randNum == 1) && (digitalRead(squareButton) == isPushed)){
score++;
}
if((randNum == 2) && (digitalRead(triButton) == isPushed)){
score++;
}
if((randNum == 3) && (digitalRead(circButton) == isPushed)){
score++;
}
}while(count < x);
}