Advertisement
pleasedontcode

OLED Messaging rev_05

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