// Multiple Animations (with u8g2 library)
//
// For explanation and to compare it with the Adafruit GFX library,
// see the previous project:
// https://wokwi.com/projects/446922523692645377
//
// This project by 'upir' using the u8g2 library is used to start with:
// https://wokwi.com/projects/374294166215201793
//
// I could not use the bitmaps from the previous project,
// since the u8g2 library has a different bit order.
// Everything had to be converted all over again.
//
#include <Wire.h>
#include <U8g2lib.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"
#include "ani_cork.h"
const int buttonPin = 9;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C display(U8G2_R0, U8X8_PIN_NONE);
const int num_animations = 8;
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] =
{
sockallArray,
ballallArray,
bellallArray,
champagneallArray,
candleallArray,
candyallArray,
sparklingallArray,
corkallArray,
};
int lengths[num_animations] =
{
sockallArray_LEN,
ballallArray_LEN,
bellallArray_LEN,
champagneallArray_LEN,
candleallArray_LEN,
candyallArray_LEN,
sparklingallArray_LEN,
corkallArray_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(); // start the u8g2 library
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.clearBuffer(); // clear the internal memory
display.drawXBMP(30, 0, 64, 64, frames[frame]); // draw frame of the animation
display.sendBuffer(); // transfer internal memory to the 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 u8g2 library
🎄
🎅
🍾
⛄
🔔