#include "RDBdimmer.h"//

//#define USE_SERIAL  SerialUSB //Serial for boards whith USB serial port
#define USE_SERIAL  Serial
#define outputPin  3
#define zerocross  2 // for boards with CHANGEBLE input pins
#define potPin A0 // define the input pin for potentiometer

/// fix potentiometer issue
const int potMin = 0;// watch video (link above) for details
const int potMax = 198;// watch video (link above) for details
int potValue;// watch video (link above) for details

//dimmerLamp dimmer(outputPin, zerocross); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards
dimmerLamp dimmer(outputPin); //initialase port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero

int outVal = 0;


const int segment[] ={12,10,8,7,6,11,9,1};
const int digit[] = {5,13,4,};
unsigned long interval =0;
bool tmbl = false;
int laststate;
int led = 3;
int up = A1;
int down = A2;
int val = 0;
int intensityIndex = 0; // Dimulai dari tingkat intensitas 0%

// Tentukan tingkat intensitas (0%, 10%, 20%, 25%, 40%, 50%, 60%, 75%, 90%)
int intensityLevels[] = {0 ,22, 44, 55, 88, 110, 132, 165, 198};

void setup()
 {
  // put your setup code here, to run once:
  dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE) 
  for(int i = 0;i<=7;i++){    
    pinMode(segment[i], OUTPUT);
  }
  for(int i = 0;i<=3;i++){    
    pinMode(digit[i], OUTPUT);
  }
  {
  pinMode(up, INPUT_PULLUP);
  pinMode(down, INPUT_PULLUP);
  pinMode(led, OUTPUT);
  Serial.begin(9600);
}
}

void tampil(int nilai) 
{
    if (nilai == 0){
      digitalWrite(segment[0], 0);
      digitalWrite(segment[1], 0);
      digitalWrite(segment[2], 0);
      digitalWrite(segment[3], 0);
      digitalWrite(segment[4], 0);
      digitalWrite(segment[5], 0);
      digitalWrite(segment[6], 1);
      digitalWrite(segment[7], 1);
  }
  if (nilai == 1){
      digitalWrite(segment[0], 1);
      digitalWrite(segment[1], 0);
      digitalWrite(segment[2], 0);
      digitalWrite(segment[3], 1);
      digitalWrite(segment[4], 1);
      digitalWrite(segment[5], 1);
      digitalWrite(segment[6], 1);
      
  }
  if (nilai == 2){
      digitalWrite(segment[0], 0);
      digitalWrite(segment[1], 0);
      digitalWrite(segment[2], 1);
      digitalWrite(segment[3], 0);
      digitalWrite(segment[4], 0);
      digitalWrite(segment[5], 1);
      digitalWrite(segment[6], 0);
  }
  if (nilai == 3){
      digitalWrite(segment[0], 0);
      digitalWrite(segment[1], 0);
      digitalWrite(segment[2], 0);
      digitalWrite(segment[3], 0);
      digitalWrite(segment[4], 1);
      digitalWrite(segment[5], 1);
      digitalWrite(segment[6], 0);
  }
  if (nilai == 4){
      digitalWrite(segment[0], 1);
      digitalWrite(segment[1], 0);
      digitalWrite(segment[2], 0);
      digitalWrite(segment[3], 1);
      digitalWrite(segment[4], 1);
      digitalWrite(segment[5], 0);
      digitalWrite(segment[6], 0);
  }
  if (nilai == 5){
      digitalWrite(segment[0], 0);
      digitalWrite(segment[1], 1);
      digitalWrite(segment[2], 0);
      digitalWrite(segment[3], 0);
      digitalWrite(segment[4], 1);
      digitalWrite(segment[5], 0);
      digitalWrite(segment[6], 0);
  }
  if (nilai == 6){
      digitalWrite(segment[0], 0);
      digitalWrite(segment[1], 1);
      digitalWrite(segment[2], 0);
      digitalWrite(segment[3], 0);
      digitalWrite(segment[4], 0);
      digitalWrite(segment[5], 0);
      digitalWrite(segment[6], 0);
  }
  if (nilai == 7){
      digitalWrite(segment[0], 0);
      digitalWrite(segment[1], 0);
      digitalWrite(segment[2], 0);
      digitalWrite(segment[3], 1);
      digitalWrite(segment[4], 1);
      digitalWrite(segment[5], 1);
      digitalWrite(segment[6], 1);
  }
  if (nilai == 8){
      digitalWrite(segment[0], 0);
      digitalWrite(segment[1], 0);
      digitalWrite(segment[2], 0);
      digitalWrite(segment[3], 0);
      digitalWrite(segment[4], 0);
      digitalWrite(segment[5], 0);
      digitalWrite(segment[6], 0);
  }
  if (nilai == 9){
      digitalWrite(segment[0], 0);
      digitalWrite(segment[1], 0);
      digitalWrite(segment[2], 0);
      digitalWrite(segment[3], 0);
      digitalWrite(segment[4], 1);
      digitalWrite(segment[5], 0);
      digitalWrite(segment[6], 0);
  }
  if (nilai == 'P'){
      digitalWrite(segment[0], 0);
      digitalWrite(segment[1], 0);
      digitalWrite(segment[2], 1);
      digitalWrite(segment[3], 1);
      digitalWrite(segment[4], 0);
      digitalWrite(segment[5], 0);
      digitalWrite(segment[6], 0);
  }
 
  digitalWrite(segment[7], 1);
}
void display(int a)
{
    tampil(a / 100);
    digitalWrite(digit[0], 1);
    digitalWrite(digit[1], 0);
    digitalWrite(digit[2], 0);
    delay(10);
    digitalWrite(digit[0], 0);
    tampil(a % 100 /10);
    digitalWrite(digit[0], 0);
    digitalWrite(digit[1], 1);
    digitalWrite(digit[2], 0);
    digitalWrite(segment[7], 0);
    delay(10);
    digitalWrite(digit[1], 0);
    if (a % 10 == 0) {
        tampil('P');
    } else {
        tampil(a % 10);
    }

    digitalWrite(digit[0], 0);
    digitalWrite(digit[1], 0);
    digitalWrite(digit[2], 1);
    delay(10);
    digitalWrite(digit[2], 0);
    
}

