/******************************************************************************************/
/*
/* File Name : sketch.ino
/*
/* Created on : Dec 8 , 2024
/*
/* Author : Mohamed Reda Ibrahim
/*
/* Version : V01
/*
/* Description : This RC Car is an ESP32-based project that enables an RC car to
/* clean vertical surfaces. The car is equipped with
/* various motor drivers,
/*
/* Layer : APP
/***************************************************************************************/
// ========================Include section==================================
#include<ESP32Servo.h>
#include <BluetoothSerial.h>
//BluetoothSerial SerialBT;
// ========================Bluetooth Buffer=================================
// Handle received and sent messages
char message = 'n';
char incomingChar;
// ========================Bluetooth commands===============================
// - 'F' : Set the rc to forward mode and the stepper to push wire
// - 'B' : Set the rc to backward mode and the stepper to drag the wire
// - 'R' : Set the rc to right mode and the stepper to push wire
// - 'L' : Set the rc to left mode and the stepper to push wire
// - 'S' : Set the rc motor to stop mode
// - 'U' : Set the servo motor to the clean mode at angle 0
// - 'D' : Set the servo motor to standby mode at angle 100
// ========================Stepper driver pins==============================
#define STEPPER_DIR_PIN 36 // Direction pin for A4988
#define STEPPER_STEP_PIN 37 // Step pin for A4988
#define STEPPER_UP LOW
#define STEPPER_DOWN HIGH
int stepCount=0;
int delValue=100;
// ========================L298 motor driver pins===========================
#define IN1 14
#define IN2 15
#define IN3 16
#define IN4 17
// ========================ESC driver pins==================================
Servo esc;
#define ESC 18
// ========================Servo driver pins================================
Servo myservo;
#define Servo 19
/**
* @brief Initializes the ESP32 board and sets up the necessary configurations.
*
* This function is called once at the beginning of the program.
*/
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32-S2!");
pinMode(STEPPER_DIR_PIN, OUTPUT);
pinMode(STEPPER_STEP_PIN, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
myservo.attach(Servo);
esc.attach(ESC,1000,2000);
esc.write(100);
}
/**
* @brief The main loop function that runs continuously.
*
* This function is called repeatedly after the setup() function.
* It executes different code based on the selected mode.
*/
void loop() {
// Read received messages
if (Serial.available()){
char incomingChar = Serial.read();
if (incomingChar != '\n'){
message = incomingChar;
}
Serial.write(incomingChar);
}
if (message =='F'){
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
digitalWrite(STEPPER_DIR_PIN, STEPPER_UP);
digitalWrite(STEPPER_STEP_PIN, HIGH);
digitalWrite(STEPPER_STEP_PIN, LOW);
delayMicroseconds(delValue);
}
else if (message =='B'){
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
digitalWrite(STEPPER_DIR_PIN, STEPPER_DOWN);
digitalWrite(STEPPER_STEP_PIN, HIGH);
digitalWrite(STEPPER_STEP_PIN, LOW);
delayMicroseconds(delValue);
}
else if (message =='R'){
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
digitalWrite(STEPPER_DIR_PIN, STEPPER_UP);
digitalWrite(STEPPER_STEP_PIN, HIGH);
digitalWrite(STEPPER_STEP_PIN, LOW);
delayMicroseconds(delValue);
}
else if (message =='L'){
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
digitalWrite(STEPPER_DIR_PIN, STEPPER_UP);
digitalWrite(STEPPER_STEP_PIN, HIGH);
digitalWrite(STEPPER_STEP_PIN, LOW);
delayMicroseconds(delValue);
}
else if (message =='S'){
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
else if (message =='D'){
myservo.write(100);
}
else if (message =='U'){
myservo.write(0);
}
delay(20);
}