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

// Uncomment according to your hardware type
#define HARDWARE_TYPE  MD_MAX72XX::PAROLA_HW
//#define HARDWARE_TYPE MD_MAX72XX::GENERIC_HW

// Defining size, and output pins
#define MAX_DEVICES 4
#define CS_PIN 5
#define LDR_PIN 36

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

const float GAMMA = 0.7;
const float RL10 = 50;

void setup() {
  pinMode(LDR_PIN, INPUT);
  // Intialize the object:
  Display.begin();
  // Set the intensity (brightness) of the display (0-15):
  Display.setIntensity(0);
  // Clear the display:
  Display.displayClear();
  Display.displayText("Tes Sensor LDR", PA_CENTER, 70, 0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);


  Serial.begin(9600);
}

void loop() {

  if (Display.displayAnimate()) {
    // mendeklarasikan nilai analog ke dalam variabel light
    int light = analogRead(LDR_PIN);
    //volt / tegangan
    float voltage = light / 4095. * 5;
    // hambatan
    float resistance = 2000 * voltage / (1 - voltage / 5);
    // lux merupakan satuan turunan SI dari pencahayaan dan daya pancar cahaya
    float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));

    Serial.print("analog: ");
    Serial.print(light);
    Serial.print(" lux: ");
    Serial.println(lux);

    if (lux <= 10) {
      Serial.println(" => Malam");
      Display.displayText("Selamat malam !", PA_CENTER, 70, 0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
    } else if (lux <= 1000) {
      Serial.println(" => Pagi/Mendung");
      Display.displayText("Selamat Pagi !", PA_CENTER, 70, 0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
    } else if (lux <= 10000) {
      Serial.println(" => Siang");
      Display.displayText("Selamat Siang !", PA_CENTER, 70, 0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
    } else {
      Serial.println(" => Siang Sangat Terang");
      Display.displayText("Hari yang sangat cerah !", PA_CENTER, 70, 0, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
    }

    delay(500);

    Display.displayReset();
  }
}