Can't update variable

I managed to get timer1 to change its frequency to 25 kHz and I can vary the duty cycle individually for its two pins. I follow it in the scope and I can change the duty cycle (pwmA and pwmB) by changing the variable value manually and push out the new code. But i dont seem to be able to change its value in the function loop or from anywhere alse for some reason.

Btw it does only work to have the timer settings either in the void setup() or in a separate function as it is now. Doesnt work in the main loop for me, if anyone wonders about that.

Probably something easy, dunno, my eyes are tired. Appreciate more eyes.

//Dual 25kHz PWM using Timer1 Mode 10

int value = 100;
int pwmA = value; // Duty cycle (0-320 = 0-100%)
int pwmB = 300; // Duty cycle (0-320 = 0-100%)

void setup() {
  pinMode(11, OUTPUT);  //pwmA
  pinMode(12, OUTPUT); //pwmB
  pwm();

}

void loop() {

}

void pwm(){
  
  TCCR1A = 0;            //clear timer registers
  TCCR1B = 0;
  TCNT1 = 0;

  TCCR1B |= _BV(CS10);   //no prescaler
  ICR1 = 320;            //PWM mode counts up 320 then down 320 counts (25kHz)

  OCR1A = pwmA;          //0-320 = 0-100% duty cycle
  TCCR1A |= _BV(COM1A1); //output A clear rising/set falling

  OCR1B = pwmB;          //0-320 = 0-100% duty cycle
  TCCR1A |= _BV(COM1B1); //output B clear rising/set falling

  TCCR1B |= _BV(WGM13);  //PWM mode with ICR1 Mode 10
  TCCR1A |= _BV(WGM11);  //WGM13:WGM10 set 1010

  value = 300;
}

I can change the duty cycle by changing the variable value manually and push out the new code. But i dont seem to be able to change its value in the function loop or from anywhere alse for some reason.

Are you expecting to see the duty cycle of pwmA change from 100 to 300 with your code?

OCR1A never gets assigned the new value.

cattledog:
Are you expecting to see the duty cycle of pwmA change from 100 to 300 with your code?

OCR1A never gets assigned the new value.

Yes that is what I am trying to do. Ie I want to be able to update the duty cycle of pwmA and B. OCR1A and B shall take its value from pwmA and B right? In the future by temp sensors, just now I am only trying to change it from somewhere by changing pwmA.

I guess now void pwm() only runs once and that is why it doesnt update. I need to loop the OCR1A/B somehow.

OCR1A and B shall take its value from pwmA and B right?

pwmA and pwmB are only given values once in your code when they are initialized as global variables. You never change them anywhere.

If you want to change it, you will have to call pwm() again. It would be better to pass in the variables as parameters than using globals.

//Dual 25kHz PWM using Timer1 Mode 10

void setup() {
  pinMode(11, OUTPUT);  //pwmA
  pinMode(12, OUTPUT); //pwmB
}

void loop() {
  pwm(100, 300);
  delay(5000);
  pwm(200,200);
  delay(5000);
  pwm(300,100);
  delay(5000);
}

void pwm(int pwmA, int pwmB) {

  TCCR1A = 0;            //clear timer registers
  TCCR1B = 0;
  TCNT1 = 0;

  TCCR1B |= _BV(CS10);   //no prescaler
  ICR1 = 320;            //PWM mode counts up 320 then down 320 counts (25kHz)

  OCR1A = pwmA;          //0-320 = 0-100% duty cycle
  TCCR1A |= _BV(COM1A1); //output A clear rising/set falling

  OCR1B = pwmB;          //0-320 = 0-100% duty cycle
  TCCR1A |= _BV(COM1B1); //output B clear rising/set falling

  TCCR1B |= _BV(WGM13);  //PWM mode with ICR1 Mode 10
  TCCR1A |= _BV(WGM11);  //WGM13:WGM10 set 1010
}

Thanks for your help. Just asking the question helped me think;) This worked:

//Dual 25kHz PWM using Timer1 Mode 10

int pwmA = 200; // Duty cycle (0-320 = 0-100%)
int pwmB = 300; // Duty cycle (0-320 = 0-100%)

void setup() {
  pinMode(11, OUTPUT);  //pwmA
  pinMode(12, OUTPUT); //pwmB
  setpwm();

}

void loop() {
  OCR1A = pwmA;          //0-320 = 0-100% duty cycle
  delay(5000);
  pwmA = 20;
}

void setpwm(){
  
  TCCR1A = 0;            //clear timer registers
  TCCR1B = 0;
  TCNT1 = 0;

  TCCR1B |= _BV(CS10);   //no prescaler
  ICR1 = 320;            //PWM mode counts up 320 then down 320 counts (25kHz)

  OCR1A = pwmA;          //0-320 = 0-100% duty cycle
  TCCR1A |= _BV(COM1A1); //output A clear rising/set falling

  OCR1B = pwmB;          //0-320 = 0-100% duty cycle
  TCCR1A |= _BV(COM1B1); //output B clear rising/set falling

  TCCR1B |= _BV(WGM13);  //PWM mode with ICR1 Mode 10
  TCCR1A |= _BV(WGM11);  //WGM13:WGM10 set 1010


}

blh64:
If you want to change it, you will have to call pwm() again. It would be better to pass in the variables as parameters than using globals.

That was elegant. Is it preferable to the way I did it you think? I had as I said problems trying to put the whole function into loop.

https://www.arduino.cc/en/Reference/FunctionDeclaration