Use analogWrite api instead of ledc

This commit is contained in:
Oleg Kalachev
2025-11-18 16:54:51 +03:00
parent 795b248b94
commit 531b3f4d04

View File

@@ -25,13 +25,15 @@ const int MOTOR_FRONT_LEFT = 3;
void setupMotors() { void setupMotors() {
print("Setup Motors\n"); print("Setup Motors\n");
#ifdef ESP32
// configure pins
ledcAttach(MOTOR_0_PIN, PWM_FREQUENCY, PWM_RESOLUTION); ledcAttach(MOTOR_0_PIN, PWM_FREQUENCY, PWM_RESOLUTION);
ledcAttach(MOTOR_1_PIN, PWM_FREQUENCY, PWM_RESOLUTION); ledcAttach(MOTOR_1_PIN, PWM_FREQUENCY, PWM_RESOLUTION);
ledcAttach(MOTOR_2_PIN, PWM_FREQUENCY, PWM_RESOLUTION); ledcAttach(MOTOR_2_PIN, PWM_FREQUENCY, PWM_RESOLUTION);
ledcAttach(MOTOR_3_PIN, PWM_FREQUENCY, PWM_RESOLUTION); ledcAttach(MOTOR_3_PIN, PWM_FREQUENCY, PWM_RESOLUTION);
#else
analogWriteResolution(PWM_RESOLUTION);
analogWriteFrequency(PWM_FREQUENCY);
#endif
sendMotors(); sendMotors();
print("Motors initialized\n"); print("Motors initialized\n");
} }
@@ -45,10 +47,10 @@ int getDutyCycle(float value) {
} }
void sendMotors() { void sendMotors() {
ledcWrite(MOTOR_0_PIN, getDutyCycle(motors[0])); analogWrite(MOTOR_0_PIN, getDutyCycle(motors[0]));
ledcWrite(MOTOR_1_PIN, getDutyCycle(motors[1])); analogWrite(MOTOR_1_PIN, getDutyCycle(motors[1]));
ledcWrite(MOTOR_2_PIN, getDutyCycle(motors[2])); analogWrite(MOTOR_2_PIN, getDutyCycle(motors[2]));
ledcWrite(MOTOR_3_PIN, getDutyCycle(motors[3])); analogWrite(MOTOR_3_PIN, getDutyCycle(motors[3]));
} }
bool motorsActive() { bool motorsActive() {