Advertisement
microrobotics

ESP32-EXPRESS-MINI Kit Sample Code

May 2nd, 2024 (edited)
1,618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include <Adafruit_GFX.h>
  3. #include <Adafruit_SSD1306.h>
  4.  
  5. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  6. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  7.  
  8. // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
  9. #define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
  10. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  11.  
  12. // Smiley bitmap: 8x8 pixels
  13. const uint8_t smiley[8] = {
  14.   0b00111100,
  15.   0b01000010,
  16.   0b10100101,
  17.   0b10000001,
  18.   0b10100101,
  19.   0b10011001,
  20.   0b01000010,
  21.   0b00111100
  22. };
  23.  
  24. int xPos = 0; // Initial x position
  25. int yPos = 0; // Initial y position
  26. int xMove = 2; // x movement speed
  27. int yMove = 1; // y movement speed
  28. int scale = 4; // Scaling factor for the bitmap
  29.  
  30. void setup() {
  31.   Serial.begin(9600);
  32.  
  33.   // Initialize OLED display
  34.   if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
  35.     Serial.println(F("SSD1306 allocation failed"));
  36.     for (;;); // Don't proceed, loop forever
  37.   }
  38.  
  39.   display.clearDisplay(); // Clear the buffer
  40. }
  41.  
  42. void loop() {
  43.   display.clearDisplay(); // Clear the display buffer
  44.  
  45.   // Update the position
  46.   xPos += xMove;
  47.   yPos += yMove;
  48.  
  49.   // Boundary detection for bouncing
  50.   if (xPos <= 0 || xPos >= SCREEN_WIDTH - 8 * scale) {
  51.     xMove = -xMove; // Reverse direction on x-axis
  52.   }
  53.   if (yPos <= 0 || yPos >= SCREEN_HEIGHT - 8 * scale) {
  54.     yMove = -yMove; // Reverse direction on y-axis
  55.   }
  56.  
  57.   // Draw the scaled bitmap at the new position
  58.   displayScaledBitmap(xPos, yPos, scale);
  59.   display.display(); // Show the updated frame
  60.  
  61.   delay(10); // Delay to control animation speed
  62. }
  63.  
  64. void displayScaledBitmap(int x, int y, int scaleFactor) {
  65.   for (int i = 0; i < 8; i++) {
  66.     for (int j = 0; j < 8; j++) {
  67.       if (smiley[i] & (1 << (7 - j))) { // Check each bit (note the bit order reversal)
  68.         display.fillRect(x + j * scaleFactor, y + i * scaleFactor, scaleFactor, scaleFactor, SSD1306_WHITE);
  69.       }
  70.     }
  71.   }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement