Repeating only a specific condition during a loop

Hi Guys,

i am hoping someone can help me Repeating only a specific condition during a void loop. Basically, i have a set of conditiond within the void loop sections, and i want to repeat the bottom part the void loop a specific number of times. how would i go about that ?

not much to go on there,

While( your condition == true ) // loop until you conditions become true { Act on it


please post your code or at least what you have so far, might want to have a look here,97455.0.html

Not much information to go on here but the usual way to repeat something x times is the ‘for’ command

Funky_Arduino: Hi Guys,

i am hoping someone can help me Repeating only a specific condition during a void loop. Basically, i have a set of conditiond within the void loop sections, and i want to repeat the bottom part the void loop a specific number of times. how would i go about that ?

Set up a variable, a global byte (or static byte inside loop()) and change the value however you want the condition to run next time through loop. If you want something to run 3 times then you can make the variable = 3 in setup() then each time through loop() check if it's > 0. If so then run the specific code and subtract 1 from the variable. 3-2-1, it runs, 0 it doesn't run and the variable doesn't change (unless you code for that on say, a button press). All that without -another- set of braces and indents to go with a for-next and you get code that's easier to modify.

hi all,

thanks for your replies. I have attached the code.
Essentially i want to repeat the second for loop 3 types. The first for loop will run to generate 60 pwm values, but i dont want it do do anything more then that.
Im not quite sure on how to implement the solutions you guys have suggested in the previous posts as i am fairly new to arduino and programming in general.

Assistance is appreciated

void_example.ino (390 Bytes)

Had a look at your code, im not being funny but you seem confused.

 void loop() {
for (k=0;k<counter;k++){

pwm_count= pwm_count_old[k];
// Repeat for 60 points before it moves onto the next function
for {count=0;counter;count++){
scaling= Output/10 ; 
pwm_new=scaling* (pwm_default[count]);

analogWrite(pwm_pin,pwm_default); }

delay (2000); // delay after each pwm value

if count>60; 

heres how to perform your first loop

int pwm_pin = 3;  // pwm output pin or whichever pin you choose
void setup()

void loop()
  for (int pwm_count=0; pwm_count<60; pwm_count++) // Repeat for 60 values

if you only want perform an action 3 times just create a new for loop with a value of 3
what is it exactly you wish to do?
you reference scaling / 10, i am not sure what your doing, to break the value of 60 in to 6 steps do this

for (int pwm_count=0; pwm_count<60; pwm_count+=10)


i think i should have made it more clearer… i only pasted the void loop of my code. this line pwm_count= pwm_count_old[k]; is me generating a new set of pwm values by . I have uploaded the entire code now. It sound be more clear what i am trying to achieve, and what i am asking which is repeating the second for loop 3 times. again i am very new at programming and every help is so greatly appreciated.


void_example.ino (1.65 KB)

Im reading through your code now, there are a few errors

two things i find useful when writing code are

Serial.println(pwm_count_old[index]); // debug to serial monitor

so you can see whats happening and also


when you don't want void loop() to continously repeat, also useful for debugging

Now there's a PIC solution if I ever saw one. Run a closed loop inside a closed loop to prevent the second one from running!

LOL @ GoForSmoke, while(1); is usefull for debugging when i dont want the whole procedure to run, besides PIC’s don’t use stupid void loop(){} anyway.

Funky_Arduino this is as much as i can get from your description, you have 61 numbers in the 2 arrays and there was a couple of other syntax errors but this code is working as close as i can get to your original description

int x[60]={ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10,
           11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
           21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
           31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
           41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
           51, 52, 53, 54, 55, 56, 57, 58, 59, 60};

int y[60]={0.00,0.05,0.10,0.16,0.21,0.26,0.31,0.36,0.41,0.45,
           0.50,0.45,0.41,0.36,0.31,0.26,0.21,0.16,0.10,0.05}; // too many numbers here ,0.00};

int pwm_default[60]={  0, 13, 27, 40, 53, 66, 79, 92,104,116,
                     128,116,104, 92, 79, 66, 53, 40, 27, 13}; // too many numbers here ,0};

int pwm_count_old[60]={ 5, 9,14,19,23,28,32,36,41,45,
                       41,36,32,28,23,19,14, 9, 5, 0};

String inString = "";  // does nothing here
int pwm_pin=6;
float output=9;
float pwm_new;
int scalling = 10;
void setup()

void loop()
  for (int index=0; index<60; index++)  // Repeat for 60 points before it moves onto the next function
      Serial.println(pwm_count_old[index]); // Debug to serial
      analogWrite(pwm_pin,pwm_count_old[index]); // i would like to run through each of the pwm_count _old vector values and analog write each value

  for (int do_loop=0; do_loop<3; do_loop++)
      for (int index=0; index<60; index++)
          pwm_new=(output / scalling) * pwm_default[index];  //i would like to run through each of the pwm_default values multiplied by by scaling 
          Serial.println(pwm_new); // Debug to serial

It's simpler if you use what's there as intended.

void loop() // that has to be there anyway { if (end_condition) return; // better/more complete would be put the AVR into sleep mode

BTW, all elements of int y[] you have there will == 0. Integers round off float constants.

I agree there are better ways for doing it, but if you look at the original post there's not alot to go on

Funky, this is an example of doing what you asked about. You still need to apply the principle to your code.

This is a modified version of BlinkWithoutDelay made to limit the number of blinks.
It is tested and working on my UNO running IDE 0022, I think it should work on IDE 1.0.

Wherever I added code I made a comment starting with the word MODIFIED.
Be sure to read the comments in those parts, especially the PLEASE NOTE part.

See how with very little added code you can reach into a program and alter the behavior. If you add other code that changes blinkCounter (say on a button press or serial input), you don’t need to mess with nesting loops or any of that. Just changing the variable controls the action.

/* MODIFIED 8/25/12 to only blink 10 times

 Blink without Delay 
 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 This example code is in the public domain.

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

// MODIFIED 8/25/12 -- adding a variable to limit blinks
byte  blinkCounter; 

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT); 
  // MODIFIED 8/25/12 -- setting the number of blinks to do
  blinkCounter = 10;  

void loop()
  // here is where you'd put code that needs to be running all the time.
  // MODIFIED 8/25/12 to limit the number of blinks
  // PLEASE NOTE THAT code placed above this inside loop() will still run
  if ( blinkCounter < 1 )  return; // press the reset button to blink again 

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) 
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
      ledState = LOW;
      // MODIFIED 8/25/12 -- it blinked ON now OFF so reduce the count

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);