// #include <Arduino.h>
#define ENCODER_CLK 19
#define ENCODER_DT 20
#define ENCODER_BTN 18
#define LED_GPIO 11 // pin
#define PWM1_Ch 1 // 0-5
#define PWM1_Res 10 // bit
#define MAX_DUTY 1023 //max
#define PWM1_Freq 2500 // min. 4-
int counter = 0;
int state = 0;
int PWM1_DutyCycle = 0;
void IRAM_ATTR readEncoder() {
int dtValue = digitalRead(ENCODER_DT);
if (dtValue == HIGH) {
// counter++; // Clockwise
if (PWM1_DutyCycle < MAX_DUTY)
{
// PWM1_DutyCycle++;
PWM1_DutyCycle += 10;
// Constrain duty cycle to 0-MAX_DUTY
PWM1_DutyCycle = constrain(PWM1_DutyCycle, 0, MAX_DUTY);
}
state = 1;
}
if (dtValue == LOW) {
// counter--; // Counterclockwise
if (PWM1_DutyCycle > 0)
{
// PWM1_DutyCycle--;
PWM1_DutyCycle -= 10;
// Constrain duty cycle to 0-MAX_DUTY
PWM1_DutyCycle = constrain(PWM1_DutyCycle, 0, MAX_DUTY);
}
state = 1;
}
}
// Get the counter value, disabling interrupts.
// This make sure readEncoder() doesn't change the value
// while we're reading it.
int getCounter() {
int result;
noInterrupts();
// result = counter;
result = PWM1_DutyCycle;
interrupts();
return result;
}
void setup() {
Serial.begin(115200);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_BTN, INPUT_PULLUP);
// Initialize the LED pin as a PWM output
// ledcSetup(PWM1_Ch, PWM1_Freq, PWM1_Res);
// ledcAttachPin(LED_GPIO, PWM1_Ch);
ledcAttachChannel(LED_GPIO, PWM1_Freq, PWM1_Res, PWM1_Ch);
// ledcAttach(LED_GPIO, PWM1_Freq, PWM1_Res);
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), readEncoder, FALLING);
}
void loop() {
if (state == 1)
{
state = 0;
Serial.println(getCounter());
ledcWrite(LED_GPIO, PWM1_DutyCycle);
}
if (digitalRead(ENCODER_BTN) == LOW) {
PWM1_DutyCycle = MAX_DUTY;
ledcWrite(LED_GPIO, PWM1_DutyCycle);
Serial.println(getCounter());
}
delay(1);
}
// const int ledPin = 22; // Pin connected to the LED
// const int pwmChannel = 0; // PWM channel number
// const int pwmFrequency = 1000; // PWM frequency in Hz
// const int pwmResolution = 10; // PWM resolution (10 bits = 0-1023)
// const int clkPin = 10; // CLK pin of the rotary encoder
// const int dtPin = 11; // DT pin of the rotary encoder
// volatile int dutyCycle = 0; // Duty cycle for PWM (0-1023)
// volatile int lastClkState; // Last state of the CLK pin
// void IRAM_ATTR handleEncoder() {
// int clkState = digitalRead(clkPin);
// if (clkState != lastClkState) {
// if (digitalRead(dtPin) != clkState) {
// dutyCycle += 1; // Rotate clockwise
// } else {
// dutyCycle -= 1; // Rotate counterclockwise
// }
// // Constrain duty cycle to 0-1023
// dutyCycle = constrain(dutyCycle, 0, 1023);
// ledcWrite(pwmChannel, dutyCycle); // Update PWM duty cycle
// }
// lastClkState = clkState; // Update last state
// }
// void setup() {
// Serial.begin(115200);
// // Initialize the LED pin as a PWM output
// // ledcSetup(pwmChannel, pwmFrequency, pwmResolution);
// // ledcAttachPin(ledPin, pwmChannel);
// ledcAttachChannel(ledPin, pwmFrequency, pwmResolution, pwmChannel);
// // ledcAttach(ledPin, freq, resolution);
// // Initialize rotary encoder pins
// pinMode(clkPin, INPUT);
// pinMode(dtPin, INPUT);
// // Read initial state of the CLK pin
// lastClkState = digitalRead(clkPin);
// // Attach interrupt to the CLK pin
// attachInterrupt(digitalPinToInterrupt(clkPin), handleEncoder, CHANGE);
// }
// void loop() {
// // Print the current duty cycle to the Serial Monitor
// Serial.print("Duty Cycle: ");
// Serial.println(dutyCycle);
// delay(500); // Update every half second
// }Loading
esp32-c6-devkitc-1
esp32-c6-devkitc-1