Advertisement
pleasedontcode

**Quiz Display** rev_01

Nov 14th, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Quiz Display**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-11-14 15:06:13
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* A quiz game, where it will use tts voice with the */
  21.     /* speaker, and it will ask questions with 2 choices, */
  22.     /* and there is red button (left) and yellow button */
  23.     /* (right)    It will display the choices on the OLED */
  24.     /* Display. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Wire.h>
  31. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  32. #include <Adafruit_SSD1306.h>   //https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37. void displayQuestion(const char* question, const char* option1, const char* option2);
  38. void checkAnswer(bool answer);
  39.  
  40. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  41. const uint8_t RedButton_PushButton_PIN_D2       = 2;
  42. const uint8_t YellowButton_PushButton_PIN_D3        = 3;
  43.  
  44. /***** DEFINITION OF I2C PINS *****/
  45. const uint8_t OLED_SSD1306OledDisplay_I2C_PIN_SDA_A4        = A4;
  46. const uint8_t OLED_SSD1306OledDisplay_I2C_PIN_SCL_A5        = A5;
  47. const uint8_t OLED_SSD1306OledDisplay_I2C_SLAVE_ADDRESS     = 60;
  48.  
  49. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  50. // Create instances for the buttons
  51. EasyButton redButton(RedButton_PushButton_PIN_D2);
  52. EasyButton yellowButton(YellowButton_PushButton_PIN_D3);
  53.  
  54. // Create instance for the OLED display
  55. Adafruit_SSD1306 display(128, 32); // Initialize with width and height
  56.  
  57. void setup(void)
  58. {
  59.     // put your setup code here, to run once:
  60.     pinMode(RedButton_PushButton_PIN_D2, INPUT_PULLUP);
  61.     pinMode(YellowButton_PushButton_PIN_D3, INPUT_PULLUP);
  62.  
  63.     // Initialize the display
  64.     display.begin(SSD1306_SWITCHCAPVCC, OLED_SSD1306OledDisplay_I2C_SLAVE_ADDRESS);
  65.     display.clearDisplay();
  66.     display.display();
  67.  
  68.     // Initialize buttons
  69.     redButton.begin();
  70.     yellowButton.begin();
  71. }
  72.  
  73. void loop(void)
  74. {
  75.     // Update button states
  76.     redButton.read();
  77.     yellowButton.read();
  78.  
  79.     // Display a question
  80.     displayQuestion("Is the sky blue?", "Red: No", "Yellow: Yes");
  81.  
  82.     // Check for button presses
  83.     if (redButton.pressed()) {
  84.         checkAnswer(false); // Red button pressed
  85.     }
  86.     if (yellowButton.pressed()) {
  87.         checkAnswer(true); // Yellow button pressed
  88.     }
  89.  
  90.     // Add a small delay to avoid bouncing
  91.     delay(100);
  92. }
  93.  
  94. void displayQuestion(const char* question, const char* option1, const char* option2) {
  95.     display.clearDisplay();
  96.     display.setTextSize(1);
  97.     display.setTextColor(SSD1306_WHITE);
  98.     display.setCursor(0, 0);
  99.     display.println(question);
  100.     display.setCursor(0, 16);
  101.     display.println(option1);
  102.     display.setCursor(0, 24);
  103.     display.println(option2);
  104.     display.display();
  105. }
  106.  
  107. void checkAnswer(bool answer) {
  108.     if (answer) {
  109.         // Correct answer logic
  110.         display.clearDisplay();
  111.         display.setCursor(0, 0);
  112.         display.println("Correct!");
  113.     } else {
  114.         // Incorrect answer logic
  115.         display.clearDisplay();
  116.         display.setCursor(0, 0);
  117.         display.println("Incorrect!");
  118.     }
  119.     display.display();
  120.     delay(2000); // Show result for 2 seconds
  121. }
  122.  
  123. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement