looking for alternative to for loop

Hi All

I'm trying to find a better way to do a sweep between a start rpm to end rpm over an amount of time (rate)

I have it working with the code below but I need to display more to a screen at the same time and when I add the code to display more things and do math functions it starts to slow the rate down a lot,

Is there a way to do the same thing as the for loop without it being affected by the things that need to be displayed?

for (trgtRPM = startRPM1; trgtRPM <=finRPM1; trgtRPM += Srate1 / 10){

setRPM.setValue(trgtRPM); // display stuff.
n2.setValue(calcrpm); //
n2A.setValue(calcrpm); //
duty.setValue(dutycycle); //
anl1.setValue(anlog1); //

}

if I display more than the above it runs slow.

thanks
Johnathan

Srate1 / 10)Calculate this once before the for loop and use the pre calculated value in the for loop

    n2.setValue(calcrpm);       //
       n2A.setValue(calcrpm);     //
      duty.setValue(dutycycle);    //
      anl1.setValue(anlog1);       //

Why do this for each iteration of the for loop ?

Hi Helibob

you first suggestion makes sense thanks I'll try that

I have to do this
setRPM.setValue(trgtRPM); // display stuff
n2.setValue(calcrpm); //
n2A.setValue(calcrpm); //
duty.setValue(dutycycle); //
anl1.setValue(anlog1); //

to display all the values, n2.setValue() etc is the command to send the data to that value on the screen, is there a better way to do this type of thing? if I don't put them in the code like this nothing is displayed and I need to display more than this at the same time

thanks Johnathan

In the code that you posted calcrpm, dutycycle and anlog1 don't appear to change so why do you have to display them each time through the for loop ? Of course, you have not posted most of your code, so I cannot tell what is going on elsewhere in the program.

Hi

calcrpm is the live rpm reading
dutycycle is from a PWM calculation
anlog1 is from analogRead(A1)

I'm basically controlling rpm with PWM output and need it to sweep up and down in a controlled way while displaying all the info

I didn't think it would make any difference to give the rest of the code I only have a problem with this bit, basically I have a set point that can be set to hold the rpm and have buttons to put the set point up and down 250 rpm and a run button to sweep rpm as in the code I posted above,

if I put this bit of code to make a dial gauge displaying RPM it has a big affect on the rate of sweep but works ok when just holding rpm mode

{
int zgpucm = calcrpm;

zgpucm = map(zgpucm, 0, 8100, 0, 278) ;

if (zgpucm >= 0 && zgpucm < 48) {

int zgpucm1 = zgpucm;

zgpucm1 = map(zgpucm1, 0, 48, 312 , 360);

rpm.setValue(zgpucm1);

} else {

int zgpucm2 = zgpucm;

zgpucm2 = map(zgpucm2, 48 , 278, 0, 230);

rpm.setValue(zgpucm2);

}

I don't know how to post the code on here properly

JDMDyno1:
I don't know how to post the code on here properly

Have a look at #7 on this page http://forum.arduino.cc/index.php/topic,148850.0.html

calcrpm is the live rpm reading
dutycycle is from a PWM calculation
anlog1 is from analogRead(A1)

How and why do they change during the for loop ?

so the trgtRPM goes up as the loop runs, the PID changes the PWM value to track the trgtRPM the live rpm calcrpm goes up and the PID tracks it keeping it as close as possible to the trgtRPM

anlog1 is a torque sensor that changes when the load on motor changes

so as the RPM changes I need to see the change in PWM, Torque and RPM plus other calculations I need to do at the same time

First of all, it's not the for loop that is slow; it's the stuff IN the for loop.

Is there a way to do the same thing as the for loop without it being affected by the things that need to be displayed?

Traditionally, displays are read by humans, who are much slower than electronic devices. And the displays are slow as well. If you are trying to do something 1000+ times/second, you probably don't want to display info about what you are doing more than 10 times/second, and perhaps only if there are actually changes.

If you're using a serial display, increase the bitrate.

JDMDyno1:
so the trgtRPM goes up as the loop runs, the PID changes the PWM value to track the trgtRPM the live rpm calcrpm goes up and the PID tracks it keeping it as close as possible to the trgtRPM

anlog1 is a torque sensor that changes when the load on motor changes

so as the RPM changes I need to see the change in PWM, Torque and RPM plus other calculations I need to do at the same time

None of which was discernable from the code you posted.
Have you read read this before posting a programming question ?

Can you see why posting your whole program would be an advantage in providing help ?

You can't make sense of a display updating more than 3 or 4 times per second, it's a blur, so why not update just 3 or 4 times per second?

There's no timing stuff in this loop at all.If you remove the slow bit (updating the display, I'll bet), then this whole loop would run instantly.

You need some explicit timing gear.

for(;;) {
  take the current time using millis() or micros();
  calculate what the rpm should be at the current time;

  if(we havent reached maximum time yet) {
    set the analog out to the calculated value();
    if(it's been a tenth of a second since I last updated the display) {
      update the display;
    }
  }
  else {
    set the analog out to its final max value;
    update the display;
    break;
  }
}