reduce the loop time

Hey,

For a project I have made a long program. The running time was quite long. Here the steppermotor had visible problems with.

I am now converting this program to a new program. The goal is to shorten running time and make the program clearer. Here I have a number of questions about.

  • When the steppermotor turns, the running time will be a lot longer. It seems that the stepper motor does not bother. Does anyone know if the stepper motor really does not bother? I use NEMA17 with a DM422C driver.

  • I had the idea of using a second loop. My question if a second loop will reduce the loop time?

feedback or other ideas are welcome.

Does your loop() have a lot of delays()? Please post your code. HowToPostCode

edgemoron:
Does your loop() have a lot of delays()? Please post your code. [HowToPostCode ](http://"http://"http://" How to use this forum - please read - Website and Forum - Arduino Forum""")

thank you for your responds.

I only use very small delays when the motor turns. But this are the only ones in the program.

if (pulseCountMotor <= pulseInputMotor)
  {
    digitalWrite(enablePin, HIGH);
    delayMicroseconds(6);                   //see DM422 specifications chapter 10
    digitalWrite(dirPin, dirSet);
    delayMicroseconds(6);                   //see DM422 specifications chapter 10
    digitalWrite(pulsePin, HIGH);
    delayMicroseconds(timeStepsMotor);
    digitalWrite(pulsePin, LOW);
    delayMicroseconds(6);                  //see DM422 specifications chapter 10
    pulseCountMotor++;                           //adding each course
  }
  if (pulseCountMotor > pulseInputMotor) {
    pulseCountMotor = 0;
    digitalWrite(enablePin, LOW);
    timePassed = millis();
  }

This is a project for my intership. The stuppid part of that is that I am not allowed to share the code......

Yes, that is a stupid part, isn't it?
Good luck

The goal is to shorten running time

The running time of what exactly ?
Please outline in words what the program is intended to do and which parts you think are slowing it down.

The second example in this Simple Stepper Code uses millis() and micros() rather than delay().

And the demo Several Things at a Time illustrates the use of millis() to manage timing without blocking

Also maybe have a look at Planning and Implementing a Program

By far the best way to get useful advice is to post your complete program.

...R

UKHeliBob:
The running time of what exactly ?
Please outline in words what the program is intended to do and which parts you think are slowing it down.

I have checked the loop time the following way:

void loop() {
  unsigned long time = millis();
  for (int i = 0; i < 1000; ++i) {
    .
    .
    .
    .
    [code]
    .
    .
    .
  }
  Serial.println(millis() - time);
}

ther are a coppel of states in the program:

  • 'the wait state': loop time 0.082 - 0.083: None of the components are doing anything
  • 'only the steppermotor is turning state': loop time 0.513 - 0.514: only the steppermotor is turning the reset is waiting
  • 'doing diffrend things': loop time 0.521: doing more things at the same time

I think that the steppermotor slows the program down

I understand that it's much easier with the code. Therefore I would like to add it yet. However, this is too long in this message. I have added the program in the attachment

progam.zip (5.47 KB)

FeelGoodGirl:
I understand that it's much easier with the code. Therefore I would like to add it yet. However, this is too long in this message. I have added the program in the attachment

I looked at your code and have a few comments:

1: learn to use structs to simplify your data, especially helpful when you have co-dependant data like:

int SDday;                              //SD:
int SDmonth;                            //SD:
int SDyear;                             //SD:
int SDhour;                             //SD:
int SDminute;                           //SD:
int SDsecond;                           //SD:

2: use the const keyword for variables that won't change:

//**********************Customize***********************
int norLiquidON = 5400;                  //NORMAL SERVING: The time that the pump with Liquid is on. (miliseconds)
long norPulseGoal = 400;                //NORMAL SERVING: The pulse from the flowmeter.

int strLiquidON = 6800;                  //STRONG SERVING: The time that the pump with Liquid is on. (miliseconds)
long strPulseGoal = 400;                //STRONG SERVING: The pulse from the flowmeter.

long maxTimeWater = 20000;              //WATER ON TIME: the max time the water can stay on.

long almostBOX = 2147483640;            //WARNING: when the BOX almost empty. Counts the 'TimeOnS' from normal and strong. (miliseconds)
long emptyBOX = 2147483640;             //WARNING: when the BOX is empty. Counts the 'TimeOnS' from normal and strong. (miliseconds)

int delayWaterValveO = 20;              //OPEN: !!DO NOT MAKE SMALLER THEN 20!! The delay between the push on the button and opening the water valve. (miliseconds)
int delayAntiDripO = 200;               //OPEN: The delay between the opening of the water valve and the opening of the anti drip. (miliseconds)
int delayWaterValveC = 1000;            //CLOSE: The delay beteen closing the anti drip and closing the water valve. (miliseconds)

int intervalPanic = 50;                 //LED WARNING: interval at which to blink, when panic button is pushed. (miliseconds)
int intervalAlmost = 1000;              //LED WARNING: interval at which to blink, when BOX is almost empty. (miliseconds)
int intervalEmpty = 20;                 //LED WARNING: interval at which to blink, when BOX is empty. (miliseconds)
int minFading = 30;                     //LED FADING: The minimum strength of te led while fading
int maxFading = 255;                    //LED FADING: The maximum strength of te led while fading
int upTimeFading = 500;                 //LED FADING: The time that the LED needs to fade up. (miliseconds)
int downTimeFading = 1500;              //LED FADING: The time that the LED needs to fade down. (miliseconds)

int waitServLed = 175;                  //LED FADING: The strength of the LED between fading
int ledReset = 0;                       //LED FADING: The led in error

int saftyReset = 3000;                  //SAFETY TIME: The time de reset button needs to be pushed. (miliseconds)

boolean dirSet = HIGH;                  //BOX MOTOR: direction motor, HIGH is counterclockwise, LOW is clockwise
long timerSet = 60000;                   //BOX MOTOR: The interval between every motor movement. (miliseconds)
int rpm = 80;                           //BOX MOTOR: the rpm from the motor.
float MStep = 400;                     //BOX MOTOR: !!ONLY CHANGE IF THIS IS CHANGED ON THE DRIVER!!  pulse/rev, adjustable on driver.
int rotations = 3;                      //BOX MOTOR: After the 'timerSet' is over, the moter turns x times.

//---------------------------------------------------------------------------------------------------------------------------
  1. in order to speed your loop(), you can try to combine the state conditionals where you are testing for a timer expiry and a state. Instead of like this using four tests:
  if (stageOn == 6 && emergencyActive != 1) 
  {

  }
  else if (stageOn == 6 && emergencyActive == 1) {

more like this, using only two (and reads easier):

  if (stageOn == 6) 
  {
    if(emergencyActive == 1)
    {

    }
    else
    {

    }
  }
  1. use the F Macro to store string constants into program space (although that may take longer to process during program execution):
Serial.println(F("Water valve was to long open"));
  1. I see you eliminated Serial debug to speed things up, that will help...

  2. You are reading the button pins several times each through loop... just read them once and store the value to a local variable that you use for each condition

  3. variables modified in an ISR should be declared using the volatile keyword. In your case it wasn't optimized away because it is also use in loop() but volatile informs the compiler that the hardware has access to the variable and can change any time, unbeknownst to the program.

volatile long pulseCount = 0;                    //FLOW METER: Flow sensor signal counter

Just a few suggestions...

Why do you need to speed up the loop() function ?
What problems are you trying to solve ?

Thank you BulldogLowell for your feedback. I am going to try to work with it!

UKHeliBob:
Why do you need to speed up the loop() function ?
What problems are you trying to solve ?

The problem is that the loop time is nog to long when i want my steppermotor driver on 1600 steps.

BulldogLowell:
2: use the const keyword for variables that won't change:

//**********************Customize***********************

int norLiquidON = 5400;                  //NORMAL SERVING: The time that the pump with Liquid is on. (miliseconds)
long norPulseGoal = 400;                //NORMAL SERVING: The pulse from the flowmeter.

int strLiquidON = 6800;                  //STRONG SERVING: The time that the pump with Liquid is on. (miliseconds)
long strPulseGoal = 400;                //STRONG SERVING: The pulse from the flowmeter.

long maxTimeWater = 20000;              //WATER ON TIME: the max time the water can stay on.

long almostBOX = 2147483640;            //WARNING: when the BOX almost empty. Counts the 'TimeOnS' from normal and strong. (miliseconds)
long emptyBOX = 2147483640;            //WARNING: when the BOX is empty. Counts the 'TimeOnS' from normal and strong. (miliseconds)

int delayWaterValveO = 20;              //OPEN: !!DO NOT MAKE SMALLER THEN 20!! The delay between the push on the button and opening the water valve. (miliseconds)
int delayAntiDripO = 200;              //OPEN: The delay between the opening of the water valve and the opening of the anti drip. (miliseconds)
int delayWaterValveC = 1000;            //CLOSE: The delay beteen closing the anti drip and closing the water valve. (miliseconds)

int intervalPanic = 50;                //LED WARNING: interval at which to blink, when panic button is pushed. (miliseconds)
int intervalAlmost = 1000;              //LED WARNING: interval at which to blink, when BOX is almost empty. (miliseconds)
int intervalEmpty = 20;                //LED WARNING: interval at which to blink, when BOX is empty. (miliseconds)
int minFading = 30;                    //LED FADING: The minimum strength of te led while fading
int maxFading = 255;                    //LED FADING: The maximum strength of te led while fading
int upTimeFading = 500;                //LED FADING: The time that the LED needs to fade up. (miliseconds)
int downTimeFading = 1500;              //LED FADING: The time that the LED needs to fade down. (miliseconds)

int waitServLed = 175;                  //LED FADING: The strength of the LED between fading
int ledReset = 0;                      //LED FADING: The led in error

int saftyReset = 3000;                  //SAFETY TIME: The time de reset button needs to be pushed. (miliseconds)

boolean dirSet = HIGH;                  //BOX MOTOR: direction motor, HIGH is counterclockwise, LOW is clockwise
long timerSet = 60000;                  //BOX MOTOR: The interval between every motor movement. (miliseconds)
int rpm = 80;                          //BOX MOTOR: the rpm from the motor.
float MStep = 400;                    //BOX MOTOR: !!ONLY CHANGE IF THIS IS CHANGED ON THE DRIVER!!  pulse/rev, adjustable on driver.
int rotations = 3;                      //BOX MOTOR: After the 'timerSet' is over, the moter turns x times.

//---------------------------------------------------------------------------------------------------------------------------

@BulldogLowell, What do you mean whit 'const keyword'??