// #include <Wire.h>
// #include <Adafruit_GFX.h>
// #include <Adafruit_SSD1306.h>
// #include <Keypad.h>
// #define SCREEN_WIDTH 128 // OLED display width, in pixels
// #define SCREEN_HEIGHT 64 // OLED display height, in pixels
// #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
// Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// int interval = 15000; // 15 seconds in milliseconds
// unsigned long previousMillis = 0;
// #define SPEAKER_PIN 11
// char userAns;
// int score;
// const int ROW_NUM = 4; //four rows
// const int COLUMN_NUM = 4; //four columns
// char keys[ROW_NUM][COLUMN_NUM] = { {'1','2','3', 'A'}, {'4','5','6', 'B'}, {'7','8','9', 'C'}, {'*','0','#', 'D'} };
// byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
// byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
// Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
// char operation;
// int correctAns; // Declare the variable to hold the correct answer
// int answers[4];
// void setup() {
// pinMode(SPEAKER_PIN, OUTPUT);
// display.begin(SSD1306_SWITCHCAPVCC, 0x3C); /* Initialize display with address 0x3C */
// display.clearDisplay(); /* Clear display */
// display.setTextColor(SSD1306_WHITE);
// display.setTextSize(0.1); /* Select font size of text. Increases with size of argument. */
// display.setCursor(0,0);
// display.println("Welcome to Math Quiz! ");
// display.display();
// wait(2000);
// display.setCursor(20, 20);
// display.println("Get Ready.... ");
// display.display();
// wait(3000);
// }
// void loop() {
// setTimer();
// displayQuestion();
// }
// // WORKING
// void displayQuestion() {
// display.clearDisplay();
// generateNumbers();
// // int num1 = rand() % 100 + 1; // Generate a random number between 1 and 100
// // int num2 = rand() % 100 + 1;
// // int Operator = rand() % 4; // Generate a random operator (0 = +, 1 = -, 2 = *, 3 = /)
// // // bos hena yasta
// // if (Operator == 0) {
// // correctAns = num1 + num2;
// // display.setCursor(0,0);
// // display.print(num1);
// // display.print(" + ");
// // display.print(num2);
// // display.print(" = ?");
// // } else if (Operator == 1) {
// // correctAns = num1 - num2;
// // display.setCursor(0,0);
// // display.print(num1);
// // display.print(" - ");
// // display.print(num2);
// // display.print(" = ?");
// // } else if (Operator == 2) {
// // correctAns = num1 * num2;
// // display.setCursor(0,0);
// // display.print(num1);
// // display.print(" * ");
// // display.print(num2);
// // display.print(" = ?");
// // } else if (Operator == 3) {
// // correctAns = num1 / num2;
// // display.setCursor(0,0);
// // display.print(num1);
// // display.print(" / ");
// // display.print(num2);
// // display.print(" = ?");
// // }
// answers[0] = correctAns;
// answers[1] = correctAns * 1.5;
// answers[2] = correctAns - 10;
// answers[3] = correctAns / 2; // Generate 3 incorrect answers along with the correct answer
// // Shuffle the answers randomly using a swap algorithm
// for (int i = 0; i < 4; i++) {
// int j = random() % 4;
// int temp = answers[i];
// answers[i] = answers[j];
// answers[j] = temp;
// }
// // Display the shuffled answers
// for (int i = 0; i < 4; i++) {
// display.setCursor(0, i*10 + 20);
// display.print((char)('A' + i));
// display.print(") ");
// display.println(answers[i]);
// }
// display.display();
// // Wait for user input
// while (true) {
// char key = keypad.getKey();
// if (key != NO_KEY) {
// if (key == 'A' || key == 'B' || key == 'C' || key == 'D') {
// userAns = key;
// break;
// }
// }
// }
// // Check if user's answer is correct and display result
// if ((userAns == 'A' && answers[0] == correctAns) || (userAns == 'A' && answers[0] != correctAns) ) {
// checkInput();
// }
// else if ((userAns == 'B' && answers[1] == correctAns) || (userAns == 'B' && answers[1] != correctAns)) {
// checkInput();
// }
// else if ((userAns == 'C' && answers[2] == correctAns) || (userAns == 'C' && answers[2] != correctAns)) {
// checkInput();
// }
// else if ((userAns == 'D' && answers[3] == correctAns) || (userAns == 'D' && answers[3] != correctAns)) {
// checkInput();
// }
// }
// // END WORKING
// void generateNumbers() {
// // int num1;
// // int num2;
// // int Operator = random(1, 5);
// // switch (Operator) {
// // case (1):
// // operation = "+";
// // num1 = random(1, 101);
// // num2 = random(1, 101);
// // correctAns = num1 + num2;
// // display.setCursor(0,0);
// // display.print(num1);
// // display.print(" + ");
// // display.print(num2);
// // display.print(" = ?");
// // break;
// // case (2):
// // operation = "-";
// // num1 = random(1, 101);
// // num2 = random(1, 101);
// // while (num1 < num2) {
// // num2 = random(1, 101);
// // }
// // correctAns = num1 - num2;
// // display.setCursor(0,0);
// // display.print(num1);
// // display.print(" - ");
// // display.print(num2);
// // display.print(" = ?");
// // break;
// // case (3):
// // operation = "x";
// // num1 = random(1, 101);
// // num2 = random(1, 101);
// // correctAns = num1 * num2;
// // display.setCursor(0,0);
// // display.print(num1);
// // display.print(" * ");
// // display.print(num2);
// // display.print(" = ?");
// // break;
// // case (4):
// // operation = "/";
// // num1 = random(1, 101);
// // num2 = random(1, 101);
// // while ((num1 < num2) || ((num1 % num2) != 0)) {
// // num2 = random(1, 101);
// // }
// // correctAns = num1 / num2;
// // display.setCursor(0,0);
// // display.print(num1);
// // display.print(" + ");
// // display.print(num2);
// // display.print(" = ?");
// // break;
// // }
// // Set the seed value using an integer number
// int seedValue = random();
// // Reset the random function with the new seed value
// randomSeed(seedValue);
// int num1 = random(0,100); // Generate a random number between 1 and 100
// int num2 = random(0,100);
// int Operator = random(0,4); // Generate a random operator (0 = +, 1 = -, 2 = *, 3 = /)
// // bos hena yasta
// if (Operator == 0) {
// correctAns = num1 + num2;
// display.setCursor(0,0);
// display.print(num1);
// display.print(" + ");
// display.print(num2);
// display.print(" = ?");
// } else if (Operator == 1) {
// while (num1 < num2) {
// num2 = random(1, 100);
// }
// correctAns = num1 - num2;
// display.setCursor(0,0);
// display.print(num1);
// display.print(" - ");
// display.print(num2);
// display.print(" = ?");
// } else if (Operator == 2) {
// correctAns = num1 * num2;
// display.setCursor(0,0);
// display.print(num1);
// display.print(" * ");
// display.print(num2);
// display.print(" = ?");
// } else if (Operator == 3) {
// while ((num1 < num2) || ((num1 % num2) != 0)) {
// num2 = random(1, 101);
// }
// correctAns = num1 / num2;
// display.setCursor(0,0);
// display.print(num1);
// display.print(" / ");
// display.print(num2);
// display.print(" = ?");
// }
// }
// void checkInput() {
// for (int i = 0; i < 4; i++) {
// if (userAns == 'A' + i) {
// if (answers[i] == correctAns) {
// display.setTextSize(1.5);
// display.clearDisplay();
// display.setCursor(0, 0);
// display.println("Correct!");
// display.display();
// tone(SPEAKER_PIN, 500, 100);
// score++;
// display.setCursor(0, 20);
// display.print("Score: ");
// display.println(score);
// display.display();
// wait(3000);
// display.setTextSize(0.1);
// } else if (answers[i] != correctAns) {
// display.setTextSize(1.5);
// display.clearDisplay();
// display.setCursor(0, 0);
// display.println("Wrong! You Lost!");
// display.display();
// tone(SPEAKER_PIN, 200, 100);
// // Game Should Terminate??
// display.setCursor(0, 20);
// display.print("Final Score: ");
// display.println(score);
// display.display();
// wait(3000);
// display.setCursor(35,40);
// display.println("GOOD BYE!");
// display.display();
// while (1) {}
// }
// }
// }
// }
// void setTimer() {
// if (millis() - previousMillis >= interval) {
// previousMillis = millis();
// display.clearDisplay();
// displayTimer(15); // pass the total number of seconds as 15
// }
// }
// void displayTimer(int totalSecond) {
// display.setCursor(10, 0); //set the cursor to the top right position
// char timeFormat[3]; //for formatting the time
// //Return a formatted string with only seconds
// sprintf(timeFormat, "%dS", totalSecond % 60);
// display.print(timeFormat); //print it to the OLED
// display.display();
// }
// void wait(unsigned long ms) {
// unsigned long start = millis();
// while (millis() - start < ms) {
// // wait
// }
// }
// // END MATH QUIZ
// START TIMER EEPROM
// #include <Wire.h>
// #include <Adafruit_GFX.h>
// #include <Adafruit_SSD1306.h>
// #include <EEPROM.h>
// #define SCREEN_WIDTH 128 // OLED display width, in pixels
// #define SCREEN_HEIGHT 64 // OLED display height, in pixels
// #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
// Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// const int TIMER_ADDRESS = 0; // Address in EEPROM to store timer value
// unsigned long timerDuration = 30; // Countdown duration in seconds
// unsigned long previousMillis = 0;
// void setTimer(unsigned long inSeconds) {
// if (inSeconds < 86400) { // Maximum is 23h:59m:59s = 86399 seconds
// // Store timer value in EEPROM
// EEPROM.put(TIMER_ADDRESS, inSeconds);
// }
// }
// void displayTimer(int totalSecond) {
// display.clearDisplay();
// display.setCursor(0, 0);
// char timeFormat[8];
// sprintf(timeFormat, "%02d", totalSecond / 60); // minutes
// display.print(timeFormat);
// display.print(":");
// sprintf(timeFormat, "%02d", abs((int)totalSecond % 60)); // seconds
// display.print(timeFormat);
// display.display();
// }
// void setup() {
// display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// display.clearDisplay();
// display.setTextColor(WHITE);
// display.setTextSize(1);
// EEPROM.put(TIMER_ADDRESS, timerDuration); // Store timer value in EEPROM
// displayTimer(timerDuration); // Display the initial countdown value on the OLED
// }
// void loop() {
// unsigned long currentMillis = millis();
// if (currentMillis - previousMillis >= 1000 && timerDuration > 0) {
// previousMillis = currentMillis;
// timerDuration -= 1;
// EEPROM.put(TIMER_ADDRESS, timerDuration); // Update timer value in EEPROM
// displayTimer(timerDuration); // Display the updated countdown value on the OLED
// }
// if (timerDuration == 0) {
// // Stop the countdown and exit the loop
// display.clearDisplay();
// display.setCursor(0, 0);
// display.print("Countdown Done!");
// display.display();
// while (true); // Loop forever
// }
// }
// FINISH TIMER EEPROM
// START TIMER RTC
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
RTC_DS1307 rtc;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
unsigned long timerDuration = 15; // Countdown duration in seconds
unsigned long previousMillis = 0;
void displayTimer(int totalSecond) {
display.clearDisplay();
display.setCursor(0, 0);
char timeFormat[8];
sprintf(timeFormat, "%02d", totalSecond / 60); // minutes
display.print(timeFormat);
display.print(":");
sprintf(timeFormat, "%02d", abs((int)totalSecond % 60)); // seconds
display.print(timeFormat);
display.display();
}
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
Wire.begin();
rtc.begin();
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Uncomment this line to set the RTC to the current date and time
rtc.writeSqwPinMode(DS1307_OFF); // Disable square wave output
displayTimer(timerDuration); // Display the initial countdown value on the OLED
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 1000 && timerDuration > 0) {
previousMillis = currentMillis;
timerDuration -= 1;
displayTimer(timerDuration); // Display the updated countdown value on the OLED
}
if (timerDuration == 0) {
// Stop the countdown and exit the loop
display.clearDisplay();
display.setCursor(0, 0);
display.print("Countdown Done!");
display.display();
while (true); // Loop forever
}
}
// END TIMER RTC