microrobotics icon

ESP32-EXPRESS-MINI Kit Sample Code

microrobotics | PRO | 06/04/24 11:11:00 AM UTC (Edited) | 0 ⭐ | 13745 👁️ | Never ⏰ | []
Arduino |

2.02 KB

|

None

|

0 👍

/

0 👎

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
 
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
 
// Smiley bitmap: 8x8 pixels
const uint8_t smiley[8] = {
  0b00111100,
  0b01000010,
  0b10100101,
  0b10000001,
  0b10100101,
  0b10011001,
  0b01000010,
  0b00111100
};
 
int xPos = 0; // Initial x position
int yPos = 0; // Initial y position
int xMove = 2; // x movement speed
int yMove = 1; // y movement speed
int scale = 4; // Scaling factor for the bitmap
 
void setup() {
  Serial.begin(9600);
  
  // Initialize OLED display
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
 
  display.clearDisplay(); // Clear the buffer
}
 
void loop() {
  display.clearDisplay(); // Clear the display buffer
 
  // Update the position
  xPos += xMove;
  yPos += yMove;
 
  // Boundary detection for bouncing
  if (xPos <= 0 || xPos >= SCREEN_WIDTH - 8 * scale) {
    xMove = -xMove; // Reverse direction on x-axis
  }
  if (yPos <= 0 || yPos >= SCREEN_HEIGHT - 8 * scale) {
    yMove = -yMove; // Reverse direction on y-axis
  }
 
  // Draw the scaled bitmap at the new position
  displayScaledBitmap(xPos, yPos, scale);
  display.display(); // Show the updated frame
 
  delay(10); // Delay to control animation speed
}
 
void displayScaledBitmap(int x, int y, int scaleFactor) {
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      if (smiley[i] & (1 << (7 - j))) { // Check each bit (note the bit order reversal)
        display.fillRect(x + j * scaleFactor, y + i * scaleFactor, scaleFactor, scaleFactor, SSD1306_WHITE);
      }
    }
  }
}
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