/*
Wokwi | questions
Multiple gifs, but with a JOYSTICK?! (multiple inputs test)
Mikael — 11/15/2025 2:06 PM
*/
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//#include <Bounce2.h>
#include "yes.h"
#include "smirk64.h"
#include "embarrassed.h"
#include "MIKAEL.h"
const int JOY_X_PIN = 0; // Arduino boards use A10 or A0
const int JOY_Y_PIN = 1;
const int DEADZONE = 400;
int joyX = 0;
int joyY = 0;
int oldXval = 0;
int oldYval = 0;
const int BTN_PIN = 10; // joystick SW pin
//Bounce button = Bounce();
#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 = 4;
int animation;
const unsigned char** frames;
int frame_count;
int frame;
int oldSwVal = HIGH;
unsigned long previousMillis; // millis-timer for animation
const unsigned long interval = 20; // interval for animation
const unsigned char** animations[num_animations] =
{
smirk64_Array,
yes64_Array,
ani_embarressed_Array,
Mikael_Array,
};
int lengths[num_animations] =
{
smirk64_Len,
yes64_Len,
ani_embarressed_Len,
Mikael_Len,
};
void setup() {
Serial.begin(9600); // the text doesnt display without this
//(serial communication between ESP and your computer, 9600 baud per second)
Wire.begin(3, 2); // SDA = GPIO8, SCL = GPIO9
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
animation = 0;
frames = animations[animation];
frame_count = lengths[animation];
frame = 0;
pinMode(BTN_PIN, INPUT_PULLUP);
}
void loop() {
unsigned long currentMillis = millis();
joyX = analogRead(JOY_X_PIN);
//Serial.print("X value: ");
//Serial.println(joyX);
joyY = analogRead(JOY_Y_PIN);
//Serial.print("Y value: ");
//Serial.println(joyY);
// Update the Bounce instance
//button.update();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
display.clearDisplay(); // reset the OLED
display.drawBitmap(32, 0, frames[frame], FRAME_WIDTH, FRAME_HEIGHT, 1); // puts the animation in the right place
display.display(); // pushes the animation to the OLED
frame++;
if (frame >= frame_count)
frame = 0;
}
int joyXvalue = analogRead(JOY_X_PIN);
// if the joystick has changed...
if (joyXvalue > (oldXval + 5) || joyXvalue < (oldXval - 5)) {
oldXval = joyXvalue;
frames = animations[animation];
frame_count = lengths[animation];
frame = 0;
if (joyXvalue > 4000) {
Serial.println("Left");
animation++;
if (animation >= num_animations)
animation = 0;
}
if (joyXvalue < 100) {
Serial.println("Right");
animation--;
if (animation < 0 )
animation = num_animations - 1;
}
}
int joyYvalue = analogRead(JOY_Y_PIN);
if (joyYvalue > (oldYval + 5) || joyYvalue < (oldYval - 5)) {
oldYval = joyYvalue;
frames = animations[animation];
frame_count = lengths[animation];
frame = 0;
if (joyYvalue > 4000) {
Serial.println("Up");
animation++;
if (animation >= num_animations)
animation = 0;
}
if (joyYvalue < 100) {
Serial.println("Down");
animation--;
if (animation < 0 )
animation = num_animations - 1;
}
}
int switchValue = digitalRead(BTN_PIN);
if (switchValue != oldSwVal) {
oldSwVal = switchValue;
if (switchValue == LOW) {
Serial.println("Button pressed");
}
delay(20);
}
}