Advertisement
mikroavr

oled_lcd

Jul 15th, 2021
1,209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 13.37 KB | None | 0 0
  1. /**************************************************************************
  2.   This is an example for our Monochrome OLEDs based on SSD1306 drivers
  3.  
  4.   Pick one up today in the adafruit shop!
  5.   ------> http://www.adafruit.com/category/63_98
  6.  
  7.   This example is for a 128x64 pixel display using I2C to communicate
  8.   3 pins are required to interface (two I2C and one reset).
  9.  
  10.   Adafruit invests time and resources providing this open
  11.   source code, please support Adafruit and open-source
  12.   hardware by purchasing products from Adafruit!
  13.  
  14.   Written by Limor Fried/Ladyada for Adafruit Industries,
  15.   with contributions from the open source community.
  16.   BSD license, check license.txt for more information
  17.   All text above, and the splash screen below must be
  18.   included in any redistribution.
  19.  **************************************************************************/
  20.  
  21. #include <SPI.h>
  22. #include <Wire.h>
  23. #include <Adafruit_GFX.h>
  24. #include <Adafruit_SSD1306.h>
  25.  
  26. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  27. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  28.  
  29. // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
  30. // The pins for I2C are defined by the Wire-library.
  31. // On an arduino UNO:       A4(SDA), A5(SCL)
  32. // On an arduino MEGA 2560: 20(SDA), 21(SCL)
  33. // On an arduino LEONARDO:   2(SDA),  3(SCL), ...
  34. #define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
  35. #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
  36. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  37.  
  38. #define NUMFLAKES     10 // Number of snowflakes in the animation example
  39.  
  40. #define LOGO_HEIGHT   16
  41. #define LOGO_WIDTH    16
  42. static const unsigned char PROGMEM logo_bmp[] =
  43. { 0b00000000, 0b11000000,
  44.   0b00000001, 0b11000000,
  45.   0b00000001, 0b11000000,
  46.   0b00000011, 0b11100000,
  47.   0b11110011, 0b11100000,
  48.   0b11111110, 0b11111000,
  49.   0b01111110, 0b11111111,
  50.   0b00110011, 0b10011111,
  51.   0b00011111, 0b11111100,
  52.   0b00001101, 0b01110000,
  53.   0b00011011, 0b10100000,
  54.   0b00111111, 0b11100000,
  55.   0b00111111, 0b11110000,
  56.   0b01111100, 0b11110000,
  57.   0b01110000, 0b01110000,
  58.   0b00000000, 0b00110000
  59. };
  60.  
  61. #define ctr_oled 19
  62.  
  63. int pinState = LOW;
  64. unsigned long cur_time, old_time;
  65.  
  66. void setup() {
  67.   Serial.begin(115200);
  68.   pinMode(ctr_oled, OUTPUT);
  69.   digitalWrite(ctr_oled, HIGH);
  70.   delay(1000);
  71.  
  72.   // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  73.   if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
  74.     Serial.println(F("SSD1306 allocation failed"));
  75.     for (;;); // Don't proceed, loop forever
  76.   }
  77.  
  78.   // Show initial display buffer contents on the screen --
  79.   // the library initializes this with an Adafruit splash screen.
  80.   display.display();
  81.   delay(2000); // Pause for 2 seconds
  82.  
  83.   // Clear the buffer
  84.   display.clearDisplay();
  85.  
  86.   // Draw a single pixel in white
  87.   display.drawPixel(10, 10, SSD1306_WHITE);
  88.  
  89.   // Show the display buffer on the screen. You MUST call display() after
  90.   // drawing commands to make them visible on screen!
  91.   display.display();
  92.   delay(2000);
  93.   // display.display() is NOT necessary after every single drawing command,
  94.   // unless that's what you want...rather, you can batch up a bunch of
  95.   // drawing operations and then update the screen all at once by calling
  96.   // display.display(). These examples demonstrate both approaches...
  97.  
  98.   //testdrawline();      // Draw many lines
  99.  
  100.   //testdrawrect();      // Draw rectangles (outlines)
  101.  
  102.   //testfillrect();      // Draw rectangles (filled)
  103.  
  104.   //testdrawcircle();    // Draw circles (outlines)
  105.  
  106.   //testfillcircle();    // Draw circles (filled)
  107.  
  108.   // testdrawroundrect(); // Draw rounded rectangles (outlines)
  109.  
  110.   //testfillroundrect(); // Draw rounded rectangles (filled)
  111.  
  112.   //testdrawtriangle();  // Draw triangles (outlines)
  113.  
  114.   //testfilltriangle();  // Draw triangles (filled)
  115.  
  116.   //testdrawchar();      // Draw characters of the default font
  117.  
  118.   //testdrawstyles();    // Draw 'stylized' characters
  119.  
  120.   //testscrolltext();    // Draw scrolling text
  121.  
  122.   //testdrawbitmap();    // Draw a small bitmap image
  123.  
  124.   // Invert and restore display, pausing in-between
  125.   display.invertDisplay(true);
  126.   delay(1000);
  127.   display.invertDisplay(false);
  128.   delay(1000);
  129.  
  130.   //testanimate(logo_bmp, LOGO_WIDTH, LOGO_HEIGHT); // Animate bitmaps
  131. }
  132.  
  133. void loop() {
  134.  
  135.   cur_time = millis();
  136.   if (cur_time - old_time > 5000) {
  137.     if (pinState == LOW) {
  138.       Serial.println("oled nyala");
  139.       pinState = HIGH;
  140.       //init ulang oled
  141.     } else {
  142.       Serial.println("oled off");
  143.       pinState = LOW;
  144.     }
  145.  
  146.     digitalWrite(ctr_oled, pinState);
  147.     delay(1000);
  148.     Serial.print("pin Status: ");
  149.     Serial.println(pinState);
  150.    
  151.     if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
  152.       Serial.println(F("SSD1306 allocation failed"));
  153.       for (;;); // Don't proceed, loop forever
  154.     }
  155.     display.display();
  156.     delay(2000); // Pause for 2 seconds
  157.  
  158.     // Clear the buffer
  159.     display.clearDisplay();
  160.     testscrolltext();
  161.     testdrawline();
  162.     testfillcircle();
  163.     old_time = cur_time;
  164.   }
  165. }
  166.  
  167. void testdrawline() {
  168.   int16_t i;
  169.  
  170.   display.clearDisplay(); // Clear display buffer
  171.  
  172.   for (i = 0; i < display.width(); i += 4) {
  173.     display.drawLine(0, 0, i, display.height() - 1, SSD1306_WHITE);
  174.     display.display(); // Update screen with each newly-drawn line
  175.     delay(1);
  176.   }
  177.   for (i = 0; i < display.height(); i += 4) {
  178.     display.drawLine(0, 0, display.width() - 1, i, SSD1306_WHITE);
  179.     display.display();
  180.     delay(1);
  181.   }
  182.   delay(250);
  183.  
  184.   display.clearDisplay();
  185.  
  186.   for (i = 0; i < display.width(); i += 4) {
  187.     display.drawLine(0, display.height() - 1, i, 0, SSD1306_WHITE);
  188.     display.display();
  189.     delay(1);
  190.   }
  191.   for (i = display.height() - 1; i >= 0; i -= 4) {
  192.     display.drawLine(0, display.height() - 1, display.width() - 1, i, SSD1306_WHITE);
  193.     display.display();
  194.     delay(1);
  195.   }
  196.   delay(250);
  197.  
  198.   display.clearDisplay();
  199.  
  200.   for (i = display.width() - 1; i >= 0; i -= 4) {
  201.     display.drawLine(display.width() - 1, display.height() - 1, i, 0, SSD1306_WHITE);
  202.     display.display();
  203.     delay(1);
  204.   }
  205.   for (i = display.height() - 1; i >= 0; i -= 4) {
  206.     display.drawLine(display.width() - 1, display.height() - 1, 0, i, SSD1306_WHITE);
  207.     display.display();
  208.     delay(1);
  209.   }
  210.   delay(250);
  211.  
  212.   display.clearDisplay();
  213.  
  214.   for (i = 0; i < display.height(); i += 4) {
  215.     display.drawLine(display.width() - 1, 0, 0, i, SSD1306_WHITE);
  216.     display.display();
  217.     delay(1);
  218.   }
  219.   for (i = 0; i < display.width(); i += 4) {
  220.     display.drawLine(display.width() - 1, 0, i, display.height() - 1, SSD1306_WHITE);
  221.     display.display();
  222.     delay(1);
  223.   }
  224.  
  225.   delay(2000); // Pause for 2 seconds
  226. }
  227.  
  228. void testdrawrect(void) {
  229.   display.clearDisplay();
  230.  
  231.   for (int16_t i = 0; i < display.height() / 2; i += 2) {
  232.     display.drawRect(i, i, display.width() - 2 * i, display.height() - 2 * i, SSD1306_WHITE);
  233.     display.display(); // Update screen with each newly-drawn rectangle
  234.     delay(1);
  235.   }
  236.  
  237.   delay(2000);
  238. }
  239.  
  240. void testfillrect(void) {
  241.   display.clearDisplay();
  242.  
  243.   for (int16_t i = 0; i < display.height() / 2; i += 3) {
  244.     // The INVERSE color is used so rectangles alternate white/black
  245.     display.fillRect(i, i, display.width() - i * 2, display.height() - i * 2, SSD1306_INVERSE);
  246.     display.display(); // Update screen with each newly-drawn rectangle
  247.     delay(1);
  248.   }
  249.  
  250.   delay(2000);
  251. }
  252.  
  253. void testdrawcircle(void) {
  254.   display.clearDisplay();
  255.  
  256.   for (int16_t i = 0; i < max(display.width(), display.height()) / 2; i += 2) {
  257.     display.drawCircle(display.width() / 2, display.height() / 2, i, SSD1306_WHITE);
  258.     display.display();
  259.     delay(1);
  260.   }
  261.  
  262.   delay(2000);
  263. }
  264.  
  265. void testfillcircle(void) {
  266.   display.clearDisplay();
  267.  
  268.   for (int16_t i = max(display.width(), display.height()) / 2; i > 0; i -= 3) {
  269.     // The INVERSE color is used so circles alternate white/black
  270.     display.fillCircle(display.width() / 2, display.height() / 2, i, SSD1306_INVERSE);
  271.     display.display(); // Update screen with each newly-drawn circle
  272.     delay(1);
  273.   }
  274.  
  275.   delay(2000);
  276. }
  277.  
  278. void testdrawroundrect(void) {
  279.   display.clearDisplay();
  280.  
  281.   for (int16_t i = 0; i < display.height() / 2 - 2; i += 2) {
  282.     display.drawRoundRect(i, i, display.width() - 2 * i, display.height() - 2 * i,
  283.                           display.height() / 4, SSD1306_WHITE);
  284.     display.display();
  285.     delay(1);
  286.   }
  287.  
  288.   delay(2000);
  289. }
  290.  
  291. void testfillroundrect(void) {
  292.   display.clearDisplay();
  293.  
  294.   for (int16_t i = 0; i < display.height() / 2 - 2; i += 2) {
  295.     // The INVERSE color is used so round-rects alternate white/black
  296.     display.fillRoundRect(i, i, display.width() - 2 * i, display.height() - 2 * i,
  297.                           display.height() / 4, SSD1306_INVERSE);
  298.     display.display();
  299.     delay(1);
  300.   }
  301.  
  302.   delay(2000);
  303. }
  304.  
  305. void testdrawtriangle(void) {
  306.   display.clearDisplay();
  307.  
  308.   for (int16_t i = 0; i < max(display.width(), display.height()) / 2; i += 5) {
  309.     display.drawTriangle(
  310.       display.width() / 2  , display.height() / 2 - i,
  311.       display.width() / 2 - i, display.height() / 2 + i,
  312.       display.width() / 2 + i, display.height() / 2 + i, SSD1306_WHITE);
  313.     display.display();
  314.     delay(1);
  315.   }
  316.  
  317.   delay(2000);
  318. }
  319.  
  320. void testfilltriangle(void) {
  321.   display.clearDisplay();
  322.  
  323.   for (int16_t i = max(display.width(), display.height()) / 2; i > 0; i -= 5) {
  324.     // The INVERSE color is used so triangles alternate white/black
  325.     display.fillTriangle(
  326.       display.width() / 2  , display.height() / 2 - i,
  327.       display.width() / 2 - i, display.height() / 2 + i,
  328.       display.width() / 2 + i, display.height() / 2 + i, SSD1306_INVERSE);
  329.     display.display();
  330.     delay(1);
  331.   }
  332.  
  333.   delay(2000);
  334. }
  335.  
  336. void testdrawchar(void) {
  337.   display.clearDisplay();
  338.  
  339.   display.setTextSize(1);      // Normal 1:1 pixel scale
  340.   display.setTextColor(SSD1306_WHITE); // Draw white text
  341.   display.setCursor(0, 0);     // Start at top-left corner
  342.   display.cp437(true);         // Use full 256 char 'Code Page 437' font
  343.  
  344.   // Not all the characters will fit on the display. This is normal.
  345.   // Library will draw what it can and the rest will be clipped.
  346.   for (int16_t i = 0; i < 256; i++) {
  347.     if (i == '\n') display.write(' ');
  348.     else          display.write(i);
  349.   }
  350.  
  351.   display.display();
  352.   delay(2000);
  353. }
  354.  
  355. void testdrawstyles(void) {
  356.   display.clearDisplay();
  357.  
  358.   display.setTextSize(1);             // Normal 1:1 pixel scale
  359.   display.setTextColor(SSD1306_WHITE);        // Draw white text
  360.   display.setCursor(0, 0);            // Start at top-left corner
  361.   display.println(F("Hello, world!"));
  362.  
  363.   display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
  364.   display.println(3.141592);
  365.  
  366.   display.setTextSize(2);             // Draw 2X-scale text
  367.   display.setTextColor(SSD1306_WHITE);
  368.   display.print(F("0x")); display.println(0xDEADBEEF, HEX);
  369.  
  370.   display.display();
  371.   delay(2000);
  372. }
  373.  
  374. void testscrolltext(void) {
  375.   display.clearDisplay();
  376.  
  377.   display.setTextSize(2); // Draw 2X-scale text
  378.   display.setTextColor(SSD1306_WHITE);
  379.   display.setCursor(10, 0);
  380.   display.println(F("scroll"));
  381.   display.display();      // Show initial text
  382.   delay(100);
  383.  
  384.   // Scroll in various directions, pausing in-between:
  385.   display.startscrollright(0x00, 0x0F);
  386.   delay(2000);
  387.   display.stopscroll();
  388.   delay(1000);
  389.   display.startscrollleft(0x00, 0x0F);
  390.   delay(2000);
  391.   display.stopscroll();
  392.   delay(1000);
  393.   display.startscrolldiagright(0x00, 0x07);
  394.   delay(2000);
  395.   display.startscrolldiagleft(0x00, 0x07);
  396.   delay(2000);
  397.   display.stopscroll();
  398.   delay(1000);
  399. }
  400.  
  401. void testdrawbitmap(void) {
  402.   display.clearDisplay();
  403.  
  404.   display.drawBitmap(
  405.     (display.width()  - LOGO_WIDTH ) / 2,
  406.     (display.height() - LOGO_HEIGHT) / 2,
  407.     logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);
  408.   display.display();
  409.   delay(1000);
  410. }
  411.  
  412. #define XPOS   0 // Indexes into the 'icons' array in function below
  413. #define YPOS   1
  414. #define DELTAY 2
  415.  
  416. void testanimate(const uint8_t *bitmap, uint8_t w, uint8_t h) {
  417.   int8_t f, icons[NUMFLAKES][3];
  418.  
  419.   // Initialize 'snowflake' positions
  420.   for (f = 0; f < NUMFLAKES; f++) {
  421.     icons[f][XPOS]   = random(1 - LOGO_WIDTH, display.width());
  422.     icons[f][YPOS]   = -LOGO_HEIGHT;
  423.     icons[f][DELTAY] = random(1, 6);
  424.     Serial.print(F("x: "));
  425.     Serial.print(icons[f][XPOS], DEC);
  426.     Serial.print(F(" y: "));
  427.     Serial.print(icons[f][YPOS], DEC);
  428.     Serial.print(F(" dy: "));
  429.     Serial.println(icons[f][DELTAY], DEC);
  430.   }
  431.  
  432.   for (;;) { // Loop forever...
  433.     display.clearDisplay(); // Clear the display buffer
  434.  
  435.     // Draw each snowflake:
  436.     for (f = 0; f < NUMFLAKES; f++) {
  437.       display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, SSD1306_WHITE);
  438.     }
  439.  
  440.     display.display(); // Show the display buffer on the screen
  441.     delay(200);        // Pause for 1/10 second
  442.  
  443.     // Then update coordinates of each flake...
  444.     for (f = 0; f < NUMFLAKES; f++) {
  445.       icons[f][YPOS] += icons[f][DELTAY];
  446.       // If snowflake is off the bottom of the screen...
  447.       if (icons[f][YPOS] >= display.height()) {
  448.         // Reinitialize to a random position, just off the top
  449.         icons[f][XPOS]   = random(1 - LOGO_WIDTH, display.width());
  450.         icons[f][YPOS]   = -LOGO_HEIGHT;
  451.         icons[f][DELTAY] = random(1, 6);
  452.       }
  453.     }
  454.   }
  455. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement