Serial controlled pwm on all pins

hello guys,
i need to control a bunch of electromagnets with pwm.

Since i have some issues on multiple fronts, i would like to make some clever code to control through serial

  1. pwm
    2.period of duty cycle

Do i have to use the delay or there is some better solution?
im reading heavy code will mess up my timing . Any way to measure loop execution time?

here is a super naive approach

float period ;//serially controlled
float pwm;//ranging from 0..1 representing percentage of ON within duty cycle

void loop() {  
  for (int i=2; i<14 ; i++)
  {
  led = i;
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(period*pwm);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(period-(period-pwm));   
  }  
}

any thoughts greatly appreciated.

There is a better solution - "blink without delay" - where you monitor the time and control when output are turned off & on.
Take a look at the code in my post "piano tones micros" where I have what is essentially 13 PWM outputs created that all run interactively.
The frequency for each output is predefined, you could change that to make them interactive via serial port.

blink with out delay, was right under my nose.

thanks, ill try to wrap my head round your code. looks like what i need.

It was written for a '1284P, which has 32 IO.
Will run on a '328P, with 20 IO. Delete the button reading, change the flags so each output is on all the time, and update the values in the frequency array based on what you receive via serial messages.

allrigth, i knew nothing of the existence of port registers but seems interesting,

I think im trying to do smth different or i dont understant your code correctly.

I establish a duty cycle with

 if( currentTime - previousTime > interval)
{ previousTime = previousTime + interval; 
do other stuff }

I would like though to turn on and off within that duty cycle in pwm like fashion.
So inside that loop create another time check to turn on for 25% of the interval.
Im scratching my head a bit . Any ideas most welcome.

 if( currentTime - previousTime > interval)
{ previousTime = previousTime + interval;

That would result in previousTime being equal to currentTime, wouldn't it?

yea , so when current time updates i can toggle on/off.
its the blink without delay thing.

Any ideas for further timecheck.?
is setting manually the dutycycle a naive approach ?

here is another approach
is not working really, not sure why.

  unsigned long currentMillis = millis();
  //get remainder
  long modulo = currentMillis % interval ;
 //bring back to 0..1 range
  modulo = modulo / interval; 
  //check it
  if (modulo > 0.5) {ledState == HIGH;}
  else {ledState == LOW;}
  
  digitalWrite(ledPin, ledState);

shouldnt it be a blinkwithout delay approach?

 //bring back to 0..1 range
  modulo = modulo / interval;

You mean "bring back to 0 OR 1". Integer arithmetic is being performed.