Skip to content

Instantly share code, notes, and snippets.

@afrieirham
Created December 27, 2020 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afrieirham/9df91e066b8b1198f7ecf6fe7f4ce321 to your computer and use it in GitHub Desktop.
Save afrieirham/9df91e066b8b1198f7ecf6fe7f4ce321 to your computer and use it in GitHub Desktop.
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 11, 10, A0, A1);
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {A2, A3, 9, 8};
byte colPins[COLS] = {7, 6, 5, };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int LCDROW = 0;
int RED = A5;
int GREEN = A4;
int BLUE = 2;
int randomNumber;
int inputNumber;
String inputString = "";
int playCount = 0;
void setup()
{
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("GAME START");
randomSeed(analogRead(5));
Serial.begin(9600);
pinMode(RED, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(GREEN, OUTPUT);
delay(1000);
resetGame();
}
void loop(){
char key = keypad.getKey();
// Start game
if(playCount == 0 && key != NO_KEY) {
randomNumber = random(1,10);
lcd.clear();
lcd.print("Enter a number");
lcd.setCursor(0,1);
lcd.print("Between 1-10");
}
// Confirm input
if(key>47 && key<58 && key != NO_KEY) {
lcd.clear();
lcd.print("Your guess: ");
lcd.print(key);
parseInt(key);
lcd.setCursor(0,1);
lcd.print("# to confirm");
}
// Check input on confirm
if(key == '#' && key != NO_KEY) {
// Increment playCount
playCount++;
// Clear LCD and turn off all LED
lcd.clear();
resetLED();
// Loading
lcd.print("Let me check...");
delay(1000);
lcd.clear();
// Evaluate input
evaluateInput();
}
// No more chance
if (playCount == 3) {
lcd.clear();
lcd.print("GAME OVER!");
delay(1500);
lcd.clear();
lcd.print("The number was: ");
lcd.setCursor(0, 1);
lcd.print(randomNumber);
delay(1000);
resetGame();
}
}
// Parse string to int
void parseInt(char key) {
inputString += key;
inputNumber = inputString.toInt();
}
void evaluateInput(){
// Input is less than secret
if(randomNumber > inputNumber){
digitalWrite(RED, HIGH);
displayChances();
}
// Input is more than secret
if(randomNumber < inputNumber){
digitalWrite(BLUE, HIGH);
displayChances();
}
// Correct guess
if(randomNumber == inputNumber){
digitalWrite(GREEN, HIGH);
lcd.print("It's Right");
lcd.setCursor(0,1);
lcd.print("Congrats");
delay(1000);
resetGame();
}
// Reset input for next guess
inputString = "";
}
// Display chances left
void displayChances(){
lcd.print("Sorry :)");
delay(1000);
lcd.clear();
lcd.print(3 - playCount);
lcd.setCursor(1,0);
lcd.print(" more tries");
delay(2000);
lcd.clear();
lcd.print("Try again");
}
// Reset game
void resetGame(){
resetLED();
playCount = 0;
lcd.clear();
lcd.print("Press to start");
lcd.setCursor(0,1);
lcd.print("*");
}
// Reset LED
void resetLED(){
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
digitalWrite(RED, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment