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

//Define Button
#define btn1 0
#define btn2 1
#define btn3 2
#define btn4 3
#define btn5 4

// pin LED
#define led 9

// pin Buzzer
#define buzzer1 12

// Define pins for seven-segment display
#define C 7
#define D 8
#define E 15
#define G 16
#define F 14
#define A 5
#define B 6


// pin dotmatrix
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 4
#define CLK_PIN    13
#define DATA_PIN   11
#define CS_PIN     10

MD_Parola myParola = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);


//pin sensor
const int trigPin = 17;  // Pin untuk Trig
const int echoPin = 18;  // Pin untuk Echo





//Variable Dot Matrix
const char *nama = "Mahdi Isnain Justicil 21650150";


bool btn1Pressed = false;

void setup(){

  pinMode(btn1,INPUT_PULLUP);
  pinMode(btn2, INPUT_PULLUP);
  pinMode(btn3, INPUT_PULLUP);
  pinMode(btn4, INPUT_PULLUP);
  pinMode(btn5, INPUT_PULLUP);
  pinMode(led, OUTPUT);
  pinMode(buzzer1, OUTPUT);
   Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);


  // dot matrix
  myParola.begin();

  myParola.setSpeed(20);
  myParola.setPause(0);
  myParola.setIntensity(10);

  myParola.displayText(nama, PA_CENTER, 100, 0, PA_SCROLL_LEFT,PA_SCROLL_LEFT);

    // Set up pins for seven-segment display
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
  digitalWrite(A, HIGH);
 digitalWrite(B, HIGH);
 digitalWrite(C, HIGH);
 digitalWrite(D, HIGH);
 digitalWrite(E, HIGH);
 digitalWrite(F, HIGH);
 digitalWrite(G, HIGH);
}

void tampilkanJarakPadaDotMatrix(unsigned int distance) {
  char distanceStr[5]; // Buffer untuk mengonversi jarak menjadi string
  sprintf(distanceStr, "%4d", distance);
  myParola.displayText(distanceStr, PA_LEFT, 30, 0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
}


void loop() {
  
  // Check if btn1 is pressed

 if (digitalRead(btn1) == LOW) {
    btn1Pressed = !btn1Pressed;

    if (btn1Pressed != false) {
      myParola.displayAnimate();

      while(!myParola.displayAnimate()) {
        btn1Pressed = true;
      }
    };
  }
  // Check if btn2 is pressed
  if (digitalRead(btn2) == LOW) {
    // Blink the LED
    digitalWrite(led, HIGH);
    delay(500);
    digitalWrite(led, LOW);
    delay(500);
  }

  // Check if btn3 is pressed
  if (digitalRead(btn3) == LOW) {
    // Play a sound on buzzer1
    tone(buzzer1, 262, 500); // C
    delay(500);
    tone(buzzer1, 294, 500); // D
    delay(500);
    tone(buzzer1, 330, 500); // E
    delay(500);
    tone(buzzer1, 349, 500); // F
    delay(500);
    tone(buzzer1, 392, 500); // G
    delay(500);
    tone(buzzer1, 440, 500); // A
    delay(500);
    tone(buzzer1, 494, 500); // B
    delay(500);
    noTone(buzzer1); // Stop the buzzer
  }

  // // Check if btn4 is pressed
  if (digitalRead(btn4) == LOW) {

     // Baca jarak dari sensor ultrasonik
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    unsigned long duration = pulseIn(echoPin, HIGH);
    // Konversi durasi menjadi jarak (dalam cm)
    unsigned int distance = duration * 0.034 / 2;

    // Tampilkan hasil pada dot matrix
    tampilkanJarakPadaDotMatrix(distance);

    delay(1000);  // Tunggu 1 detik sebelum membaca lagi
  

  }

  // Check if btn5 is pressed
  if (digitalRead(btn5) == LOW) {
   

    
    digitalWrite(A, LOW);
    digitalWrite(B, LOW);
    digitalWrite(C, LOW);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(F, LOW);
    digitalWrite(G, HIGH);
    delay(500);
    digitalWrite(A, HIGH);
    digitalWrite(B, LOW);
    digitalWrite(C, LOW);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
    delay(500);
    digitalWrite(A, LOW);
    digitalWrite(B, LOW);
    digitalWrite(C, HIGH);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(F, HIGH);
    digitalWrite(G, LOW);
    delay(500);
    digitalWrite(A, LOW);
    digitalWrite(B, LOW);
    digitalWrite(C, LOW);
    digitalWrite(D, LOW);
    digitalWrite(E, HIGH);
    digitalWrite(F, HIGH);
    digitalWrite(G, LOW);
    delay(500);
    digitalWrite(A, HIGH);
    digitalWrite(B, LOW);
    digitalWrite(C, LOW);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, LOW);
    digitalWrite(G, LOW);
    delay(500);
    digitalWrite(A, LOW);
    digitalWrite(B, HIGH);
    digitalWrite(C, LOW);
    digitalWrite(D, LOW);
    digitalWrite(E, HIGH);
    digitalWrite(F, LOW);
    digitalWrite(G, LOW);
    delay(500);
    digitalWrite(A, LOW);
    digitalWrite(B, HIGH);
    digitalWrite(C, LOW);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(F, LOW);
    digitalWrite(G, LOW);
    delay(500);
    digitalWrite(A, LOW);
    digitalWrite(B, LOW);
    digitalWrite(C, LOW);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
    delay(500);
    digitalWrite(A, LOW);
    digitalWrite(B, LOW);
    digitalWrite(C, LOW);
    digitalWrite(D, LOW);
    digitalWrite(E, LOW);
    digitalWrite(F, LOW);
    digitalWrite(G, LOW);
    delay(500);
    digitalWrite(A, LOW);
    digitalWrite(B, LOW);
    digitalWrite(C, LOW);
    digitalWrite(D, LOW);
    digitalWrite(E, HIGH);
    digitalWrite(F, LOW);
    digitalWrite(G, LOW);
    delay(500);
    digitalWrite(A, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(C, HIGH);
    digitalWrite(D, HIGH);
    digitalWrite(E, HIGH);
    digitalWrite(F, HIGH);
    digitalWrite(G, HIGH);
    delay(500);
}
delay(500);
}