/**********************************************************************************************************************
  ███╗   ███╗ █████╗ ██████╗ ███████╗    ██████╗ ██╗   ██╗
  ████╗ ████║██╔══██╗██╔══██╗██╔════╝    ██╔══██╗╚██╗ ██╔╝
  ██╔████╔██║███████║██║  ██║█████╗      ██████╔╝ ╚████╔╝ 
  ██║╚██╔╝██║██╔══██║██║  ██║██╔══╝      ██╔══██╗  ╚██╔╝  
  ██║ ╚═╝ ██║██║  ██║██████╔╝███████╗    ██████╔╝   ██║   
  ╚═╝     ╚═╝╚═╝  ╚═╝╚═════╝ ╚══════╝    ╚═════╝    ╚═╝   

  ██████╗  ██████╗ ██████╗  █████╗ ███╗   ███╗███████╗████████╗    ██╗███╗   ██╗██████╗ ██████╗  ██████╗ ███╗   ███╗
  ██╔══██╗██╔═══██╗██╔══██╗██╔══██╗████╗ ████║██╔════╝╚══██╔══╝    ██║████╗  ██║██╔══██╗██╔══██╗██╔═══██╗████╗ ████║
  ██████╔╝██║   ██║██████╔╝███████║██╔████╔██║█████╗     ██║       ██║██╔██╗ ██║██████╔╝██████╔╝██║   ██║██╔████╔██║
  ██╔═══╝ ██║   ██║██╔══██╗██╔══██║██║╚██╔╝██║██╔══╝     ██║       ██║██║╚██╗██║██╔═══╝ ██╔══██╗██║   ██║██║╚██╔╝██║
  ██║     ╚██████╔╝██║  ██║██║  ██║██║ ╚═╝ ██║███████╗   ██║       ██║██║ ╚████║██║     ██║  ██║╚██████╔╝██║ ╚═╝ ██║
  ╚═╝      ╚═════╝ ╚═╝  ╚═╝╚═╝  ╚═╝╚═╝     ╚═╝╚══════╝   ╚═╝       ╚═╝╚═╝  ╚═══╝╚═╝     ╚═╝  ╚═╝ ╚═════╝ ╚═╝     ╚═╝
***********************************************************************************************************************/

#define BLYNK_TEMPLATE_ID "TMPL6_PrcUCZ4"
#define BLYNK_TEMPLATE_NAME "CarCleaner"
#define BLYNK_AUTH_TOKEN "U-D1E-CKeoQ41-i0lh6y4XZCx2W532cq"
#define BLYNK_FIRMWARE_VERSION    "0.1.0"
#define BLYNK_PRINT Serial // Defines the object that is used for printing
//#define BLYNK_DEBUG        // Optional, this enables more detailed prints

#include <Arduino.h>            //นำเข้าไลบรารี่ Arduino
#include <WiFi.h>               //นำเข้าไลบรารี่ WiFi
#include <WiFiClient.h>       
#include <BlynkSimpleEsp32.h>   //นำเข้าไลบรารี่ Blynk

char auth[] = BLYNK_AUTH_TOKEN;
/* เก็บข้อมูลตัวแปร ไวไฟ และรหัส */
String send_ssid;
String send_pass;

char ssid[] = "Wokwi-GUEST";
char pass[] = "";

#define L_L_PWM   32
#define L_R_PWM   33
#define R_L_PWM   25
#define R_R_PWM   26
#define C_L_PWM   27
#define C_R_PWM   14

#define V_Foward    V0
#define V_Backward  V1
#define V_Left      V2
#define V_Right     V3
#define V_Cleaner   V4
#define V_Speed     V5

int Speedvalue = 0;

void setup() {
  pinMode(5,OUTPUT);
  pinMode(L_L_PWM,OUTPUT);
  pinMode(L_R_PWM,OUTPUT);
  pinMode(R_L_PWM,OUTPUT);
  pinMode(R_R_PWM,OUTPUT);
  pinMode(C_L_PWM,OUTPUT);
  pinMode(C_R_PWM,OUTPUT);

  Serial.begin(115200);
  WiFi.begin(ssid, pass);
  Blynk.begin(auth, ssid, pass);

}

void loop() {

  if (WiFi.status() == WL_CONNECTED)
  {
    Blynk.run();
    digitalWrite(5,HIGH);
  }
}

BLYNK_WRITE(V_Foward){
  int value = param.asInt();
  if(value == 1){
    Serial.println((String)"Foward Speed:"+Speedvalue);
    analogWrite(L_R_PWM,LOW);
    analogWrite(R_L_PWM,LOW);
    analogWrite(L_L_PWM, Speedvalue);
    analogWrite(R_R_PWM, Speedvalue);
  }
  else{
    analogWrite(L_L_PWM,LOW);
    analogWrite(R_R_PWM,LOW);
  }
}
BLYNK_WRITE(V_Backward){
  int value = param.asInt();
  if(value == 1){
    Serial.println((String)"BackWard Speed: "+Speedvalue);
    analogWrite(L_L_PWM,LOW);
    analogWrite(R_R_PWM,LOW);
    analogWrite(L_R_PWM, Speedvalue);
    analogWrite(R_L_PWM, Speedvalue);
  }
  else{
    analogWrite(L_R_PWM,LOW);
    analogWrite(R_L_PWM,LOW);
    Serial.println("Backward Stop");
  }
}
BLYNK_WRITE(V_Left){
  int value = param.asInt();
  if(value == 1){
    Serial.println((String)"Left Speed: "+Speedvalue);
    analogWrite(L_L_PWM,LOW);
    analogWrite(R_L_PWM,LOW);
    analogWrite(L_R_PWM, Speedvalue);
    analogWrite(R_R_PWM, Speedvalue);
  }
  else{
    analogWrite(L_R_PWM,LOW);
    analogWrite(R_R_PWM,LOW);
  }
}
BLYNK_WRITE(V_Right){
  int value = param.asInt();
  if(value == 1){
    Serial.println((String)"Right Speed: "+Speedvalue);
    analogWrite(L_R_PWM,LOW);
    analogWrite(R_R_PWM,LOW);
    analogWrite(L_L_PWM, Speedvalue);
    analogWrite(R_L_PWM, Speedvalue);
  }
  else{
    analogWrite(L_L_PWM,LOW);
    analogWrite(R_L_PWM,LOW);
  }
}
BLYNK_WRITE(V_Cleaner){
  int value = param.asInt();
  if(value == 1){
    Serial.println("Cleaner ON");
    analogWrite(C_L_PWM,LOW);
    analogWrite(C_R_PWM,255);
  }
  else{
    Serial.println("Cleaner OFF");
    analogWrite(C_L_PWM,LOW);
    analogWrite(C_R_PWM,LOW);
  }
}
BLYNK_WRITE(V_Speed){
  Speedvalue = param.asInt();

}