Hello, my name is Carlos, I am doing a project in high school and I have very little time left. I am using this code to control 4 brushless motors which work perfectly. I have the ESC and the pwm pin connected to the arduino. I am controlling them with a hc-05 sensor my project is a drone. I also have an mpu6050 but I don't know how to implement it. So far I am using this code which works perfectly with command 'A' and command 'S', but in command '1' only m1 is activated, I hope it can help solve that and be able to add command '2 ' that makes motor 1 and 4 rotate at 1500 and motor 2 and 3 at 2000, as well as command '3' whose functionality is to rotate the motors at a speed of 1700. I know that there are really capable people here and that is why I turn to you, I hope you can help me establish those commands and, if possible, integrate the mpu6050 to have more stability. Thank you very much for reading this post.
#include <SoftwareSerial.h>
const int m1 = 3;
const int m2 = 4;
const int m3 = 6;
const int m4 = 5;
int min_throttle = 1000;
int max_throttle = 2000;
unsigned long currentMillis, previousMillis;
SoftwareSerial BTSerial(10, 11); // RX, TX
void setSpeed(int motor, int speed) {
if (motor == m1 || motor == m2 || motor == m3 || motor == m4) {
analogWrite(motor, speed);
}
}
void setSpeedDifferentName(int motor, int speed) {
if (motor == m1 || motor == m2 || motor == m3 || motor == m4) {
analogWrite(motor, speed);
}
}
void setup() {
//Init Serial USB
Serial.begin(9600);
Serial.println(F("Initialize System"));
//Init ESC
pinMode(m1, OUTPUT);
pinMode(m2, OUTPUT);
pinMode(m3, OUTPUT);
pinMode(m4, OUTPUT);
initProcedure();
//Init Bluetooth
BTSerial.begin(9600);
}
void loop() {
if (BTSerial.available()) {
char command = BTSerial.read();
if (command == 'A') {
runBrushless();
} else if (command == 'S') {
stopMotors();
} else if (command == '1') {
setSpeed(m1, 1500); // Motor 1 a 1500
setSpeed(m2, 1500); // Motor 2 a 1500
setSpeed(m3, 2000); // Motor 3 a 2000
setSpeed(m4, 2000); // Motor 4 a 2000
}
}
}
void runBrushless() {
Serial.println("Received start command");
currentMillis = 0;
previousMillis = millis();
while (currentMillis < 500000) {
if (BTSerial.available()) {
char command = BTSerial.read();
if (command == 'S') {
stopMotors();
return;
}
}
// Aquí se puede agregar cualquier lógica adicional mientras los motores están en funcionamiento
currentMillis = millis() - previousMillis;
digitalWrite(m1, HIGH);
digitalWrite(m2, HIGH);
digitalWrite(m3, HIGH);
digitalWrite(m4, HIGH);
delayMicroseconds(1350);
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(m4, LOW);
delay(0);
}
}
void stopMotors() {
Serial.println("Received stop command");
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(m4, LOW);
}
void initProcedure() { previousMillis = millis();
Serial.println("throttle up");
while (currentMillis < 3000) {
currentMillis = millis() - previousMillis;
Serial.println(currentMillis);
digitalWrite(m1, HIGH);
digitalWrite(m2, HIGH);
digitalWrite(m3, HIGH);
digitalWrite(m4, HIGH);
delayMicroseconds(max_throttle);
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(m4, LOW);
delay(20);
}
currentMillis = 0;
previousMillis = millis();
Serial.println("throttle down");
while (currentMillis < 4500) {
currentMillis = millis() - previousMillis;
Serial.println(currentMillis);
digitalWrite(m1, HIGH);
digitalWrite(m2, HIGH);
digitalWrite(m3, HIGH);
digitalWrite(m4, HIGH);
delayMicroseconds(min_throttle);
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
digitalWrite(m3, LOW);
digitalWrite(m4, LOW);
delay(20);
}
}
For MPU6050 part, you can study this: Arduino and MPU6050 Accelerometer and Gyroscope Tutorial
Here are the arduino library links for MPU6050
I have deleted your other cross-post @carlosmedin0.
Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.
Repeated cross-posting can result in a suspension from the forum.
In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.
Thanks in advance for your cooperation.
What kind of Arduino are you using?
Pin 4 will not work with analogWrite on an Arduino Uno
And you are not using analogWrite correctly. Please take a look at the Arduino Reference to learn how it works.
if (motor == m1 || motor == m2 || motor == m3 || motor == m4) {
Can motor, as you have written the code, have a value other than m1, m2, m3 or m4?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.