// 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);
/* // 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
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(){
}
void loop(){ // Loop and read the count Serial.println("Encoder count = " + String((int32_t)encoder.getCount()) + " " + String((int32_t)encoder2.getCount())); delay(100);
/* // 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); }
}