#define Pin_KNOB_SIG 15
#define Pin_PWM_Write 12
#define Pin_PWM_Read 14
int data = 0;
int speed = 0;
int timer1 = 0;
int timer2 = 0;
int timer3 = 0;
int start = 0;
int current = 0;
void setup() {
pinMode(Pin_PWM_Write, OUTPUT);
pinMode(Pin_PWM_Read, INPUT_PULLUP);
Serial.begin(9600);
ledcSetup(0, 50, 10); // Channel 0, 50 Hz, Resolution 10
ledcAttachPin(Pin_PWM_Write, 0);
attachInterrupt(digitalPinToInterrupt(Pin_PWM_Read), read_PWM_input, CHANGE);
}
void loop() {
data = analogRead(Pin_KNOB_SIG);
speed = map(data, 0, 4095, 26, 123);
/* Speed 26 = 508 us ; Speed 123 = 2404 us */
ledcWrite(0, speed);
Serial.print("Speed: ");
Serial.println(speed);
Serial.print("PWM High : ");
Serial.println(timer1);
Serial.print("PWM Low : ");
Serial.println(timer2);
Serial.print("PWM Total : ");
Serial.println(timer3);
delay(500);
}
void read_PWM_input() {
current = micros();
if (current > start) {
timer1 = current - start;
start = current;
}
timer2 = 20000 - timer1;
timer3 = timer1 + timer2;
}