#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>

// Definisikan pin hardware
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 12
#define CLK_PIN   10
#define DATA_PIN  13
#define CS_PIN    11

// Inisialisasi instance Parola
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);

// Inisialisasi pin sensor jarak
const int trigPin = 3;
const int echoPin = 2;

void setup() {
  // Mengatur pin sensor jarak
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  P.begin();
}

void loop() {
  // Mengirim sinyal ultrasonik
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Menerima sinyal ultrasonik yang dipantulkan
  long duration = pulseIn(echoPin, HIGH);

  // Menghitung jarak
  int distance = duration * 0.034 / 2;

  // Menampilkan jarak pada LED Matrix
  char distStr[10];
  sprintf(distStr, "Jarak: %d cm", distance);
  P.displayText(distStr, PA_CENTER, 100, 0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
  P.displayAnimate();

  delay(500);
}