Advertisement
ccarman602

OLED Falling Stars

Jun 3rd, 2020
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*********************************************************************
  2. This is a modified form of Adafruit's OLED demo to only show the falling stars.
  3. Original header comments:
  4.  
  5. This is an example for our Monochrome OLEDs based on SSD1306 drivers
  6.  
  7.   Pick one up today in the adafruit shop!
  8.   ------> http://www.adafruit.com/category/63_98
  9.  
  10. This example is for a 128x64 size display using SPI to communicate
  11. 4 or 5 pins are required to interface
  12.  
  13. Adafruit invests time and resources providing this open source code,
  14. please support Adafruit and open-source hardware by purchasing
  15. products from Adafruit!
  16.  
  17. Written by Limor Fried/Ladyada  for Adafruit Industries.  
  18. BSD license, check license.txt for more information
  19. All text above, and the splash screen must be included in any redistribution
  20. *********************************************************************/
  21.  
  22. #include <SPI.h>
  23. #include <Wire.h>
  24. #include <Adafruit_GFX.h>
  25. #include <Adafruit_SSD1306.h>
  26.  
  27. // If using software SPI (the default case):
  28. #define OLED_MOSI   9
  29. #define OLED_CLK   10
  30. #define OLED_DC    11
  31. #define OLED_CS    12
  32. #define OLED_RESET 13
  33. Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
  34.  
  35. /* Uncomment this block to use hardware SPI
  36. #define OLED_DC     6
  37. #define OLED_CS     7
  38. #define OLED_RESET  8
  39. Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS);
  40. */
  41.  
  42. #define NUMFLAKES 10
  43. #define XPOS 0
  44. #define YPOS 1
  45. #define DELTAY 2
  46.  
  47. #define LOGO16_GLCD_HEIGHT 16
  48. #define LOGO16_GLCD_WIDTH  16
  49. static const unsigned char PROGMEM logo16_glcd_bmp[] =
  50. { B00000000, B11000000,
  51.   B00000001, B11000000,
  52.   B00000001, B11000000,
  53.   B00000011, B11100000,
  54.   B11110011, B11100000,
  55.   B11111110, B11111000,
  56.   B01111110, B11111111,
  57.   B00110011, B10011111,
  58.   B00011111, B11111100,
  59.   B00001101, B01110000,
  60.   B00011011, B10100000,
  61.   B00111111, B11100000,
  62.   B00111111, B11110000,
  63.   B01111100, B11110000,
  64.   B01110000, B01110000,
  65.   B00000000, B00110000 };
  66.  
  67. #define SSD1306_LCDHEIGHT 64
  68. #if (SSD1306_LCDHEIGHT != 64)
  69. #error("Height incorrect, please fix Adafruit_SSD1306.h!");
  70. #endif
  71.  
  72. void setup()   {                
  73.   Serial.begin(9600);
  74.  
  75.   // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  76.   display.begin(SSD1306_SWITCHCAPVCC);
  77.   display.clearDisplay();
  78.   // init done
  79.  
  80.   // draw a bitmap icon and 'animate' movement
  81.   testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH);
  82. }
  83.  
  84. void loop() {
  85. }
  86.  
  87. void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
  88.   uint8_t icons[NUMFLAKES][3];
  89.  
  90.   // initialize
  91.   for (uint8_t f=0; f< NUMFLAKES; f++) {
  92.     icons[f][XPOS] = random(display.width());
  93.     icons[f][YPOS] = 0;
  94.     icons[f][DELTAY] = random(5) + 1;
  95.    
  96.     Serial.print("x: ");
  97.     Serial.print(icons[f][XPOS], DEC);
  98.     Serial.print(" y: ");
  99.     Serial.print(icons[f][YPOS], DEC);
  100.     Serial.print(" dy: ");
  101.     Serial.println(icons[f][DELTAY], DEC);
  102.   }
  103.  
  104.   while (1) {
  105.     // draw each icon
  106.     for (uint8_t f=0; f< NUMFLAKES; f++) {
  107.       display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE);
  108.     }
  109.     display.display();
  110.     delay(200);
  111.    
  112.     // then erase it + move it
  113.     for (uint8_t f=0; f< NUMFLAKES; f++) {
  114.       display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, BLACK);
  115.       // move it
  116.       icons[f][YPOS] += icons[f][DELTAY];
  117.       // if its gone, reinit
  118.       if (icons[f][YPOS] > display.height()) {
  119.         icons[f][XPOS] = random(display.width());
  120.         icons[f][YPOS] = 0;
  121.         icons[f][DELTAY] = random(5) + 1;
  122.       }
  123.     }
  124.    }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement