#define GPIO_A1 25
#define GPIO_A2 33
#define GPIO_B1 32
#define GPIO_B2 26
#define GPIO_C1 12
#define GPIO_C2 14
#define GPIO_D1 27
#define GPIO_D2 13
#define FREQUENCY_HZ 500
#define RESOLUTION_BITS 7 // 1 - 16
#define MAXIMUM_DUTY_CYCLE ((1 << RESOLUTION_BITS) - 1)
uint8_t pins[][2] = {{GPIO_A1, GPIO_A2}, {GPIO_B1, GPIO_B2}, {GPIO_C1, GPIO_C2}, {GPIO_D1, GPIO_D2}};
void setSpeed(uint8_t motor, int8_t speed){
ledcWrite(2 * motor, speed > 0 ? speed : 0);
ledcWrite(2 * motor + 1, speed < 0 ? -speed : 0);
}
void setup() {
for (uint8_t motor = 0; motor < 4; motor++){
ledcSetup(2 * motor, FREQUENCY_HZ, RESOLUTION_BITS);
ledcSetup(2 * motor + 1, FREQUENCY_HZ, RESOLUTION_BITS);
ledcAttachPin(pins[motor][0], 2 * motor);
ledcAttachPin(pins[motor][1], 2 * motor + 1);
}
}
void loop() {
for (uint16_t motor = 0; motor < 4; motor++){
int8_t step = 1;
for (int8_t speed = 1; !(speed == 0 && step == 1); speed += step){
setSpeed(motor, speed);
delay(30);
if (abs(speed) == MAXIMUM_DUTY_CYCLE)
step = -step;
}
}
}