#include <Keypad.h>
#include <Wire.h>
#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
int x = 5;
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] = {5, 4, 3, 2};
byte colPins[COLS] = {6, 7, 8, 9};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
char lastKey = '\0'; // Store last key to prevent repeats
void setup() {
u8g2.begin();
u8g2.setFont(u8g2_font_ncenB08_tr);
}
void displayMessage(const char* message) {
u8g2.clearBuffer();
u8g2.setCursor(0, 10);
u8g2.print(message);
u8g2.sendBuffer();
}
void loop() {
if (x == 5) {
u8g2.clearBuffer();
u8g2.setCursor(0, 10);
u8g2.print("Whatchapterdidgojodie?");
u8g2.setCursor(0, 20);
u8g2.print("A-236");
u8g2.setCursor(0, 30);
u8g2.print("B-84");
u8g2.setCursor(0, 40);
u8g2.print("C-147");
u8g2.setCursor(0, 50);
u8g2.print("D-64");
u8g2.sendBuffer();
delay(5000);
}
char key = keypad.getKey();
if (key && key != lastKey) { // Ensure a new key was pressed
displayMessage("You pressed:");
u8g2.setCursor(0, 30);
u8g2.print(key);
u8g2.sendBuffer();
delay(1000);
if (key == 'A') {
displayMessage("Correct!");
x++;
} else {
displayMessage("Wrong!");
}
delay(1000);
lastKey = key; // Update last key to prevent repeats
} else if (!key) {
lastKey = '\0'; // Reset when no key is pressed
}
if (x == 6) {
displayMessage("Next question!");
delay(1000);
u8g2.clearBuffer()
u8g2.clearBuffer();
u8g2.setCursor(0, 10);
u8g2.print("What does TNT stand for?");
u8g2.setCursor(0, 20);
u8g2.print("A-Trinitrotoluene");
u8g2.setCursor(0, 30);
u8g2.print("B-Thermonuclear Test");
u8g2.setCursor(0, 40);
u8g2.print("C-Minecraft");
u8g2.setCursor(0, 50);
u8g2.print("D-Tom Not Tim");
u8g2.sendBuffer();
delay(5000);
key = keypad.getKey();
if (key && key != lastKey) {
displayMessage("You pressed:");
u8g2.setCursor(0, 30);
u8g2.print(key);
u8g2.sendBuffer();
delay(1000);
if (key == 'A') {
displayMessage("Correct!");
x++;
} else {
displayMessage("Wrong!");
}
delay(1000);
}
lastKey = key;
}
if (x == 7) {
u8g2.clearBuffer();
u8g2.setCursor(0, 10);
u8g2.print("What'stheCapitalofFrance?");
u8g2.setCursor(0, 20);
u8g2.print("A-Cayenne");
u8g2.setCursor(0, 30);
u8g2.print("B-Lyon");
u8g2.setCursor(0, 40);
u8g2.print("C-Paris");
u8g2.setCursor(0, 50);
u8g2.print("D-RueDeRivoli");
u8g2.sendBuffer();
delay(5000);
if (key == 'C') {
displayMessage("Correct!");
x++;
} else {
displayMessage("Wrong!");
}
if (!key) {
lastKey = '\0';
}
}
if (x == 8) {
displayMessage("Next question!");
delay(2000);
u8g2.clearBuffer();
u8g2.setCursor(0, 10);
u8g2.print("WholedtheVoyagerProject?");
u8g2.setCursor(0, 20);
u8g2.print("A-Neumann");
u8g2.setCursor(0, 30);
u8g2.print("B-Stone");
u8g2.setCursor(0, 40);
u8g2.print("C-Feynman");
u8g2.setCursor(0, 50);
u8g2.sendBuffer();
delay(5000);
if (key == 'B') {
displayMessage("Correct!");
x++;
} else {
displayMessage("Wrong!");
}
if (!key); {
lastKey = '\0';
}
if (x == 9) {
displayMessage("Next question!");
delay(2000);
u8g2.clearBuffer();
u8g2.setCursor(0, 10);
u8g2.print("LargestCityinGuyane?");
u8g2.setCursor(0, 20);
u8g2.print("A-SaintgGeorges");
u8g2.setCursor(0, 30);
u8g2.print("B-Cayenne");
u8g2.setCursor(0, 40);
u8g2.print("C-Rueon");
u8g2.setCursor(0, 50);
u8g2.sendBuffer();
delay(5000);
if (key == 'B') {
displayMessage("Correct!");
x++;
} else {
displayMessage("Wrong!");
}
(!key); {
lastKey = '\0';
}
}
}
}
Explaining the Code
#include <Keypad.h>
#include <Wire.h>
#include <U8g2lib.h>
This Code essentially says to have these libraries included in the project (Library - Imagine a book but instead of chapters it is different functions for the arduino to do)
* `x` is a variable used to track the current question number.
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'A', 'B', 'C', 'D'},
{'A', 'B', 'C', 'D'},
{'A', 'B', 'C', 'D'},
{'A', 'B', 'C', 'D'}
};
This code creates the array for the 4*4 keypad
byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {6, 7, 8, 9};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
This piece says where all of the keypad' wires are plugged in
This code prevents repeats in the answers
void setup() {
u8g2.begin();
u8g2.setFont(u8g2_font_ncenB08_tr);
This code lets the u8g2 library begin and chooses the font it uses
void displayMessage(const char* message) {
u8g2.clearBuffer();
u8g2.setCursor(0, 10);
u8g2.print(message);
u8g2.sendBuffer();
* `u8g2.print()` and related functions update the display.
* A helper function `displayMessage()` is used to show short messages cleanly.
void loop() {
if (x == 5) {
This code makes it so that this loops forever and if the variable is 5 the next piece of code activates
u8g2.clearBuffer();
u8g2.setCursor(0, 10);
u8g2.print("Whatchapterdidgojodie?");
u8g2.setCursor(0, 20);
u8g2.print("A-236");
u8g2.setCursor(0, 30);
u8g2.print("B-84");
u8g2.setCursor(0, 40);
u8g2.print("C-147");
u8g2.setCursor(0, 50);
u8g2.print("D-64");
u8g2.sendBuffer();
delay(5000);
}
This code shows the questions and the choices for answers
char key = keypad.getKey();
* `keypad.getKey()` is used to read the user's input.
if (key && key != lastKey) {
displayMessage("You pressed:");
u8g2.setCursor(0, 30);
u8g2.print(key);
u8g2.sendBuffer();
delay(1000);
This piece of code not only makes sure a new key other than the last question was pressed but it also announces on the display monitor what key was pressed
if (key == 'A') {
displayMessage("Correct!");
x++;
} else {
displayMessage("Wrong!");
}
delay(1000);
This block of code displays what answer is correct to press.
lastKey = key;
} else if (!key) {
lastKey = '\0';
The sketch presents questions one at a time. If the correct answer is pressed (e.g., 'A'), it displays "Correct!" and proceeds to the next question. Wrong answers display "Wrong!". The game progresses until all questions are answered.