Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

#include <WiFi.

h>
#include <BlynkSimpleEsp32.h>

// WiFi credentials
char ssid[] = "YourWiFiSSID";
char pass[] = "YourWiFiPassword";

// Blynk authentication token


char auth[] = "YourAuthToken";

// Pins for motor control


#define MOTOR_A_PWM 2 // GPIO pin for PWM speed control of Motor A
#define MOTOR_A_FWD 4 // GPIO pin for forward direction of Motor A
#define MOTOR_A_REV 5 // GPIO pin for reverse direction of Motor A
#define MOTOR_B_PWM 14 // GPIO pin for PWM speed control of Motor B
#define MOTOR_B_FWD 15 // GPIO pin for forward direction of Motor B
#define MOTOR_B_REV 16 // GPIO pin for reverse direction of Motor B

// Pins for limit switches


#define LIMIT_SWITCH_A 18 // GPIO pin for limit switch A
#define LIMIT_SWITCH_B 19 // GPIO pin for limit switch B

// Blynk Virtual Pins


#define VIRTUAL_SPEED_PIN V0 // Virtual pin for speed control
#define VIRTUAL_FORWARD_PIN V1 // Virtual pin for forward direction
#define VIRTUAL_REVERSE_PIN V2 // Virtual pin for reverse direction
// Motor speed (0-255)
int motorSpeed = 0;
bool forwardRequested = false;
bool reverseRequested = false;

BLYNK_READ(VIRTUAL_SPEED_PIN) {
motorSpeed = param.asInt(); // Update motor speed when virtual pin V0 is read
}

BLYNK_READ(VIRTUAL_FORWARD_PIN) {
forwardRequested = param.asInt(); // Update forwardRequested when virtual pin V1 is read
}

BLYNK_READ(VIRTUAL_REVERSE_PIN) {
reverseRequested = param.asInt(); // Update reverseRequested when virtual pin V2 is read
}

void setup() {
// Setup Blynk
Blynk.begin(auth, ssid, pass);

// Setup GPIO pins for motor control


pinMode(MOTOR_A_PWM, OUTPUT);
pinMode(MOTOR_A_FWD, OUTPUT);
pinMode(MOTOR_A_REV, OUTPUT);
pinMode(MOTOR_B_PWM, OUTPUT);
pinMode(MOTOR_B_FWD, OUTPUT);
pinMode(MOTOR_B_REV, OUTPUT);

// Setup GPIO pins for limit switches


pinMode(LIMIT_SWITCH_A, INPUT_PULLUP);
pinMode(LIMIT_SWITCH_B, INPUT_PULLUP);
}

void loop() {
Blynk.run();

// Example logic for gate movement based on forward/reverse requests and limit switch status
if (forwardRequested && digitalRead(LIMIT_SWITCH_A)) {
// Move gate in forward direction
analogWrite(MOTOR_A_PWM, motorSpeed);
digitalWrite(MOTOR_A_FWD, HIGH);
digitalWrite(MOTOR_A_REV, LOW);
analogWrite(MOTOR_B_PWM, motorSpeed);
digitalWrite(MOTOR_B_FWD, HIGH);
digitalWrite(MOTOR_B_REV, LOW);
} else if (reverseRequested && digitalRead(LIMIT_SWITCH_B)) {
// Move gate in reverse direction
analogWrite(MOTOR_A_PWM, motorSpeed);
digitalWrite(MOTOR_A_FWD, LOW);
digitalWrite(MOTOR_A_REV, HIGH);
analogWrite(MOTOR_B_PWM, motorSpeed);
digitalWrite(MOTOR_B_FWD, LOW);
digitalWrite(MOTOR_B_REV, HIGH);
} else {
// Stop gate movement
analogWrite(MOTOR_A_PWM, 0);
analogWrite(MOTOR_B_PWM, 0);
}
}

You might also like