Manually LOW Frequenzy PWM generation.

Hello Enerybody,
i have following Problem with my Programm.
I need a PWM-Driving Signal for an Solenoid of an Tubrocharger. The Frequenze is only 25-30Hz. I cannot change the Prescaller of the internal Timers. With the low frequency it may possible to write an void to generate the PWM.
I write the following void but it don´t works. I am very new in programming c an arduino.
I also cannot use delay in my programm, because I need a fast programm to regulate the boost of the engine. There are some criticall messurements and regulation during the programm who need a fast response.
Here is my code i tryed diffrent thing but it don´t work.

int NC;
void setup(){
  pinMode(13,OUTPUT);
}

void loop(){
  Low_PWM(13,1,50);
}

int Low_PWM(int Pin, int Fre, int Value ){
  int nextCall=HIGH;
  int PWM_out;
  long time;
  unsigned long pmillis=0;
  long ontime;
  time=1000/Fre;
  ontime=time-(time/100*(100-Value));

  if (millis() - pmillis > time && nextCall==HIGH) {
    if (millis() - pmillis > ontime){ 
      digitalWrite(Pin,HIGH);
      nextCall=LOW;
    }
    else { 
      digitalWrite(Pin,LOW);
      nextCall=HIGH;
    }
    pmillis=millis();  

  }
}

can anyone help me?
Sorry for my bad english I´m an German :stuck_out_tongue: :stuck_out_tongue:

I think part of your problem may be due to the scope of some variables like "pmillis" and "nextCall". Either declare them with global scope at the top of your sketch, or declare them "static".

It will be fine if the PWM_out is an seperate function because the rest of the Program is about 7pages printed syntax and i try to reorganize them to single funktions an my main porblem ist the low Frequency PWM.

After hours of trying i finished my code ist looks like these

unsigned long pmicros;
int nextCall=HIGH;

void setup(){
  pinMode(13,OUTPUT);
 }

void loop(){
Low_PWM(13,30,75);
}

void Low_PWM(int Pin, int Fre, int Value){
long time;
long ontime;
time=1000000/Fre;
  ontime=time/100*Value;
  if (micros() - pmicros > time && nextCall==HIGH) {
    digitalWrite(Pin,HIGH);
    pmicros=micros();
    nextCall=LOW;
  }
  if (micros() - pmicros > ontime&& nextCall==LOW){ 
    digitalWrite(Pin,LOW);
    nextCall=HIGH;  
  }
}

With these Funktion it is allowed to drive a free usable PWM Frequency from 1 to 50Hz and an ontime between 0-100%.