0
votes

Using an Uno and Arduino 1.8.12

I'm trying to use the Fade.ino example code (loop portion below as that's what relevant here) to play with a logic analyzer. When I run the code I see (with the logic analyzer) a series of PWM pulses with PWM period of 2ms and a gradually increasing and then decreasing duty-cycle that appears appropriate to the loop code. What I don't understand is why I don't see the 30ms delay until the PWM pulses have run their ranges from 0 - 255 or 255 - 0.

My expectation had been to see a 30ms delay after each PWM pulse (so something like a 2ms+30ms PWM period). I thought that delay() would block so that the loop and therefore the analogWrite / PWM update would wait before continuing to the next increment. I also tried to reduce the delay to 1ms and found that the PWM period became chaotic - suggesting that the delay is doing something to, perhaps, ensure that one pulse completes before the next? My newb is showing?

// the loop routine runs over and over again forever:
void loop()  { 
  // set the brightness of pin 9:
  analogWrite(led, brightness);    

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  // wait for 30 milliseconds to see the dimming effect    
  delay(30);                            
}

Thanks much for any help.

1

1 Answers

0
votes

Ok, after some more thought I've answered my own question.

The PWM library is constantly sending a signal to the output pin at the current value of brightness. This signal continues (with duty cycle consistent with "brightness" value and a PWM period of approx. 2ms) until delay time is reached at which time the loop is executed again, updating the value of brightness and changing the PWM signal being send to the pin.

I can see this from my logic analyzer where the same value is presented as an unchanging PWM pulse duty cycle until the delay time is reached, at which time the pulse duty cycle changes. Exactly as intended.

Cheers