#define BLYNK_TEMPLATE_ID "TMPL3TvpVvld3"
#define BLYNK_TEMPLATE_NAME "rope climbing bot"
#define BLYNK_AUTH_TOKEN "sC7rC75yBZ6egnEAfMSCOo5_3-Wjb0K4"

// ================= LIBRARIES =================
#include <WiFiS3.h>
#include <BlynkSimpleWifi.h>

// ================= WIFI DETAILS =================
char ssid[] = "KSIT-2.4Gz";
char pass[] = "ksit@2025";

// =================================================
// L298N CONNECTIONS
// =================================================

// MOTOR A
#define IN1 5
#define IN2 6
#define ENA 9

// MOTOR B
#define IN3 7
#define IN4 8
#define ENB 10

// =================================================
// SPEED VARIABLE
// =================================================
int motorSpeed = 230;

// =================================================
// SETUP
// =================================================
void setup()
{
  Serial.begin(115200);

  // MOTOR A
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ENA, OUTPUT);

  // MOTOR B
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENB, OUTPUT);

  stopMotors();

  // CONNECT TO BLYNK
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

  Serial.println("2 BO Motor Robot Connected");
}

// =================================================
// LOOP
// =================================================
void loop()
{
  Blynk.run();
}

// =================================================
// V0 -> BOTH MOTORS FORWARD
// =================================================
BLYNK_WRITE(V0)
{
  int value = param.asInt();

  if(value == 1)
  {
    forward();
  }
}

// =================================================
// V1 -> BOTH MOTORS BACKWARD
// =================================================
BLYNK_WRITE(V1)
{
  int value = param.asInt();

  if(value == 1)
  {
    backward();
  }
}

// =================================================
// V2 -> STOP
// =================================================
BLYNK_WRITE(V2)
{
  int value = param.asInt();

  if(value == 1)
  {
    stopMotors();
  }
}

// =================================================
// V3 -> FAST SPEED
// =================================================
BLYNK_WRITE(V3)
{
  int value = param.asInt();

  if(value == 1)
  {
    motorSpeed = 220;

    analogWrite(ENA, motorSpeed);
    analogWrite(ENB, motorSpeed);

    Serial.println("FAST SPEED");
  }
}

// =================================================
// V4 -> SLOW SPEED
// =================================================
BLYNK_WRITE(V4)
{
  int value = param.asInt();

  if(value == 1)
  {
    motorSpeed = 80;

    analogWrite(ENA, motorSpeed);
    analogWrite(ENB, motorSpeed);

    Serial.println("SLOW SPEED");
  }
}

// =================================================
// BOTH MOTORS FORWARD
// =================================================
void forward()
{
  analogWrite(ENA, motorSpeed);
  analogWrite(ENB, motorSpeed);

  // MOTOR A
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);

  // MOTOR B
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);

  Serial.println("FORWARD");
}

// =================================================
// BOTH MOTORS BACKWARD
// =================================================
void backward()
{
  analogWrite(ENA, motorSpeed);
  analogWrite(ENB, motorSpeed);

  // MOTOR A
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);

  // MOTOR B
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);

  Serial.println("BACKWARD");
}

// =================================================
// STOP BOTH MOTORS
// =================================================
void stopMotors()
{
  // MOTOR A
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);

  // MOTOR B
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);

  analogWrite(ENA, 0);
  analogWrite(ENB, 0);

  Serial.println("STOP");
}