Advertisement
pleasedontcode

OLED Messaging rev_03

Aug 25th, 2024
100
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: OLED Messaging
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-08-25 15:19:03
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Display "Hello, World!" on the SSD1306 OLED using */
  21.     /* I2C communication with SDA on pin D21 and SCL on */
  22.     /* pin D22, ensuring proper initialization of the */
  23.     /* Adafruit SSD1306 library. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27. /********* User code review feedback **********
  28. #### Feedback 1 ####
  29. - print on serial that esp32 is alive on loop function
  30. ********* User code review feedback **********/
  31.  
  32. /****** DEFINITION OF LIBRARIES *****/
  33. #include <Wire.h>
  34. #include <Adafruit_SSD1306.h>  // https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
  35. #include <U8g2_for_Adafruit_GFX.h>  // https://github.com/olikraus/U8g2_for_Adafruit_GFX
  36.  
  37. /****** FUNCTION PROTOTYPES *****/
  38. void setup(void);
  39. void loop(void);
  40.  
  41. /***** DEFINITION OF I2C PINS *****/
  42. const uint8_t display_SSD1306OledDisplay_I2C_PIN_SDA_D21 = 21; // SDA pin
  43. const uint8_t display_SSD1306OledDisplay_I2C_PIN_SCL_D22 = 22; // SCL pin
  44. const uint8_t display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 0x3C; // I2C address (0x3C is common for SSD1306)
  45.  
  46. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  47. // Create an instance of the display object
  48. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  49. #define SCREEN_HEIGHT 64  // OLED display height, in pixels
  50. #define OLED_RESET    -1  // Reset pin # (or -1 if sharing Arduino reset pin)
  51. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // Initialize display with I2C
  52.  
  53. // Create an instance for U8g2 using the Adafruit display
  54. U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx; // Create an instance for U8g2
  55.  
  56. void setup(void)
  57. {
  58.     // Initialize serial communication for debugging
  59.     Serial.begin(9600);
  60.    
  61.     // Initialize the display with I2C address
  62.     display.begin(SSD1306_SWITCHCAPVCC, display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS);
  63.     display.clearDisplay(); // Clear the display buffer
  64.  
  65.     // Connect u8g2 procedures to Adafruit GFX
  66.     u8g2_for_adafruit_gfx.begin(display);
  67.    
  68.     // Set up the display
  69.     display.setTextSize(1); // Set text size
  70.     display.setTextColor(SSD1306_WHITE); // Set text color
  71.     display.setCursor(0, 0); // Set cursor position
  72.     display.println(F("Hello, World!")); // Print message
  73.     display.display(); // Refresh the display
  74.     delay(2000); // Delay for 2 seconds
  75. }
  76.  
  77. void loop(void)
  78. {
  79.     // Main code to run repeatedly
  80.     display.clearDisplay(); // Clear the display buffer
  81.     display.setCursor(0, 0); // Set cursor position
  82.     display.println(F("ESP32 with OLED")); // Print message
  83.     display.display(); // Refresh the display
  84.     delay(2000); // Delay for 2 seconds
  85.  
  86.     // Print to serial to indicate ESP32 is alive
  87.     Serial.println("ESP32 is alive!"); // Print message to serial monitor
  88. }
  89.  
  90. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement