good day to all! i need help on how to properly code my project.
schematic diagram:
- i just finished working on how to control the movement of my car using a mobile app from play store, and it works perfectly fine. i used two 3.7v li-ion battery via arduino jack(linear actuator not included yet), but it is kind of slow when it comes to a not flat terrain. so i decided to upgrade my power supply to 12 volts via L293D motor shield ext power (still actuator not included yet), as expected the speed improved but the bluetooth module(hc-05) automatically disconnects after 5 secs controlling the car. can anyone help me in fixing this?
- i plan to connect a linear actuator (with a 12v power supply) if the above problem is fixed, the linear actuator alone functions really well with a 12v supply. i struggle on how to combine the code of the linear actuator along with the four TT motors. can you help me with this? i want my car to function as forward, reverse, left, and right. then when the car stop for 5 secs the actuator will extend for 10 secs and retract after 10 secs. as it retracts that iss the time i can control its movement again.
NOTE: the 2 items was tested separately
these are the code:
LINEAR ACTUATOR ONLY
#include <AFMotor.h>
// Define motor parameters
AF_DCMotor motor(4); // Motor connected to M1 on the L293D shield
void setup() {
Serial.begin(9600); // initialize serial communication at 9600 bits per second
Serial.println("Linear Actuator Control");
motor.setSpeed(255); // Set the motor speed. You can adjust this value.
}
void loop() {
// Extend the linear actuator
Serial.println("Extending...");
motor.setSpeed(255);
motor.run(FORWARD); // Set the motor to move in the FORWARD direction
delay(5000); // Wait for 5 seconds (adjust as needed)
// Retract the linear actuator
Serial.println("Retracting...");
motor.run(BACKWARD); // Set the motor to move in the BACKWARD direction
delay(5000); // Wait for 5 seconds (adjust as needed)
// Stop the motor
motor.run(RELEASE); // Release the motor, stopping it
delay(5000); // Wait for 5 seconds (adjust as needed)
}
CODE for the car's movement:
#include <AFMotor.h>
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
char command;
void setup()
{
Serial.begin(9600); //Set the baud rate to your Bluetooth module.
}
void loop(){
if(Serial.available() > 0){
command = Serial.read();
Stop(); //initialize with motors stoped
//Change pin mode only if new command is different from previous.
//Serial.println(command);
switch(command){
case 'F':
forward();
break;
case 'B':
back();
break;
case 'L':
left();
break;
case 'R':
right();
break;
}
}
}
void forward()
{
motor2.setSpeed(255); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
motor3.setSpeed(255);//Define maximum velocity
motor3.run(FORWARD); //rotate the motor clockwise
}
void back()
{
motor2.setSpeed(255); //Define maximum velocity
motor2.run(BACKWARD); //rotate the motor anti-clockwise
motor3.setSpeed(255); //Define maximum velocity
motor3.run(BACKWARD); //rotate the motor anti-clockwise
}
void left()
{
motor2.setSpeed(255); //Define maximum velocity
motor2.run(BACKWARD); //rotate the motor anti-clockwise
motor3.setSpeed(255); //Define maximum velocity
motor3.run(FORWARD); //rotate the motor clockwise
}
void right()
{
motor2.setSpeed(255); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
motor3.setSpeed(255); //Define maximum velocity
motor3.run(BACKWARD); //rotate the motor anti-clockwise
}
void Stop()
{
motor2.setSpeed(0); //Define minimum velocity
motor2.run(RELEASE); //rotate the motor clockwise
motor3.setSpeed(0); //Define minimum velocity
motor3.run(RELEASE); //stop the motor when release the button
}
(BTW M1 on the motor shield is not functioning properly when i connect a motor)