void loop(){
  int percent = map(val, 0, 220, 0, 100); // Konversi nilai menjadi persentase
  Serial.print(" ");
  Serial.print(percent);
  Serial.println("%");
  val = constrain(val, 0, 220);
  analogWrite(led, val);
  delay(20);

  if (analogRead(up) == HIGH && val < intensityLevels[sizeof(intensityLevels) / sizeof(int) - 1]) {
    // Tombol "up" hanya berfungsi jika intensitas belum mencapai 90%
    intensityIndex = (intensityIndex + 1) % (sizeof(intensityLevels) / sizeof(int));
    val = intensityLevels[intensityIndex];
    delay(500); // Delay untuk menghindari pembacaan yang terlalu cepat dari tombol
  } else if (analogRead(down) == HIGH && val > intensityLevels[0]) {
    // Tombol "down" hanya berfungsi jika intensitas belum mencapai 0%
    intensityIndex = (intensityIndex - 1 + sizeof(intensityLevels) / sizeof(int)) % (sizeof(intensityLevels) / sizeof(int));
    val = intensityLevels[intensityIndex];
    delay(500); // Delay untuk menghindari pembacaan yang terlalu cepat dari tombol
  }
  // put your main code here, to run repeatedly:
  unsigned long times = millis();
  if(times - interval >= 0){ 
    interval = times;
    if(tmbl == false){
        tmbl = true;
    } else{
        tmbl = false;
    }      
  }
    if(tmbl == true){
  int percent = map(val, 0, 220, 0, 100); // Konversi nilai menjadi persentase
  display(percent*10);
  } 
  correctValue(); 
}


void correctValue()
{
   if(val <potMin)
  {
    val =potMin;
  }
  if(val >potMax)
  {
    val =potMax;
  }   
}