/*
** Raspberry Pi Pico SDK PWM Example
** Originally from Pico PMW Example, simplified
** When you stop the simulation, Wokwi downloads as .vcp
**  file that can be viewed in [PulseView](https://sigrok.org/wiki/PulseView)
*/
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/pwm.h"


#define GPIO_SPEAKER 3
#define MAX_DUTY_CYCLE 100 // percent
  
int main() {
  int change = 5;    
  int dutyPercent = 0;
  float clkdiv = 255.0;
  float frequencyDividerChange = 30.0;

  stdio_init_all(); // UART setup for both input and output

  // PWM setup
  gpio_set_function(GPIO_SPEAKER, GPIO_FUNC_PWM);
  uint slice_num = pwm_gpio_to_slice_num(GPIO_SPEAKER);
  pwm_set_clkdiv(slice_num, clkdiv);
  pwm_set_wrap(slice_num, MAX_DUTY_CYCLE);  
  pwm_set_enabled(slice_num, true);

  while (1) {
    dutyPercent += change;
    if (dutyPercent >= MAX_DUTY_CYCLE) {
      dutyPercent = 0;
      if (frequencyDividerChange > 0) {
        clkdiv -= frequencyDividerChange;
        pwm_set_clkdiv(slice_num, clkdiv);
      }

    } // when at max, go directly back to zero
    pwm_set_gpio_level(GPIO_SPEAKER, dutyPercent);
    printf("LED is now %d %% and freq divider %0.1f\r\n", dutyPercent, clkdiv);
    sleep_ms(200);
  }
}
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT
D0D1D2D3D4D5D6D7GNDLOGIC