I am using four servo motors for our robotic arm and one L298N motor driver for two motors for the base of the arm.
When I individually tested the robotic arm and controlled four servo motors it worked perfectly fine, and when I tested the linear motion with the motor driver individually it also worked fine.
When I combined them only the servos were working but the L298N doesn't. When I comment out the servos.attach()
line, then the motor driver works again.
The pins are different, that's why I couldn't figure out why am I getting this issue.
#include <WiFi.h>
#include <WebServer.h>
#include <Robojax_L298N_DC_motor.h>
#include<Servo.h>
WebServer server(80);
Servo myservo, shoulder, elbow, wrist;
int grippos = 0, shoulderpos = 0, elbowpos = 0, wristpos = 0;
// motor 1 settings
#define CHA 0
#define ENA 19 // this pin must be PWM enabled pin if Arduino board is used
#define IN1 18
#define IN2 5
// motor 2 settings
#define IN3 15
#define IN4 2
#define ENB 4 // this pin must be PWM enabled pin if Arduino board is used
#define CHB 1
const int CCW = 2; // do not change
const int CW = 1; // do not change
#define motor1 1 // do not change
#define motor2 2 // do not change
String prev="";
Robojax_L298N_DC_motor robot(IN1, IN2, ENA, CHA, IN3, IN4, ENB, CHB);
void gripopen() {
Serial.println("open");
grippos += 30;
if (grippos > 180) {
grippos = 180;
}
myservo.write(grippos);
}
void gripclose() {
grippos -= 30;
Serial.println("close");
if (grippos < 0) {
grippos = 0;
}
myservo.write(grippos);
}
void wristleft() {
wristpos -= 30;
Serial.println("wristleft");
if (wristpos < 0) {
wristpos=0;
}
wrist.write(wristpos);
}
void wristright() {
Serial.println("wristright");
wristpos += 30;
if (wristpos>180) {
wristpos = 180;
}
wrist.write(wristpos);
}
void elbowup() {
Serial.println("elbow up");
elbowpos += 15;
if (elbowpos > 180) {
elbowpos = 180;
}
elbow.write(elbowpos);
}
void elbowdown() {
Serial.println("elbow down");
elbowpos -= 15;
if (elbowpos < 0) {
elbowpos = 0;
}
elbow.write(elbowpos);
}
void shoulderup() {
Serial.println("shoulder up");
shoulderpos += 15;
if (shoulderpos > 180) {
shoulderpos = 180;
}
shoulder.write(shoulderpos);
}
void shoulderdown() {
Serial.println("shoulder down");
shoulderpos -= 15;
if (shoulderpos < 0) {
shoulderpos = 180;
}
shoulder.write(shoulderpos);
}
void forward() {
Serial.println("forward");
robot.rotate(motor1, 100, CW);
robot.rotate(motor2, 100, CCW);
Serial.println("end reached");
}
void backward() {
Serial.println("backward");
robot.rotate(motor1, 100, CCW);
robot.rotate(motor2, 100, CW);
Serial.println("end reached");
}
void left() {
Serial.println("left");
robot.rotate(motor1, 100, CW);
robot.rotate(motor2, 100, CW);
Serial.println("end reached");
}
void right() {
Serial.println("right");
robot.rotate(motor1, 100, CCW);
robot.rotate(motor2, 100, CCW);
Serial.println("end reached");
}
void stopbot() {
Serial.println("stopbot");
robot.brake(1);
robot.brake(2);
prev = "";
Serial.println("end reached");
}
void reset() {
myservo.write(0);
shoulder.write(0);
elbow.write(0);
wrist.write(0);
}
void handleRoot() {
server.send(200, "text/plain", "Ready");
}
void handleGet() {
if (server.hasArg("data")) {
String data = server.arg("data");
if (data == "gripper_open") {
gripopen();
} else if (data == "reset") {
Serial.println("Reset");
} else if (data == "Stop") {
stopbot();
} else if (data == "Forward") {
// forward();
if (prev != data) {
forward();
prev = data;
}
} else if (data == "Backward") {
// backward();
if (prev != data) {
backward();
prev=data;
}
} else if (data == "Left") {
// left();
if (prev != data) {
left();
prev = data;
}
} else if (data == "Right") {
// right();
if (prev != data) {
right();
prev = data;
}
} else if (data == "gripper_close") {
gripclose();
} else if (data == "elbow_up") {
elbowup();
} else if (data == "elbow_down") {
elbowdown();
} else if (data == "shoulder_up") {
shoulderup();
} else if (data == "shoulder_down") {
shoulderdown();
} else if (data == "Wrist_left") {
wristleft();
} else if (data == "Wrist_right") {
wristright();
}
Serial.println("Data: " + data);
}
server.send(200, "text/plain", "Data Received");
}
void handlePost() {
if (server.hasArg("data")) {
String data = server.arg("data");
Serial.println("Data: " + data);
}
// server.send(200, "text/plain", "Data Received");
server.send(200, "text/plain", "Processing Data");
}
void handleUpload() {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.println("Receiving data:");
} else if (upload.status == UPLOAD_FILE_WRITE) {
Serial.write(upload.buf, upload.currentSize);
} else if (upload.status == UPLOAD_FILE_END) {
server.send(200, "text/plain", "Data: ");
}
}
void setup() {
Serial.begin(9600);
WiFi.softAP("ESP32");
myservo.attach(13);
shoulder.attach(12);
elbow.attach(14);
wrist.attach(27);
myservo.write(0);
shoulder.write(0);
elbow.write(0);
wrist.write(0);
// motordriver pins
robot.begin();
server.on("/", handleRoot);
server.on("/get", HTTP_GET, handleGet);
server.on("/pot", HTTP_POST,handlePost);
server.begin();
}
void loop() {
server.handleClient();
}
I am receiving the correct data from the app that I also cross-checked. Can anyone help me with this or guide me how can I solve this issue?