#include <SPI.h>
#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 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define NUMFLAKES 10 // Number of snowflakes in the animation example
#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16
int XPOS = 0 ;
int YPOS = 0;
bool RIGHT = true;
bool LEFT = false;
bool UP = false;
bool DOWN = false;
static const unsigned char PROGMEM logo_bmp[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000 };
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.display();
delay(2000); // Pause for 2 seconds
display.clearDisplay();
display.display();
delay(2000);
initBitmap(); // Draw a small bitmap image
}
void initBitmap(void) {
display.clearDisplay();
display.drawBitmap(
0, 0,
logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);
display.display();
}
void loop() {
animateBitmap();
}
void animateBitmap(void) {
display.clearDisplay();
if (RIGHT) {
if (XPOS >= 128 - LOGO_WIDTH){
RIGHT = false;
LEFT = false;
UP = false;
DOWN = true;
goDown();
} else {
goRight();
}
}
if (DOWN) {
if (YPOS >= 64-LOGO_HEIGHT){
RIGHT = false;
LEFT = true;
UP = false;
DOWN = false;
goLeft();
} else {
goDown();
}
}
if (LEFT) {
if (XPOS <= 0){
RIGHT = false;
LEFT = false;
UP = true;
DOWN = false;
goUp();
} else {
goLeft();
}
}
if (UP) {
if (YPOS <= 0){
RIGHT = true;
LEFT = false;
UP = false;
DOWN = false;
goRight();
} else {
goUp();
}
}
display.display();
delay(1);
}
void goRight(void) {
XPOS++;
display.drawBitmap(XPOS, YPOS, logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);
}
void goLeft(void) {
XPOS--;
display.drawBitmap(XPOS, YPOS, logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);
}
void goDown(void) {
YPOS++;
display.drawBitmap(XPOS, YPOS, logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);
}
void goUp(void) {
YPOS--;
display.drawBitmap(XPOS, YPOS, logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);
}