Оформи код соответствующим тэгом, см. Правила
Доброго времени суток! Имеется код в котором осуществляется управление бесколлекторным двигателем двумя кнопками, при нажатии на первую кнопку двигатель должен включаться и крутиться до тех пор пока повторно не нажмется эта же кнопка. А при нажатии на вторую он должен работать пока кнопка нажата. Суть проблемы заключается в том что вторая кнопка работает как нужно, двигатель крутится пока нажата. С первой же кнопкой печально все при нажатии на неё ни происходит ни чего. Ткните носом)))) где ошибка.
C++:
// Include the necessary libraries
#include <Servo.h>
// Define the pins for the buttons and hall sensor
#define BUTTON_1_PIN 2
#define BUTTON_2_PIN 3
#define HALL_SENSOR_PIN A0
// Define the maximum run time in milliseconds (60 minutes)
#define MAX_RUN_TIME 3600000
// Define the desired speed in RPM
#define DESIRED_SPEED 2100
// Define the ESC control pin
#define ESC_PIN 9
// Create a servo object for the ESC
Servo esc;
// Variables to store the button states
int button1State = 0;
int button2State = 0;
// Variable to store the current speed
int currentSpeed = 0;
// Variable to store the start time
unsigned long startTime = 0;
// Setup function
void setup() {
// Initialize the serial communication
Serial.begin(9600);
// Set the button pins as inputs
pinMode(BUTTON_1_PIN, INPUT);
pinMode(BUTTON_2_PIN, INPUT);
// Attach the ESC to the control pin
esc.attach(ESC_PIN);
// Set the initial speed to 0
esc.writeMicroseconds(1000);
}
// Loop function
void loop() {
// Read the button states
button1State = digitalRead(BUTTON_1_PIN);
button2State = digitalRead(BUTTON_2_PIN);
// If button 1 is pressed
if (button1State == HIGH) {
// Check if the motor has been running for more than 60 minutes
if (millis() - startTime > MAX_RUN_TIME) {
// Stop the motor
esc.writeMicroseconds(1000);
// Print a message to the serial monitor
Serial.println("Maximum run time reached. Motor stopped.");
} else {
// Set the motor speed to the desired speed
esc.writeMicroseconds(map(DESIRED_SPEED, 0, 10000, 1000, 2000));
// Print a message to the serial monitor
Serial.println("Motor running at 2800 RPM.");
}
}
// If button 2 is pressed
if (button2State == HIGH) {
// Set the motor speed to the desired speed
esc.writeMicroseconds(map(DESIRED_SPEED, 0, 10000, 1000, 2000));
// Print a message to the serial monitor
Serial.println("Motor running at 2800 RPM.");
} else {
// Stop the motor
esc.writeMicroseconds(1000);
// Print a message to the serial monitor
Serial.println("Motor stopped.");
}
// Update the current speed
currentSpeed = map(pulseIn(HALL_SENSOR_PIN, HIGH), 0, 10000, 0, 10000);
// Print the current speed to the serial monitor
Serial.print("Current speed: ");
Serial.print(currentSpeed);
Serial.println(" RPM");
// Delay for 100 milliseconds
delay(100);
}
Изменено: