madhephaestus / ESP32Servo

Arduino-compatible servo library for the ESP32
133 stars 53 forks source link

pwm.adjustFrequency needs to be written twice to change the freq. #30

Open cnc4less opened 1 year ago

cnc4less commented 1 year ago

include

include

include

int APin = 13; ESP32PWM pwm; int freq = 1000;

ESP32Encoder encoder; ESP32Encoder encoder2;

// timer and flag for example, not needed for encoders unsigned long encoder2lastToggled; bool encoder2Paused = false;

void setup(){

// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
pwm.attachPin(APin, freq, 10); // 1KHz 8 bit

Serial.begin(115200);
// Enable the weak pull down resistors

//ESP32Encoder::useInternalWeakPullResistors=DOWN;
// Enable the weak pull up resistors
ESP32Encoder::useInternalWeakPullResistors=UP;

// use pin 19 and 18 for the first encoder
encoder.attachHalfQuad(19, 18);
// use pin 17 and 16 for the second encoder
encoder2.attachHalfQuad(17, 16);

// set starting count value after attaching
encoder.setCount(37);

// clear the encoder's raw count and set the tracked count to zero
encoder2.clearCount();
Serial.println("Encoder Start = " + String((int32_t)encoder.getCount()));
// set the lastToggle
encoder2lastToggled = millis();

}

void loop(){ // Loop and read the count Serial.println("Encoder count = " + String((int32_t)encoder.getCount()) + " " + String((int32_t)encoder2.getCount())); delay(100);

// every 5 seconds toggle encoder 2

/*
if (millis() - encoder2lastToggled >= 5000) {
    if(encoder2Paused) {
        Serial.println("Resuming Encoder 2");
        encoder2.resumeCount();
    } else {
        Serial.println("Paused Encoder 2");
        encoder2.pauseCount();
    }

    encoder2Paused = !encoder2Paused;
    encoder2lastToggled = millis();
}
*/

/* // fade the LED on thisPin from off to brightest: for (float brightness = 0; brightness <= 0.99; brightness += 0.01) { // Write a unit vector value from 0.0 to 1.0 pwm.writeScaled(brightness); delay(50); }

*/

// delay(1000);
//  fade the LED on thisPin from brithstest to off:
for (float brightness = 0.0; brightness <= 1; brightness += 0.01)
{
    freq += 10;
    // Adjust the frequency on the fly with a specific brightness
    // Frequency is in herts and duty cycle is a unit vector 0.0 to 1.0
    pwm.adjustFrequency(5, brightness); // update the time base of the PWM
    pwm.adjustFrequency(5, brightness); // update the time base of the PWM
    delay(50);
}

// pause between LEDs:
delay(500);
freq = 10;
// pwm.adjustFrequency(freq, 0.5); // reset the time base

}

cnc4less commented 1 year ago

the same code above works fine on esp32 but crashes on esp32s3