Unicorn Matrix Animasi — ESP32 simulation

ESP32 mengendalikan NeoPixel matrix 7x72 dengan tombol ganti animasi.

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

Components

Sketch code

#include <FastLED.h>

#define DATA_PIN 21
#define NUM_LEDS 64

CRGB leds[NUM_LEDS];

// Fungsi pemetaan koordinat X,Y ke Indeks LED
uint16_t XY(uint8_t x, uint8_t y) {
  // Jika susunan LED kamu Zig-zag, buka komentar baris di bawah ini:
  // if (y % 2 == 1) return (y * WIDTH) + (WIDTH - 1 - x); 
  
  return (y * WIDTH) + x; // Susunan standar (Progressive)
}

void pixel(uint8_t x, uint8_t y, CRGB warna) {
  if (x < WIDTH && y < HEIGHT) {
    leds[XY(x, y)] = warna;
  }
}

void mataTerbuka(int arah) {
  fill_solid(leds, NUM_LEDS, CRGB::Black);

  // Pembatasan agar lirikan tidak keluar dari batas
  if (arah > 0) arah = 1;
  if (arah < 0) arah = -1;

  // Bingkai atas (merah)
  pixel(0, 0, CRGB::Red);
  pixel(1, 0, CRGB::Red);
  pixel(4, 0, CRGB::Red);
  pixel(5, 0, CRGB::Red);

  // Latar belakang bola mata putih (diperluas dari x=0 hingga x=5 mengikuti arah)
  for (uint8_t x = 0; x < WIDTH; x++) {
    pixel(x, 1, CRGB::White);
  }

  // Iris/pupil merah (bergeser sesuai parameter arah)
  pixel(1 + arah, 1, CRGB::Red);
  pixel(4 + arah, 1, CRGB::Red);

  // Bagian bawah mata (merah)
  pixel(1, 2, CRGB::Red);
  pixel(2, 2, CRGB::Red);
  pixel(3, 2, CRGB::Red);
  pixel(4, 2, CRGB::Red);

  FastLED.show();
}

void mataTertutup() {
  fill_solid(leds, NUM_LEDS, CRGB::Black);

  for (uint8_t x = 0; x < WIDTH; x++) {
    pixel(x, 2, CRGB::Red);
  }

  FastLED.show();
}

void stroboKiri() {
  fill_solid(leds, NUM_LEDS, CRGB::Black);

  pixel(0, 0, CRGB::Red);
  pixel(1, 0, CRGB::Red);
  pixel(0, 1, CRGB::Red);
  pixel(1, 1, CRGB::White);
  pixel(1, 2, CRGB::Red);

  FastLED.show();
}

void stroboKanan() {
  fill_solid(leds, NUM_LEDS, CRGB::Black);

  pixel(4, 0, CRGB::Red);
  pixel(5, 0, CRGB::Red);
  pixel(4, 1, CRGB::White);
  pixel(5, 1, CRGB::Red);
  pixel(4, 2, CRGB::Red);

  FastLED.show();
}

void setup() {
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(80);
  FastLED.clear(true);
}

void loop() {
  mataTerbuka(0);    // Lurus
  delay(1000);

  mataTerbuka(1);    // Melirik Kanan
  delay(600);

  mataTerbuka(-1);   // Melirik Kiri
  delay(600);

  mataTertutup();    // Kedip
  delay(200);

  mataTerbuka(0);    // Kembali Lurus
  delay(700);

  // Mode Strobo
  for (int i = 0; i < 5; i++) {
    stroboKiri();
    delay(120);

    stroboKanan();
    delay(120);
  }
}

More projects by rsetyo-widayanto