// Multiple Animations (with Adafruit GFX library)
//
// The sketch is from the Wokwi Animator: https://animator.wokwi.com/
// The address of the OLED display was set to 0x3C
// The Christmas animations are from:
// https://icons8.com/icons/set/christmas--style-glyph-neue--animated--mono
// A number of mono color GIF of 64x64 were downloaded.
// They were transferred into individual files with ImageMagick:
// magick icons8-christmas.gif -coalesce ani_%03d.png
// Those png files were loaded into: https://javl.github.io/image2cpp/
// Multiple files can be uploaded at once (I learned that today on the Wokwi Discord).
// The Background color and Inverting colors is still confusing for me.
// The resulting code was placed in a *.h file in the project.
// The variables for the length and the array were adjusted.
// The sketch is further adapted to allow switching between animations by a button.
//
// See also the same project with the u8g2 library:
// https://wokwi.com/projects/446968915922376705
//
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Bounce2.h>
#include "ani_sock.h"
#include "ani_ball.h"
#include "ani_bell.h"
#include "ani_champagne.h"
#include "ani_candle.h"
#include "ani_candy.h"
#include "ani_sparkle.h"
const int buttonPin = 9;
#define SCREEN_I2C_ADDR 0x3C // or 0x3C
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RST_PIN -1 // Reset pin (-1 if not available)
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RST_PIN);
#define FRAME_WIDTH 64
#define FRAME_HEIGHT 64
const int num_animations = 7;
int animation;
const unsigned char** frames;
int frame_count;
int frame;
unsigned long previousMillis; // millis-timer for animation
const unsigned long interval = 20; // interval for animation
const unsigned char** animations[num_animations] =
{
sock_Array,
ball_Array,
bell_Array,
champagneallArray,
candleallArray,
candy_Array,
sparkleallArray,
};
int lengths[num_animations] =
{
sock_Len,
ball_Len,
bell_Len,
champagneallArray_LEN,
candleallArray_LEN,
candy_Len,
sparkleallArray_LEN,
};
// INSTANTIATE A Button OBJECT FROM THE Bounce2 NAMESPACE
Bounce2::Button button = Bounce2::Button();
void setup()
{
button.attach(buttonPin, INPUT_PULLUP);
button.interval(5); // interval in ms
// How does the pressed state influence the fell() ?
// button.setPressedState(LOW);
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_I2C_ADDR);
animation = 0;
frames = animations[animation];
frame_count = lengths[animation];
frame = 0;
}
void loop()
{
unsigned long currentMillis = millis();
// Update the Bounce instance
button.update();
if(currentMillis - previousMillis >= interval)
{
display.clearDisplay();
display.drawBitmap(32, 0, frames[frame], FRAME_WIDTH, FRAME_HEIGHT, 1);
display.display();
frame++;
if(frame >= frame_count)
frame = 0;
}
if(button.fell())
{
animation++;
if(animation >= num_animations)
animation = 0;
frames = animations[animation];
frame_count = lengths[animation];
frame = 0;
}
delay(5); // speed up the Wokwi simulation
}Press the button for a second or so.
with Adafruit GFX library