How Count Void Loops?

What instructions would count 3 Void loops then stop the loop from operation?
thank you

You could use a sentinel variable to only execute the loop body when true.
You could use a counter variable starting at zero to track the number of iterations of the loop.
At the beginning of the loop, you could increment the counter.
At the end of the loop, if the counter is not less than three, you could change the sentinel variable to false.

Another solution

Your current sketch

// your definition of variables

void setup() {
 // your setup sentences
}

void loop() {
  // your loop sentences
}

Modify it like this

// your definition of variables

void setup() {
// your setup sentences
  for (int i = 0; i < 3; i++) {  
  // your loop sentences now here
  }
}

void loop() {
// nothing
}

I placed the "for (int i = 0; i < 3; i++)" at the location in Void and it compiled but it doesn't stop after 3 cycles. If you have the time can you take a look? thank you

```cpp
/*
 * cheapStepper_simple.ino
 * ///////////////////////////////////////////
 * using CheapStepper Arduino library v.0.2.0
 * created by Tyler Henry, 7/2016
 * ///////////////////////////////////////////
 * 
 * this sketch illustrates basic step() functionality of the library:
 * the stepper performs a 1/2 rotation or flip and repeats every hour.
 * 
 * //////////////////////////////////////////////////////
 */

// first, include the library :)

#include <CheapStepper.h>

CheapStepper stepper;
// here we declare our stepper using default pins:
// arduino pin <--> pins on ULN2003 board:
// 8 <--> IN1
// 9 <--> IN2
// 10 <--> IN3
// 11 <--> IN4

 // let's create a boolean variable to save the direction of our rotation

boolean moveClockwise = true;

void setup() {
  // let's set a custom speed of 20rpm (the default is ~16.25rpm)
  for (int i = 0; i < 3; i++)
    stepper.setRpm(20); 
  /* Note: CheapStepper library assumes you are powering your 28BYJ-48 stepper
   * using an external 5V power supply (>100mA) for RPM calculations
   * -- don't try to power the stepper directly from the Arduino
   * 
   * accepted RPM range: 6RPM (may overheat) - 24RPM (may skip)
   * ideal range: 10RPM (safe, high torque) - 22RPM (fast, low torque)
   */
  // let's just set up a serial connection and test print to the console
  
  Serial.begin(9600);
  Serial.println("Ready to start moving!");
  
}

void loop() {

  // let's move a full rotation (4096 mini-steps)
  // we'll go step-by-step using the step() function
  
  for (int s=0; s<512; s++){
    // this will loop 512 times
    // 4096 steps = full rotation using default values
    /* Note:
     * you could alternatively use 4076 steps... 
     * if you think your 28BYJ-48 stepper's internal gear ratio is 63.68395:1 (measured) rather than 64:1 (advertised)
     * for more info, see: http://forum.arduino.cc/index.php?topic=71964.15)
     */

    // let's move one "step" (of the 4096 per full rotation)
    
    stepper.step(moveClockwise);
    /* the direction is based on moveClockwise boolean:
     * true for clockwise, false for counter-clockwise
     * -- you could also say stepper.stepCW(); or stepper.stepCCW();
     */

    // now let's get the current step position of motor
    
    int nStep = stepper.getStep();

    // and if it's divisible by 64...
    
    if (nStep%64==0){ 

      // let's print the position to the console
      
      Serial.print("current step position: "); Serial.print(nStep);
      Serial.println();
      
    }
  }

  // now we've moved 4096 step
 
  // let's wait 2s, 1s = 1000
   delay(2000);

   // and switch directions before starting loop() again
  
  moveClockwise = !moveClockwise;
}

But I didn't say put the for() in loop(), I said put it in setup().
I thought the example was clear...

It was, but OP was not clear in what was actually required. It looks like OP wants to do one small part of the loop 3 times, from the way the for loop was used. @rkruz3 what exactly do you want to happen 3 times?

void loop() {
static int loopcount = 0;

// do stuff

loopcount++;
if (loopcount == 3) {
  while(1); //hang here until reset pressed
  }
}

Hint: here, the C/C++ keyword "void" informs that the loop function returns no value.

I don't think it says "stop only a part". :wink:

It would be best for the PO to explain exactly what he wants to do

Agreed. As is often the case, the code and the request are a bit discordant.

Thank you for the help. Im sorry, I see I made a mistake where I placed it and I was not clear. Ill try to place the code again. But here is what Im trying to do.
I want for the motor to complete the loop 3 times. So the motor steps from 0 to 512, 512 to 0 then 0 to 512 and stops. The 3 is a value I want to change. Here is a copy of the Serial Monitor Print Output from this code that might help make it clearer:

20:50:12.543 -> i�Ready to start moving!
20:50:14.077 -> current step position: 64
20:50:14.124 -> current step position: 128
20:50:14.155 -> current step position: 192
20:50:14.235 -> current step position: 256
20:50:14.281 -> current step position: 320
20:50:14.328 -> current step position: 384
20:50:14.375 -> current step position: 448
20:50:14.421 -> current step position: 512
20:50:16.485 -> current step position: 448
20:50:16.532 -> current step position: 384
20:50:16.580 -> current step position: 320
20:50:16.626 -> current step position: 256
20:50:16.674 -> current step position: 192
20:50:16.721 -> current step position: 128
20:50:16.768 -> current step position: 64
20:50:16.815 -> current step position: 0
20:50:18.849 -> current step position: 64
20:50:18.929 -> current step position: 128
20:50:18.976 -> current step position: 192
20:50:19.022 -> current step position: 256
20:50:19.069 -> current step position: 320
20:50:19.117 -> current step position: 384
20:50:19.148 -> current step position: 448
20:50:19.180 -> current step position: 512
Then stop.

Thank you for the help.

Is the problem solved?

Yes that worked. I will try to grow the code with that bit. thank you!

So if you really do not want the program to stop, you can try renaming your original void loop() into void loopMotor() and wite a new void loop() to start growing your program with something like this:

void loop() {
  static int loopTimes = 0;
  if (loopTimes < 3) {
    loopTimes++;
    loopMotor();
    Serial.println("\nLoop #" + String(loopTimes) + "\n"); // this line is optional, of course
  } 
}

void loopMotor() {

  // let's move a full rotation (4096 mini-steps)
  // we'll go step-by-step using the step() function

  for (int s = 0; s < 512; s++) {
...
1 Like

Thank you. So helpful. Your exactly correct. I want to pause after 3 loops pause for 15 minutes then repeat. So these tips will help me a lot as I fumble through creating this code. Thanks so much!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.