#include <Keypad.h>
#include <FastLED.h>
//ini Keypad.
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'0', '1', '2', '3'},
{'4', '5', '6', '7'},
{'8', '9', 'a', 'b'},
{'c', 'd', 'e', 'f'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char Set[6][8] = {
{'0', '0', '9', 'f', '7', '5', '6', 'c'}, // first 0 is because SetSymbols cannot be 0
{'0', 'b', '2', 'e', '5', '9', 'd', 'f'},
{'0', '4', '0', '2', '8', 'a', '7', '3'},
{'0', 'd', 'e', '3', '1', '2', 'c', '0'},
{'0', '8', '7', '4', '9', 'e', 'f', '1'},
{'0', '3', '6', '1', 'b', 'c', '4', 'a'}
};
//ini FastLed
#define NUM_LEDS 17
#define DATA_PIN 10
CRGB leds[NUM_LEDS];
const int kBoardWidth = 4;
const int kBoardHeight = 4;
char First;
char Second;
char Third;
char Fourth;
int indexFi;
int indexS;
int indexT;
int indexFo;
void setup() {
Serial.begin(9600);
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
randomSeed(analogRead(0));
SymbolsSetup();
}
void loop() {
char key1 = NO_KEY;
char key2 = NO_KEY;
char key3 = NO_KEY;
char key4 = NO_KEY;
while (key1 == NO_KEY) {
key1 = keypad.getKey();
}
Serial.println("key 1 pressed");
Serial.println(key1);
Serial.println();
if (key1 != NO_KEY) {
if (key1 == First) {
leds[indexFi] = CRGB::Green;
FastLED.show();
} else {
Wrong();
}
}
while (key2 == NO_KEY) {
key2 = keypad.getKey();
}
Serial.println("key 2 pressed");
Serial.println(key2);
Serial.println();
if (key2 != NO_KEY) {
if (key2 == Second) {
leds[indexS] = CRGB::Green;
FastLED.show();
} else {
Wrong();
}
}
while (key3 == NO_KEY) {
key3 = keypad.getKey();
}
Serial.println("key 3 pressed");
Serial.println(key3);
Serial.println();
if (key3 != NO_KEY) {
if (key3 == Third) {
leds[indexT] = CRGB::Green;
FastLED.show();
} else {
Wrong();
}
}
while (key4 == NO_KEY) {
key4 = keypad.getKey();
}
Serial.println("key 4 pressed");
Serial.println(key4);
Serial.println();
if (key4 != NO_KEY) {
if (key4 == Fourth) {
leds[indexFo] = CRGB::Green;
FastLED.show();
} else {
Wrong();
}
}
while(1){
leds[16] = CRGB::Green;
FastLED.show();
}
}
void SymbolsSetup() {
int SetNumber = 0;
int SetSymbols[4] = {0, 0, 0, 0};
SetNumber = random(5);
Serial.println(SetNumber);
Serial.println();
while (SetSymbols[0] == 0) {
SetSymbols[0] = random(6);
}
while (SetSymbols[1] == 0 || SetSymbols[1] == SetSymbols[0]) {
SetSymbols[1] = random(6);
}
while (SetSymbols[2] == 0 || SetSymbols[2] == SetSymbols[1] || SetSymbols[2] == SetSymbols[0]) {
SetSymbols[2] = random(6);
}
while (SetSymbols[3] == 0 || SetSymbols[3] == SetSymbols[2] || SetSymbols[3] == SetSymbols[1] || SetSymbols[3] == SetSymbols[0]) {
SetSymbols[3] = random(6);
}
int temp; // temporary variable for swapping elements
// bubble sort algorithm to sort the array in ascending order
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4 - i - 1; j++) {
if (SetSymbols[j] > SetSymbols[j + 1]) {
// swap the elements using a temporary variable
temp = SetSymbols[j];
SetSymbols[j] = SetSymbols[j + 1];
SetSymbols[j + 1] = temp;
}
}
}
Serial.println(SetSymbols[0]);
Serial.println(SetSymbols[1]);
Serial.println(SetSymbols[2]);
Serial.println(SetSymbols[3]);
Serial.println();
First = Set[SetNumber][SetSymbols[0]];
Second = Set[SetNumber][SetSymbols[1]];
Third = Set[SetNumber][SetSymbols[2]];
Fourth = Set[SetNumber][SetSymbols[3]];
Serial.println(First);
Serial.println(Second);
Serial.println(Third);
Serial.println(Fourth);
Serial.println();
LightSymbols();
}
void LightSymbols() {
leds[16] = CRGB::Red; // status light in top left.
// Convert the characters to their corresponding integer values
indexFi = First - (isdigit(First) ? '0' : ('a' - 10));
indexS = Second - (isdigit(Second) ? '0' : ('a' - 10));
indexT = Third - (isdigit(Third) ? '0' : ('a' - 10));
indexFo = Fourth - (isdigit(Fourth) ? '0' : ('a' - 10));
Serial.println(indexFi);
Serial.println(indexS);
Serial.println(indexT);
Serial.println(indexFo);
Serial.println();
// Set the corresponding elements in the leds array to blue
leds[indexFi] = CRGB::Blue;
leds[indexS] = CRGB::Blue;
leds[indexT] = CRGB::Blue;
leds[indexFo] = CRGB::Blue;
// Call FastLED.show() to update the LED strip
FastLED.show();
}
void Wrong() {
for (int i = 0; i < 16; i++) {
leds[i] = CRGB::Red;
}
FastLED.show();
delay(200);
FastLED.clear();
FastLED.show();
delay(100);
for (int i = 0; i < 16; i++) {
leds[i] = CRGB::Red;
}
FastLED.show();
delay(200);
FastLED.clear();
LightSymbols();
loop();
}