hello, so im making a robosoccer car controlled by esp32 and ps4 controller
/*
front
m1 | |m2
| |
m4 | |m3
*/
#include <PS4Controller.h>
#include <L298N.h>
#include <ESP32Servo.h>
int servoPin = 32;
Servo myservo;
const unsigned int ENA = 19;
const unsigned int IN1 = 18;
const unsigned int IN2 = 5;
const unsigned int IN3 = 23;
const unsigned int IN4 = 22;
const unsigned int ENB = 15;
const unsigned int ENC = 12;
const unsigned int IN5 = 14;
const unsigned int IN6 = 27;
const unsigned int ENE = 33;
const unsigned int IN7 = 26;
const unsigned int IN8 = 25;
L298N motor1(ENA, IN1, IN2);
L298N motor2(ENB, IN3, IN4);
L298N motor3(ENC, IN5, IN6);
L298N motor4(ENE, IN7, IN8);
void setup() {
Serial.begin(115200);
myservo.attach(servoPin);
PS4.begin();
}
void loop() {
int xAxis = PS4.RStickX();
int yAxis = PS4.LStickY();
int Rotate = PS4.LStickX();
if (Rotate < -100) {
motor1.setSpeed(190);
motor2.setSpeed(190);
motor3.setSpeed(190);
motor4.setSpeed(190);
motor1.backward();
motor2.backward();
motor3.backward();
motor4.backward();
Serial.println("ROTATE CCW 50%");
}
else if (Rotate > 100) {
motor1.setSpeed(190);
motor2.setSpeed(190);
motor3.setSpeed(190);
motor4.setSpeed(190);
motor1.forward();
motor2.forward();
motor3.forward();
motor4.forward();
Serial.println("ROTATE CW 50%");
}
else if (yAxis > 100 && PS4.R2()) {
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
motor1.forward();
motor2.backward();
motor3.backward();
motor4.forward();
Serial.println("Forward at 100%");
}
else if (yAxis > 100) {
motor1.setSpeed(190);
motor2.setSpeed(190);
motor3.setSpeed(190);
motor4.setSpeed(190);
motor1.forward();
motor2.backward();
motor3.backward();
motor4.forward();
Serial.println("Forward at 75%");
}
else if (yAxis < -100 && PS4.R2()) {
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
motor1.backward();
motor2.forward();
motor3.forward();
motor4.backward();
Serial.println("Backward at 100%");
}
else if (yAxis < -100) {
motor1.setSpeed(190);
motor2.setSpeed(190);
motor3.setSpeed(190);
motor4.setSpeed(190);
motor1.backward();
motor2.forward();
motor3.forward();
motor4.backward();
Serial.println("Backward at 75%");
}
else if (xAxis > 100 && PS4.R2()) {
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
motor1.forward();
motor2.forward();
motor3.backward();
motor4.backward();
Serial.println("Right sideway at 100%");
}
else if (xAxis > 100) {
motor1.setSpeed(190);
motor2.setSpeed(190);
motor3.setSpeed(190);
motor4.setSpeed(190);
motor1.forward();
motor2.forward();
motor3.backward();
motor4.backward();
Serial.println("Right sideway at 75%");
}
else if (xAxis < -100 && PS4.R2()) {
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
motor1.backward();
motor2.backward();
motor3.forward();
motor4.forward();
Serial.println("Left sideway at 100%");
}
else if (xAxis < -100) {
motor1.setSpeed(190);
motor2.setSpeed(190);
motor3.setSpeed(190);
motor4.setSpeed(190);
motor1.backward();
motor2.backward();
motor3.forward();
motor4.forward();
Serial.println("Left sideway at 75%");
}
else if (PS4.R1()){
myservo.write(60);
Serial.println("ball trapped");
delay(500);
}
else if (PS4.L1()) {
myservo.write(90);
Serial.println("ball Released");
delay(500);
}
else {
motor1.setSpeed(0);
motor2.setSpeed(0);
motor3.setSpeed(0);
motor4.setSpeed(0);
motor1.stop();
motor2.stop();
motor3.stop();
motor4.stop();
Serial.println("Stop");
}
delay(100);
}
so when I press R1 to trap ball it moves the servo and resets the esp32