168 lines
3.3 KiB
Plaintext
168 lines
3.3 KiB
Plaintext
|
#include <stdint.h>
|
||
|
#include <stddef.h>
|
||
|
#include <string.h>
|
||
|
#include <HardwareSerial.h>
|
||
|
#include <FastLED.h>
|
||
|
#include <Wire.h> // Library for I2C communication
|
||
|
#include <LiquidCrystal_I2C.h>
|
||
|
#include <Arduino.h>
|
||
|
#include <FastLED.h>
|
||
|
|
||
|
#define BTN_1 2
|
||
|
#define BTN_2 3
|
||
|
#define BTN_3 4
|
||
|
#define BTN_4 5
|
||
|
#define BTN_5 6
|
||
|
#define BTN_CHECK 7
|
||
|
#define DATA_PIN 8
|
||
|
#define NUM_LEDS 5
|
||
|
|
||
|
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
|
||
|
void greet_screen()
|
||
|
{
|
||
|
lcd.setCursor(2, 0); // Set the cursor on the third column and first row.
|
||
|
lcd.print("Escaperoom J1P2"); // Print the string "Hello World!"
|
||
|
lcd.setCursor(2, 1); // Set the cursor on the third column and the second row (counting starts at 0!).
|
||
|
lcd.print(" Led puzzle");
|
||
|
lcd.setCursor(2, 2);
|
||
|
lcd.print("by blonde");
|
||
|
}
|
||
|
|
||
|
void winner_screen()
|
||
|
{
|
||
|
lcd.setCursor(2, 0);
|
||
|
lcd.print("Congratulations!");
|
||
|
}
|
||
|
|
||
|
|
||
|
CRGB leds[NUM_LEDS];
|
||
|
|
||
|
int pw_state[NUM_LEDS] = {0, 0, 0, 0, 0};
|
||
|
int correct_pw[NUM_LEDS] = {1, 1, 1, 1, 1};
|
||
|
|
||
|
void generate_password()
|
||
|
{
|
||
|
for(int i = 0; i < NUM_LEDS; i++){
|
||
|
correct_pw[i] = random(0, 5);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void set_led_color(int led, int value){
|
||
|
switch (value)
|
||
|
{
|
||
|
case 0:
|
||
|
leds[led] = CRGB::Blue;
|
||
|
break;
|
||
|
case 1:
|
||
|
leds[led] = CRGB::Orange;
|
||
|
break;
|
||
|
case 2:
|
||
|
leds[led] = CRGB::Red;
|
||
|
break;
|
||
|
case 3:
|
||
|
leds[led] = CRGB::Yellow;
|
||
|
break;
|
||
|
case 4:
|
||
|
leds[led] = CRGB::Green;
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void sync_colors(){
|
||
|
for(int i = 0; i < NUM_LEDS; i++){
|
||
|
set_led_color(i, pw_state[i]);
|
||
|
}
|
||
|
FastLED.show();
|
||
|
}
|
||
|
|
||
|
bool check_pw(){
|
||
|
// Will return False if any led is wrong.
|
||
|
for(int i = 0; i < NUM_LEDS; i++){
|
||
|
if(correct_pw[i] != pw_state[i]){
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
// At this point all leds are correct!
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
int count_wrong_leds() {
|
||
|
int ret = 0;
|
||
|
for(int i = 0; i < NUM_LEDS; i++){
|
||
|
if(correct_pw[i] != pw_state[i]){
|
||
|
ret = ret + 1;
|
||
|
}
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
void fout_leds() {
|
||
|
int ret = count_wrong_leds();
|
||
|
lcd.clear();
|
||
|
lcd.setCursor(2, 0);
|
||
|
lcd.print(String("leds correct "));
|
||
|
lcd.setCursor(3,1);
|
||
|
lcd.print(String(NUM_LEDS - ret));
|
||
|
}
|
||
|
|
||
|
void setup()
|
||
|
{
|
||
|
// Setup Serial monitor
|
||
|
Serial.begin(9600);
|
||
|
|
||
|
// Init LCD
|
||
|
lcd.init();
|
||
|
lcd.backlight();
|
||
|
greet_screen();
|
||
|
|
||
|
// Init Ledstrip
|
||
|
FastLED.setBrightness(255);
|
||
|
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
|
||
|
// FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
|
||
|
|
||
|
// Setup buttons
|
||
|
pinMode(BTN_1, INPUT_PULLUP);
|
||
|
pinMode(BTN_2, INPUT_PULLUP);
|
||
|
pinMode(BTN_3, INPUT_PULLUP);
|
||
|
pinMode(BTN_4, INPUT_PULLUP);
|
||
|
pinMode(BTN_5, INPUT_PULLUP);
|
||
|
pinMode(BTN_CHECK, INPUT_PULLUP);
|
||
|
|
||
|
fill_solid(leds, NUM_LEDS, CRGB::Black);
|
||
|
FastLED.show();
|
||
|
generate_password();
|
||
|
}
|
||
|
|
||
|
void loop()
|
||
|
{
|
||
|
// Get value of each button and do something if the button is pressed
|
||
|
int start = 2;
|
||
|
for(int i = 0; i < NUM_LEDS; i++){
|
||
|
int val = digitalRead(start + i);
|
||
|
if(val == 0 ){
|
||
|
pw_state[i] = pw_state[i] + 1;
|
||
|
|
||
|
// Value can't be higher than 4
|
||
|
if(pw_state[i] > 4){
|
||
|
pw_state[i] = 0;
|
||
|
}
|
||
|
delay(100);
|
||
|
}
|
||
|
}
|
||
|
sync_colors();
|
||
|
|
||
|
// Check password if check button is pressed
|
||
|
if(digitalRead(BTN_CHECK) == 0){
|
||
|
if(check_pw() == true){
|
||
|
winner_screen();
|
||
|
}
|
||
|
else {
|
||
|
fout_leds();
|
||
|
}
|
||
|
}
|
||
|
delay(1);
|
||
|
}
|
||
|
|