Advertisement
hwthinker

ESP32 OLED 0.96 18650 holder battery- SKU: M101

Jul 20th, 2023 (edited)
1,446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // For a connection via I2C using the Arduino Wire include:
  2. #include <Wire.h>               // Only needed for Arduino 1.6.5 and earlier
  3. #include "SSD1306Wire.h"        // legacy: #include "SSD1306.h"
  4.  
  5. // I2C OLED Display works with SSD1306 driver
  6. #define OLED_SDA 5
  7. #define OLED_SCL 4
  8. #define LED      16
  9. //#define OLED_RST 16
  10.  
  11. // Initialize the OLED display using Arduino Wire:
  12. SSD1306Wire display(0x3C, OLED_SDA, OLED_SCL);   // ADDRESS, SDA, SCL  -  SDA and SCL usually populate automatically based on your board's pins_arduino.h e.g. https://github.com/esp8266/Arduino/blob/master/variants/nodemcu/pins_arduino.h
  13.  
  14.  
  15. #define DEMO_DURATION 3000
  16. typedef void (*Demo)(void);
  17.  
  18. int demoMode = 0;
  19. int counter = 1;
  20.  
  21. void setup() {
  22.   Serial.begin(115200);
  23.   Serial.println();
  24.   Serial.println();
  25.   pinMode(LED, OUTPUT);
  26.   blinky();
  27.  
  28.  
  29.   // Initialising the UI will init the display too.
  30.   display.init();
  31.  
  32.   display.flipScreenVertically();
  33.   display.setFont(ArialMT_Plain_10);
  34.  
  35. }
  36.  
  37. void blinky() {
  38.   for (int i = 0; i < 5; i++) {
  39.     digitalWrite(16, HIGH);   // turn the LED on (HIGH is the voltage level)
  40.     delay(300);                       // wait for a second
  41.     digitalWrite(16, LOW);    // turn the LED off by making the voltage LOW
  42.     delay(500);
  43.   }
  44. }
  45.  
  46. void drawFontFaceDemo() {
  47.   // Font Demo1
  48.   // create more fonts at http://oleddisplay.squix.ch/
  49.   display.setTextAlignment(TEXT_ALIGN_LEFT);
  50.   display.setFont(ArialMT_Plain_24);
  51.   display.drawString(0, 0, "HwThinker");
  52.   display.setFont(ArialMT_Plain_16);
  53.   display.drawString(0, 26, "in Action");
  54.   display.setFont(ArialMT_Plain_10);
  55.   display.drawString(0, 45, "Nature");
  56. }
  57.  
  58. void drawTextFlowDemo() {
  59.   display.setFont(ArialMT_Plain_10);
  60.   display.setTextAlignment(TEXT_ALIGN_LEFT);
  61.   display.drawStringMaxWidth(0, 0, 128,
  62.                              "Lorem ipsum\n dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore." );
  63. }
  64.  
  65. void drawTextAlignmentDemo() {
  66.   // Text alignment demo
  67.   display.setFont(ArialMT_Plain_10);
  68.  
  69.   // The coordinates define the left starting point of the text
  70.   display.setTextAlignment(TEXT_ALIGN_LEFT);
  71.   display.drawString(0, 10, "Left aligned (0,10)");
  72.  
  73.   // The coordinates define the center of the text
  74.   display.setTextAlignment(TEXT_ALIGN_CENTER);
  75.   display.drawString(64, 22, "Center aligned (64,22)");
  76.  
  77.   // The coordinates define the right end of the text
  78.   display.setTextAlignment(TEXT_ALIGN_RIGHT);
  79.   display.drawString(128, 33, "Right aligned (128,33)");
  80. }
  81.  
  82. void drawRectDemo() {
  83.   // Draw a pixel at given position
  84.   for (int i = 0; i < 10; i++) {
  85.     display.setPixel(i, i);
  86.     display.setPixel(10 - i, i);
  87.   }
  88.   display.drawRect(12, 12, 20, 20);
  89.  
  90.   // Fill the rectangle
  91.   display.fillRect(14, 14, 17, 17);
  92.  
  93.   // Draw a line horizontally
  94.   display.drawHorizontalLine(0, 40, 20);
  95.  
  96.   // Draw a line horizontally
  97.   display.drawVerticalLine(40, 0, 20);
  98. }
  99.  
  100. void drawCircleDemo() {
  101.   for (int i = 1; i < 8; i++) {
  102.     display.setColor(WHITE);
  103.     display.drawCircle(32, 32, i * 3);
  104.     if (i % 2 == 0) {
  105.       display.setColor(BLACK);
  106.     }
  107.     display.fillCircle(96, 32, 32 - i * 3);
  108.   }
  109. }
  110.  
  111. void drawProgressBarDemo() {
  112.   int progress = (counter / 5) % 100;
  113.   // draw the progress bar
  114.   display.drawProgressBar(0, 32, 120, 10, progress);
  115.  
  116.   // draw the percentage as String
  117.   display.setTextAlignment(TEXT_ALIGN_CENTER);
  118.   display.drawString(64, 15, String(progress) + "%");
  119. }
  120.  
  121.  
  122. Demo demos[] = {drawFontFaceDemo, drawTextFlowDemo, drawTextAlignmentDemo, drawRectDemo, drawCircleDemo, drawProgressBarDemo};
  123. int demoLength = (sizeof(demos) / sizeof(Demo));
  124. long timeSinceLastModeSwitch = 0;
  125.  
  126. void loop() {
  127.   // clear the display
  128.   display.clear();
  129.   // draw the current demo method
  130.   demos[demoMode]();
  131.  
  132.   display.setFont(ArialMT_Plain_10);
  133.   display.setTextAlignment(TEXT_ALIGN_RIGHT);
  134.   display.drawString(128, 54, String(millis()));
  135.   // write the buffer to the display
  136.   display.display();
  137.  
  138.   if (millis() - timeSinceLastModeSwitch > DEMO_DURATION) {
  139.     demoMode = (demoMode + 1)  % demoLength;
  140.     timeSinceLastModeSwitch = millis();
  141.   }
  142.   counter++;
  143.   delay(10);
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement