#include "LedControl.h"
LedControl lc=LedControl(11,13,10,4); // Pins: DIN,CLK,CS, # of Display connected
const int btnPinQuad4 = 5;
const int btnPinQuad3 = 4;
const int btnPinQuad2 = 7;
const int btnPinQuad1 = 6;
// starting parts randomized (this is done becuase it could give players a hint on how to solve this puzzle)
int countQuad4 = 0;
int countQuad3 = 3;
int countQuad2 = 1;
int countQuad1 = 2;
// number of symbols minus 2(because it starts at zero and needs to change when the counter is about to exceed the limit)
int countMax = 2;
// starting state of the button is always off
int ButtonStateQ4 = 0;
int ButtonStateQ3 = 0;
int ButtonStateQ2 = 0;
int ButtonStateQ1 = 0;
// LEDs that tell the player if the puzzle is finished or not
int r = 8;
int g = 9;
// this is the answer randomized to make it more interresting for players who play it a second time
// may be changed to standard answer to make it easier to give fixed hints
// int answer = random(0,3);
int answer = 2;
// checks if the banana plug is still plugged in while the puzzle isn't finished
int banana = A5;
// variable that turns speaker on
int speaker = 2;
// checks if the puzzle before is finished
int onSwitchIn = A4;
// checks if the puzzle before is finished
int onSwitchOut = A3;
// puzzle will be activated by this variable
int onSwitch = 0;
// state that tells if the puzzle is completed or not
int PuzzleState = 0;
unsigned long delayTime = 100; // Delay between button presses
// Put values in arrays in array to up the index and cycle through them
byte patient0[] =
{
// patient Z3R0 template per quadrant
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000
};
// test 4 byte array
byte quadrant1[4][8] =
{
// quadrant
{
B00011000,
B00001100,
B00001100,
B00000110,
B00000110,
B00000011,
B11111111,
B11111111
},
{
B10000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B11111110,
B11111111
},
{
B00000110,
B00000110,
B00011110,
B00111110,
B00111110,
B00011100,
B00000000,
B00000000
},
{
B00000011,
B00000110,
B00001100,
B00011000,
B00110000,
B01100000,
B11000000,
B10000000
}
};
byte quadrant2[4][8] =
{
// quadrant 2, bottom left of the matrix
{
B00011000,
B00110000,
B00110000,
B01100000,
B01100000,
B11000000,
B11111111,
B11111111
},
{
B00000001,
B00000011,
B00000110,
B00001100,
B00011000,
B00110000,
B01111111,
B11111111
},
{
B00011000,
B00011000,
B00011000,
B00011000,
B01111000,
B11111000,
B11111000,
B01110000
},
{
B11000000,
B01100000,
B00110000,
B00011000,
B00001100,
B00000110,
B00000011,
B00000001
}
};
byte quadrant3[4][8] =
{
// quadrant 3, top right of the matrix
{
B10000000,
B10000000,
B11000000,
B01100000,
B01100000,
B00110000,
B00110000,
B00011000
},
{
B11111111,
B11111110,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B10000000
},
{
B00000000,
B00011110,
B11111110,
B11000110,
B00000110,
B00000110,
B00000110,
B00000110
},
{
B00011000,
B00111100,
B01100110,
B11000011,
B10000011,
B00000011,
B00000011,
B00000011
}
};
byte quadrant4[4][8] =
{
// quadrant 4, top left of the matrix
{
B00000001,
B00000001,
B00000011,
B00000110,
B00000110,
B00001100,
B00001100,
B00011000
},
{
B11111111,
B01111111,
B00110000,
B00011000,
B00001100,
B00000110,
B00000011,
B00000001
},
{
B00000000,
B00000000,
B00000000,
B00000011,
B00011111,
B00011100,
B00011000,
B00011000
},
{
B00011000,
B00111100,
B01100110,
B11000011,
B11000001,
B11000000,
B11000000,
B11000000
}
};
void setup()
{
lc.shutdown(0,false); // Wake up displays
lc.shutdown(1,false);
lc.shutdown(2,false);
lc.shutdown(3,false);
lc.setIntensity(0,0.5); // Set intensity levels
lc.setIntensity(1,0.5);
lc.setIntensity(2,0.5);
lc.setIntensity(3,0.5);
lc.clearDisplay(0); // Clear Displays
lc.clearDisplay(1);
lc.clearDisplay(2);
lc.clearDisplay(3);
pinMode(btnPinQuad1, INPUT);
pinMode(btnPinQuad2, INPUT);
pinMode(btnPinQuad3, INPUT);
pinMode(btnPinQuad4, INPUT);
pinMode(onSwitchIn, INPUT);
pinMode(banana, INPUT);
pinMode(onSwitchIn, OUTPUT);
pinMode(speaker, OUTPUT);
pinMode(r, OUTPUT);
pinMode(g, OUTPUT);
}
// Take values in Arrays and Display them with parameters to cycle through each symbol part
void disQuadrant1(byte *quad)
{
for (int i = 0; i < 8; i++)
{
lc.setRow(1,i,quad[i]);
}
}
void disQuadrant2(byte *quad)
{
for (int i = 0; i < 8; i++)
{
lc.setRow(0,i,quad[i]);
}
}
void disQuadrant3(byte *quad)
{
for (int i = 0; i < 8; i++)
{
lc.setRow(3,i,quad[i]);
}
}
void disQuadrant4(byte *quad)
{
for (int i = 0; i < 8; i++)
{
lc.setRow(2,i,quad[i]);
}
}
void loop()
{
if (onSwitch == 1023){
if (countQuad1 == answer && countQuad2 == answer && countQuad3 == answer && countQuad4 == answer) {
if (PuzzleState){
disQuadrant1(patient0);
disQuadrant2(patient0);
disQuadrant3(patient0);
disQuadrant4(patient0);
analogWrite(onSwitchOut, 255);
} else {
digitalWrite(r, LOW);
digitalWrite(g, HIGH);
for (int i = 0; i < 3; i++) {
disQuadrant1(patient0);
disQuadrant2(patient0);
disQuadrant3(patient0);
disQuadrant4(patient0);
delay(300);
disQuadrant1(quadrant1[countQuad1]);
disQuadrant2(quadrant2[countQuad3]);
disQuadrant3(quadrant3[countQuad3]);
disQuadrant4(quadrant4[countQuad4]);
delay(300);
}
PuzzleState = 1;
}
} else {
if (analogRead(banana) == 1023){
digitalWrite(speaker, HIGH);
} else {
digitalWrite(r, HIGH);
digitalWrite(g, LOW);
// ButtonState setup
ButtonStateQ4 = digitalRead(btnPinQuad4);
ButtonStateQ3 = digitalRead(btnPinQuad3);
ButtonStateQ2 = digitalRead(btnPinQuad2);
ButtonStateQ1 = digitalRead(btnPinQuad1);
// Put #1 frame on both Display
if (ButtonStateQ1 == HIGH) {
if (countQuad1 <= countMax){
countQuad1++;
} else {
countQuad1 = 0;
}
}
disQuadrant1(quadrant1[countQuad1]);
// Put #2 frame on both Display
if (ButtonStateQ2 == HIGH) {
if (countQuad2 <= countMax){
countQuad2++;
} else {
countQuad2 = 0;
}
}
disQuadrant2(quadrant2[countQuad2]);
// Put #3 frame on both Display
if (ButtonStateQ3 == HIGH) {
if (countQuad3 <= countMax){
countQuad3++;
} else {
countQuad3 = 0;
}
}
disQuadrant3(quadrant3[countQuad3]);
// Put #4 frame on both Display
if (ButtonStateQ4 == HIGH) {
if (countQuad4 <= countMax){
countQuad4++;
} else {
countQuad4 = 0;
}
}
disQuadrant4(quadrant4[countQuad4]);
delay(delayTime);
}
}
} else {
onSwitch = analogRead(onSwitchIn);
}
}