I am a beginner working on a design project, I am trying to use 3 buttons to control a brushless motor via ESCs (2 motors, 2 ESCs). The code is supposed to start with sending a stop signal (1500 μs) to the motors and then via 2 different buttons, one increases the signal by 100 μs and the other decreases it by 100 μs. With caps at 1900 μs and 1100 μs. I used these motors, https://bluerobotics.com/learn/thruster-usage-guide/
using their exact specifications for the ESC control, however, it doesn't work, which I believe to be the code.
Any tips?
// Include the Servo library
#include <Servo.h>
// Define the pins for the buttons and motors
#define BUTTON1_PIN 2
#define BUTTON2_PIN 3
#define BUTTON3_PIN 7
#define MOTOR1_PIN 4
#define MOTOR2_PIN 5
// Define the initial values for the motors and escs
int motor1_value = 1500;
int motor2_value = 1500;
// Create servo objects for the motors
Servo motor1;
Servo motor2;
// Setup function
void setup() {
// Attach the motors to their respective pins
motor1.attach(MOTOR1_PIN);
motor2.attach(MOTOR2_PIN);
pinMode(LED_BUILTIN, OUTPUT);
// Set the initial positions for the motors
motor1.writeMicroseconds(motor1_value);
motor2.writeMicroseconds(motor2_value);
// Set the button pins as inputs
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
pinMode(BUTTON3_PIN, INPUT_PULLUP);
//Set serial comms
Serial.begin(9600);
}
// Loop function
void loop() {
// Read the state of the buttons
int button1_state = digitalRead(BUTTON1_PIN);
int button2_state = digitalRead(BUTTON2_PIN);
int button3_state = digitalRead(BUTTON3_PIN);
// Check if button 1 is pressed (Up Power)
if (button1_state == LOW) {
// Increase the motor values by 100
motor1_value += 100;
motor2_value += 100;
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(250);
// Check if the motor values are greater than 1900
if (motor1_value > 1900) {
// Set the motor values to 1900
motor1_value = 1900;
motor2_value = 1900;
}
if (motor1_value < 1100) {
motor1_value = 1100;
motor2_value = 1100;
}
// Write the new motor values to the motor
motor1.writeMicroseconds(motor1_value);
motor2.writeMicroseconds(motor2_value);
}
// Check if button 2 is pressed (Down Power)
if (button2_state == LOW) {
// Decrease the motor values by 100
motor2_value -= 100;
motor1_value -= 100;
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(250);
// Check if the motor values are greater than 1900
if (motor1_value > 1900) {
// Set the motor values to 1900
motor2_value = 1900;
motor1_value = 1900;
}
if (motor1_value < 1100) {
motor1_value = 1100;
motor2_value = 1100;
}
// Write the new motor values to the motor
motor2.writeMicroseconds(motor2_value);
motor1.writeMicroseconds(motor1_value);
}
// Set motors too off
if (button3_state == LOW) {
motor1_value = 1500;
motor2_value = 1500;
digitalWrite(LED_BUILTIN, HIGH);
}
motor1.writeMicroseconds(motor1_value);
motor2.writeMicroseconds(motor2_value);
digitalWrite(LED_BUILTIN, LOW);
Serial.print("motor 1 ");
Serial.println(motor1_value);
Serial.print("motor 2 ");
Serial.println(motor2_value);
}