//-- JUMBLE VARIABLES --
String randomWords[] = {
"TITE", "PALA", "BOBO", "MALI", "TAMA",
};
String werd;
String jumble;
int lastKnob = -1;
int count = 0;
//-- LCD Variables --
#include "Wire.h"
//#include "Adafruit_LiquidCrystal.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//Adafruit_LiquidCrystal lcd(0x20);
// https://www.seetron.com/apps/app_cceditor.html
byte arrowBitmap[8] = {0x84, 0x8E, 0x9F, 0x84, 0x84, 0x84, 0x80, 0x80};
byte upArrowChar = 0;
//--I/O pins --
const byte button = 4; // Digital pin 4
const byte knob = A0;
#define ISPRESSED LOW
//Door
#include <Servo.h>
const byte servoMotorPin = A1;
Servo servoMotor;
void WaitForButtonPress() {
while (digitalRead(4) != LOW) {
delay(100);
}
delay(500); // ignore switch bounce
}
void SetupJumble() {
randomSeed(analogRead(A1));
jumble = werd;
byte wordLen = jumble.length();
for (byte i = 0; i < wordLen; i++) {
int randomIndex = random(wordLen);
byte tmp = jumble[i];
jumble[i] = jumble[randomIndex];
jumble[randomIndex] = tmp;
}
lcd.print(jumble);
}
void SetupLCD() {
lcd.begin(16, 2); // 16 columns, 2 rows
lcd.clear();
lcd.setBacklight(HIGH);
lcd.createChar(upArrowChar, arrowBitmap);
}
void SetupInputs() {
pinMode(button, INPUT_PULLUP);
}
void setup() {
Serial.begin(115200);
SetupInputs();
SetupLCD();
NewGame();
servoMotor.attach(servoMotorPin);
}
void SwapLetters(byte index1, byte index2) {
byte wordLen = werd.length();
if (index1 >= 0 && index1 < wordLen && index2 >= 0 && index2 < wordLen) {
byte letter1 = jumble[index1];
jumble[index1] = jumble[index2];
jumble[index2] = letter1;
lcd.setCursor(0, 0);
lcd.print(jumble);
}
}
String PickRandomWord() {
int numberOfRandomWords = sizeof(randomWords) / sizeof(String);
Serial.print("numberOfRandomWords=");
Serial.println(numberOfRandomWords);
int randomWordIndex = random(numberOfRandomWords);
return randomWords[randomWordIndex];
}
void NewGame() {
closeDoor();
lcd.clear();
werd = PickRandomWord();
SetupJumble();
lastKnob = -1;
}
void ContinueGame(){
count += 1;
lcd.clear();
werd = PickRandomWord();
SetupJumble();
lastKnob = -1;
}
void Win() {
count = 0;
openDoor();
lcd.clear();
delay(500);
lcd.setCursor(0, 1);
lcd.print("** UNLOCKED! **");
delay(3000);
if (digitalRead(button) == ISPRESSED){
NewGame();
delay(500);
lcd.setCursor(0, 1);
lcd.print(" ");
closeDoor();
}
else{
lcd.clear();
lcd.setCursor(5,1);
lcd.print("WELCOME");
delay(2500);
if (digitalRead(button) == ISPRESSED){
NewGame();
delay(500);
lcd.setCursor(0, 1);
lcd.print(" ");
closeDoor();
}else{
delay(2000);
closeDoor();
delay(2000);
NewGame();
}
}
}
void Condition(){
if (count != 3){
ContinueGame();
Serial.println(count);
lcd.setCursor(14,0);
lcd.print(count);
if(count == 3){
Win();
}
}
}
void KnobArrow() {
if (digitalRead(button) != ISPRESSED && jumble == werd) Condition();
int knobValue = map(analogRead(knob), 0, 1023, 0, 15);
if ((knobValue != lastKnob)) {
if (lastKnob != -1) {
lcd.setCursor(lastKnob, 1);
lcd.write(' ');
if (digitalRead(button) == ISPRESSED) {
SwapLetters(knobValue, lastKnob);
}
}
lcd.setCursor(knobValue, 1);
lcd.write((byte)upArrowChar);
lastKnob = knobValue;
}
delay(50);
}
void openDoor(){
servoMotor.write(90);
}
void closeDoor(){
servoMotor.write(180);
}
void loop() {
KnobArrow();
}