Advertisement
pleasedontcode

"Date Display" rev_01

Jun 2nd, 2024
629
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: "Date Display"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-02 12:10:36
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Counting date starting from 02 June 2024 and */
  21.     /* adding daily one more day. */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Wire.h>
  26. #include <Adafruit_SSD1306.h>  //https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
  27. #include <U8g2_for_Adafruit_GFX.h>  //https://github.com/olikraus/U8g2_for_Adafruit_GFX
  28. #include <RTClib.h>  // Library for real-time clock
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF I2C PINS *****/
  35. const uint8_t Display_SSD1306OledDisplay_I2C_PIN_SDA_A4 = A4;
  36. const uint8_t Display_SSD1306OledDisplay_I2C_PIN_SCL_A5 = A5;
  37. const uint8_t Display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 0x3C;  // I2C address for the display
  38.  
  39. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  40. #define SCREEN_WIDTH 128  // OLED display width, in pixels
  41. #define SCREEN_HEIGHT 64  // OLED display height, in pixels
  42. #define OLED_RESET    -1  // Reset pin # (or -1 if sharing Arduino reset pin)
  43.  
  44. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  45. U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;  // Create an instance of the U8G2_FOR_ADAFRUIT_GFX class
  46. RTC_DS1307 rtc;  // Create an instance of the RTC_DS1307 class
  47.  
  48. void setup(void) {
  49.   // Initialize the display with the I2C address
  50.   if(!display.begin(SSD1306_SWITCHCAPVCC, Display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS)) {
  51.     Serial.begin(9600);
  52.     Serial.println(F("SSD1306 allocation failed"));
  53.     for(;;); // Don't proceed, loop forever
  54.   }
  55.  
  56.   // Initialize the RTC
  57.   if (!rtc.begin()) {
  58.     Serial.println(F("Couldn't find RTC"));
  59.     while (1);
  60.   }
  61.  
  62.   // Set the RTC to the initial date if it's not running
  63.   if (!rtc.isrunning()) {
  64.     rtc.adjust(DateTime(2024, 6, 2, 0, 0, 0));  // Set the date to 02 June 2024
  65.   }
  66.  
  67.   display.display();
  68.   delay(2000);  // Pause for 2 seconds
  69.  
  70.   // Clear the buffer
  71.   display.clearDisplay();
  72.  
  73.   // Initialize U8G2 for Adafruit GFX
  74.   u8g2_for_adafruit_gfx.begin(display);  // Connect u8g2 procedures to Adafruit GFX
  75.  
  76.   // Draw a single pixel in white
  77.   display.drawPixel(10, 10, SSD1306_WHITE);
  78.  
  79.   // Show the display buffer on the screen
  80.   display.display();
  81.   delay(2000);  // Pause for 2 seconds
  82. }
  83.  
  84. void loop(void) {
  85.   // Clear the display
  86.   display.clearDisplay();
  87.  
  88.   // Get the current date from the RTC
  89.   DateTime now = rtc.now();
  90.  
  91.   // Set the font and draw the date
  92.   u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf);
  93.   u8g2_for_adafruit_gfx.setCursor(0, 20);
  94.   u8g2_for_adafruit_gfx.print("Date: ");
  95.   u8g2_for_adafruit_gfx.print(now.day());
  96.   u8g2_for_adafruit_gfx.print("/");
  97.   u8g2_for_adafruit_gfx.print(now.month());
  98.   u8g2_for_adafruit_gfx.print("/");
  99.   u8g2_for_adafruit_gfx.print(now.year());
  100.  
  101.   // Show the display buffer on the screen
  102.   display.display();
  103.   delay(2000);  // Pause for 2 seconds
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement