Of course, your if blocks are wrong, specifically the else part is linked to the wrong block. That's why you have indentation (and yours was really messed up - that was the first routine fix, ctrl-T in the IDE), makes those things easier to track!
Now you mentioned it I saw it almost instantly (largely thanks to that indentation), didn't look for any other issues when fixing the type declarations.
Small difference - big effects ![]()
// Purpose: To produce a PWM signal output at 20Hz frequency and a duty cycle that has a minm of 15 % and max'm of 85%, the pot adjusted for any value in bewtween
// Arduino UNO board design: Has an input pot resistance value from pot wiper into pin A0 (pot has VCC from 5v pin and Ov from Grd pin). PWM signal ouput defined on pin 9
// Testing: currently using oscillosocpe on output pin 9, unloaded.
// Previous code worked at 30.5Hz, successfully varying LED brightness but frequency was too high for motor controller to work and turn the motor.
// STATUS: DOES COMPILE SUCCESSFULLY BUT NO PWM OUTPUT SIGNAL NOW PRODUCED ON PIN 9.
const byte potPin = A0;Â // Analog input pin that has the potentiometer wiper resistance value
const byte outPin = 9;Â Â // PWM output pin that Motor Controller) (or LED in testing) is attached to
float dutyCycle;Â Â Â Â Â // Defines the duty cycle, the % of the period (the cycle time) the PWM pulse is high
unsigned long period;Â Â // Defines the cycle time ( 1 / frequency) for PWM output
unsigned long lastChange; // The variable for incrementing the time through the loop
byte outState = LOW;Â Â Â // The variable holding the value of Hi or Lo output that is derived through the loop
void setup() {
 period = 50000;    // 50,000 us period, to be used to set the output signal at 20Hz frequency.
}
void loop() {
 dutyCycle = map(analogRead(potPin), 0, 1023, 1500, 8500) / 10000.0; // returns 15% to 85% duty cycle range based on pot rotation reading 0-1023.
 unsigned long onTime = period * dutyCycle;             // defines the high duration in us.
 unsigned long offTime = period * (1 - dutyCycle);         // defines the low duration in us.
 if (outState == HIGH) {
  if (micros() - lastChange > onTime) {
   outState = LOW;
   lastChange += onTime;
  }
 }
 else {
  if (micros() - lastChange > offTime) {
   outState = HIGH;
   lastChange += offTime;
  }
 }
 digitalWrite(outPin, outState);
}