ESP32 Animation Switcher — ESP32 simulation

An ESP32 project with an ILI9341 display and a button to switch animations.

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by rsetyo-widayanto.

Components

Sketch code

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>

#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST 4
#define BUTTON_PIN 5

Adafruit_ILI9341 tft(TFT_CS, TFT_DC, TFT_RST);
uint8_t modeEfek = 0;
bool tombolLama = HIGH;
unsigned long waktuTombol = 0, mulaiAdegan = 0;

const int CY = 120, posLX = 82, posRX = 238, EYE_W = 126, EYE_H = 76;


void mata(int cx, int buka, int pupilX, uint16_t warnaMata, bool marah) {
  int h = map(buka, 0, 100, 2, EYE_H);
  int y = CY - h / 2;
  tft.fillRect(cx - EYE_W / 2 - 4, CY - EYE_H / 2 - 8,
               EYE_W + 8, EYE_H + 16, ILI9341_BLACK);
  if (h <= 4) {
    tft.drawFastHLine(cx - EYE_W / 2, CY, EYE_W, warnaMata);
    tft.drawFastHLine(cx - EYE_W / 2 + 8, CY + 2, EYE_W - 16, warnaMata);
    return;
  }
  tft.fillRoundRect(cx - EYE_W / 2, y, EYE_W, h, h / 2, warnaMata);
  int pr = min(24, h / 2 - 3);
  if (pr > 2) {
    tft.fillCircle(cx + pupilX, CY, pr, ILI9341_BLACK);
    tft.fillCircle(cx + pupilX + 6, CY - 7, max(2, pr / 4), ILI9341_WHITE);
  }
  if (marah) {
    if (cx < 160)
      tft.fillTriangle(cx - 67, CY - 48, cx + 67, CY - 48,
                       cx + 67, CY - 20, ILI9341_BLACK);
    else
      tft.fillTriangle(cx - 67, CY - 48, cx + 67, CY - 48,
                       cx - 67, CY - 20, ILI9341_BLACK);
  }
}

void sepasang(int buka, int arah, uint16_t kiri, uint16_t kanan) {
  mata(LX, buka, arah, kiri, true);
  mata(posLX, buka, arah, kiri, true);
  mata(posRX, buka, arah, kanan, true);
  mata(posRX, buka, arah, kanan, true);

void labelMode() {
  tft.fillRect(0, 0, 320, 28, ILI9341_BLACK);
  tft.setTextSize(2);
  tft.setTextColor(ILI9341_WHITE);
  tft.setCursor(8, 7);
  if (modeEfek == 0) tft.print("MATA BUS RGB");
  else if (modeEfek == 1) tft.print("STROBO POLISI");
  else tft.print("MATA ROBOT");
}

void animasiRGB(unsigned long t) {
  uint8_t fase = (t / 900) % 8;
  int buka = 100, arah = 0;
  if (fase == 1) arah = -20;
  if (fase == 2) arah = 20;
  if (fase == 3) buka = 12;
  if (fase == 6) buka = 35;
  uint16_t warna[] = {
    tft.color565(0, 255, 210),
    tft.color565(255, 20, 150),
    tft.color565(120, 255, 0)
  };
  uint8_t i = (t / 300) % 3;
  sepasang(buka, arah, warna[i], warna[(i + 1) % 3]);
}

void animasiPolisi(unsigned long t) {
  uint8_t k = (t / 85) % 8;
  bool kiriNyala = k < 3;
  bool kananNyala = k >= 4 && k < 7;
  int buka = (k == 3 || k == 7) ? 8 : 100;
  sepasang(buka, 0,
    kiriNyala ? ILI9341_RED : tft.color565(35, 0, 0),
    kananNyala ? ILI9341_BLUE : tft.color565(0, 0, 35));
}

void animasiRobot(unsigned long t) {
  unsigned long s = t % 6000;
  int buka = 100, arah = 0;
  if (s < 500) buka = map(s, 0, 500, 5, 100);
  else if (s < 1700) arah = map(s, 500, 1700, 0, -22);
  else if (s < 2900) arah = map(s, 1700, 2900, -22, 22);
  else if (s < 4100) arah = map(s, 2900, 4100, 22, 0);
  else if (s < 4300) buka = map(s, 4100, 4300, 100, 5);
  else if (s < 4500) buka = map(s, 4300, 4500, 5, 100);
  uint8_t nyala = 110 + (sin(t / 180.0) + 1.0) *

More projects by rsetyo-widayanto