pleasedontcode

OLED Marquee rev_01

Oct 11th, 2025
296
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 Marquee
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2025-10-11 15:38:26
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* 我想要用ESP32微控制器加上OLED螢幕的Arduino程式碼,並且要在螢幕上產生修平科技大學智慧 */
  21.     /* 車輛系的字樣,並且加上跑馬燈,然後請提前幫排除問題1.避免程式碼生成後螢幕出現白點亂碼2.避免少字體 */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <Adafruit_GFX.h>
  30. #include <Adafruit_SSD1306.h>
  31.  
  32. /****** DEFINITIONS *****/
  33. #ifndef SCREEN_WIDTH
  34. #define SCREEN_WIDTH 128
  35. #endif
  36. #ifndef SCREEN_HEIGHT
  37. #define SCREEN_HEIGHT 64
  38. #endif
  39. #define OLED_RESET -1
  40. #define OLED_ADDRESS 0x3C
  41.  
  42. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  43.  
  44. // Marquee text (ASCII fallback for reliable rendering)
  45. static const char marqueeText[] = "Xiping Tech Univ Smart Vehicle Dept  ";
  46. static int marqueeX = 0;
  47. static unsigned long lastMarqueeUpdate = 0;
  48. static const unsigned long MARQUEE_DELAY = 70; // adjust speed (ms)
  49.  
  50. void setup() {
  51.   // Initialize I2C and OLED
  52.   if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDRESS)) {
  53.     // Try alternative address commonly used by some modules
  54.     if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) {
  55.       // If still failing, loop forever
  56.       for(;;);
  57.     }
  58.   }
  59.   // prevent white dots by turning off charge pump briefly during init
  60.   display.clearDisplay();
  61.   display.display();
  62.  
  63.   // Optional small welcome sequence
  64.   display.setTextSize(1);
  65.   display.setTextColor(WHITE);
  66.   display.setCursor(0, 0);
  67.   display.println("Initializing OLED...");
  68.   display.display();
  69.   delay(800);
  70.  
  71.   // Show a simple, reliable heading (ASCII fallback)
  72.   display.clearDisplay();
  73.   display.setCursor(0, 0);
  74.   display.setTextSize(1);
  75.   display.println("Xiping Tech Univ");
  76.   display.println("Smart Vehicle Dept");
  77.   display.display();
  78.   delay(1200);
  79.   // Clear to start marquee
  80.   display.clearDisplay();
  81.   display.display();
  82. }
  83.  
  84. void loop() {
  85.   unsigned long now = millis();
  86.   if(now - lastMarqueeUpdate >= MARQUEE_DELAY) {
  87.     lastMarqueeUpdate = now;
  88.  
  89.     display.clearDisplay();
  90.     display.setTextSize(1);
  91.     display.setTextColor(WHITE);
  92.  
  93.     // Marquee: draw text with horizontal offset
  94.     int y = 28;
  95.     display.setCursor(-marqueeX, y);
  96.     display.print(marqueeText);
  97.     display.display();
  98.  
  99.     // Update offset
  100.     marqueeX++;
  101.     int textW;
  102.     int16_t x1, y1;
  103.     uint16_t wText, hText;
  104.     // Measure text width
  105.     display.getTextBounds(marqueeText, 0, y, &x1, &y1, &wText, &hText);
  106.     textW = (int)wText;
  107.     if(marqueeX > textW) marqueeX = 0;
  108.   }
  109. }
  110.  
  111. /* END CODE */
  112.  
Advertisement
Add Comment
Please, Sign In to add comment