Variable Duties for 40Khz PWM.

Hello!

I am trying to give my 40kHz PWM signal a varied duty cycles oscillating between, for example, 20% and 60% for every 2 cycles. Here is my signal generating code, but I don’t know what modification I need to achieve my goal.

I tried to use analogWrite and delay but not seems to work well. Here is my code:

#include <avr/sleep.h>
#include <avr/power.h>

byte pattern = 0b10101010; //consecutive ports will have an opposite signal

void setup()
{

 DDRC = 0b00011111; //set pins A0 to A4 as outputs
 //PORTC = 0b00000000; //output low signal in all of them
 
  // initialize timer1 
  noInterrupts();           // disable all interrupts
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1  = 0;

  OCR1A = 199;            // compare match register 16MHz / 200 = 80kHz -> 40kHz square
  TCCR1B |= (1 << WGM12);   // CTC mode
  TCCR1B |= (1 << CS10);    // 1 prescaler, no prescaling
  TIMSK1 |= (1 << OCIE1A);  // enable timer compare interrupt
  interrupts();             // enable all interrupts

  // disable ADC
  ADCSRA = 0;  
  
  // turn off everything we can
  power_adc_disable ();
  power_spi_disable();
  power_twi_disable();
  power_timer0_disable();
  power_usart0_disable();

while(true); //avoid entering into the loop
}

ISR(TIMER1_COMPA_vect)          // timer compare interrupt service routine
{
  PORTC = pattern; //output portDV into ports 0 to 7
  pattern = ~pattern; //invert all the bits
}

void loop(){
analogWrite(0,255);
//analogWrite(1,255);
//analogWrite(2,255);
//analogWrite(3,255);
delayMicroseconds(5);
analogWrite(0,0 );
//analogWrite(1,100);
//analogWrite(2,100);
//analogWrite(3,100);
delayMicroseconds(5);


}

Present code with Code Tags </>, not as Quote.

Use a PWM library, your code uses CTC instead of a PWM mode.