// Including the required Arduino libraries
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <LedControl.h>
#include <LiquidCrystal_I2C.h>
// #include <string>
// https://www.instructables.com/MAX7219-LED-Dot-Matrix-Assembly-and-Testing/
// Uncomment according to your hardware type
// #define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define HARDWARE_TYPE MD_MAX72XX::GENERIC_HW
// // Defining size, and output pins
// #define MAX_DEVICES 1
// #define CS_PIN 3
int DIN = 11;
int CS = 3;
int CLK = 13;
// byte up[8] = {0x18,0x3c,0x7e,0xdb,0x99,0x18,0x18,0x18};
// byte down[8] = {0x18,0x18,0x18,0x99,0xdb,0x7e,0x3c,0x18};
// byte left[8] = {0x18,0x30,0x60,0xff,0xff,0x60,0x30,0x18};
// byte right[8] = {0x18,0x0c,0x06,0xff,0xff,0x06,0x0c,0x18};
// byte[4][8] directions = {up, down, left, right};
byte directions[4][8] = {{0x18, 0x3c, 0x7e, 0xdb, 0x99, 0x18, 0x18, 0x18},
{0x18, 0x18, 0x18, 0x99, 0xdb, 0x7e, 0x3c, 0x18},
{0x18, 0x0c, 0x06, 0xff, 0xff, 0x06, 0x0c, 0x18},
{0x18, 0x30, 0x60, 0xff, 0xff, 0x60, 0x30, 0x18}};
byte smile[8] = {0x00,0x24,0x24,0x24,0x81,0x42,0x3c,0x00};
byte frown[8] = {0x00,0x42,0x24,0x18,0x42,0x00,0x3c,0x42};
byte win[8] = {0x00,0x66,0xff,0xff,0xff,0x7e,0x3c,0x18}; // heart win condition
byte full[8] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
// byte win[8] = {0x18,0x18,0x3c,0xff,0x3c,0x3c,0x66,0xc3}; // star win condition
// byte win[8] = {0x18,0x18,0x7e,0xff,0x7e,0x7e,0x24,0xe7}; // man win condition
// byte win[8] = {0x00,0x66,0xff,0xff,0xff,0x7e,0x3c,0x18}; // heart win condition
String comments[] = {
"Last Chance...",
"Final Warning...",
"The End is near.",
"I see all.",
"You can't hide.",
"Don't look back.",
};
LedControl lc = LedControl(DIN,CLK,CS,0);
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
int pattern[99];
int level = 0;
int selection = 0;
bool pass = true;
// try with input.pullup instead
void setup(){
Serial.begin(9600);
lc.shutdown(0,false); //The MAX72XX is in power-saving mode on startup
lc.setIntensity(0,15); // Set the brightness to maximum value
lc.clearDisplay(0); // and clear the display
lcd.init(); // initialize the lcd
lcd.backlight();
pinMode(7, INPUT);
pinMode(6, INPUT);
pinMode(5, INPUT);
pinMode(4, INPUT);
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Defuse Me!"); // print message at (0, 0)
lcd.setCursor(2, 1); // move cursor to (2, 1)
lcd.print("Level: "); // print message at (2, 1)
lcd.print(level);
}
// TEST DIRECTION ARRAY
// for (int i=0; i<4; i++) {
// Serial.println(i);
// printByte(directions[i]);
// delay(600);
// }
// lc.clearDisplay(0);
void loop() {
Serial.println("running...");
pass = true;
pattern[level] = random(0, 3+1); // 0 to 3 are the arrows
level++;
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Defuse Me!"); // print message at (0, 0)
lcd.setCursor(2, 1); // move cursor to (2, 1)
lcd.print("Level: "); // print message at (2, 1)
lcd.print(level);
playPattern();
lc.clearDisplay(0);
for (int i=0; i<level; i++) {
selection = 5;
Serial.print("Selection: ");
Serial.println(selection);
while (selection == 5) {
selection = checkButtons();
}
Serial.print("Selection: ");
Serial.println(selection);
printByte(directions[selection]);
delay(300);
lc.clearDisplay(0);
if (selection != pattern[i]) {
gameOver();
break;
}
delay(400);
}
if (level == 84) { // highest simon score according to internet
gameWon();
}
if (pass) {
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("No-one Explodes!"); // print message at (0, 0)
lcd.setCursor(2, 1); // move cursor to (2, 1)
lcd.print("Level: "); // print message at (2, 1)
lcd.print(level+1);
printByte(smile);
delay(600);
}
Serial.print("level: ");
Serial.println(level);
}
int checkButtons() {
if (digitalRead(7) == HIGH) {
return 0;
}
else if (digitalRead(6) == HIGH) {
return 1;
}
else if (digitalRead(5) == HIGH) {
return 2;
}
else if (digitalRead(4) == HIGH) {
return 3;
}
else {
return 5;
}
}
void playPattern() {
for (int i=0; i<level; i++) {
Serial.print(pattern[i]);
printByte(directions[pattern[i]]);
delay(800);
lc.clearDisplay(0);
delay(200);
}
}
void gameOver() {
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print(comments[random(0, 9)]); // print message at (0, 0)
lcd.setCursor(2, 1); // move cursor to (2, 1)
lcd.print("Level: "); // print message at (2, 1)
lcd.print(level);
pass = false;
printByte(frown);
delay(1000);
level = 0;
}
void gameWon() {
pass = false;
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("YOU WIN!"); // print message at (0, 0)
lcd.setCursor(2, 1); // move cursor to (2, 1)
lcd.print("Level: "); // print message at (2, 1)
lcd.print(level);
// flicker win image for as long as there were levels
for (int i=0; i<level; i++) {
Serial.print(pattern[i]);
printByte(win);
delay(500);
lc.clearDisplay(0);
delay(500);
}
printByte(full);
level = 0;
// Satisfying reset screen:
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("RESETTING..."); // print message at (0, 0)
lcd.setCursor(2, 1); // move cursor to (2, 1)
lcd.print("Level: "); // print message at (2, 1)
lcd.print(level);
delay(2000);
lc.clearDisplay(0);
delay(100);
}
void printByte(byte character [])
{
int i = 0;
for(i=0;i<8;i++)
{
lc.setRow(0,i,character[i]);
}
}