# How do i add a loop??

Hey guys, I was wondering if you could help me with, what i think, is a simple problem that has me quite stuck! (Quite embarrassed!)

How do i get it to turn my LED off after it has flashed 10 times??

I will be using this to control my solenoid but for now, a LED will do!

This is my closest attempt so far:

``````const int solenoid = 3;
int redled = 13;

int wait = 10;      // 10ms internal crossFade delay; increase for slower fades
int hold = 0;       // Optional hold when a color is complete, before the next crossFade
int DEBUG = 1;      // DEBUG counter; if set to 1, will write values back via serial
int loopCount = 60; // How often should DEBUG report?
int repeat = 3;     // How many times should we loop before stopping? (0 for no stop)
int j = 0;          // Loop counter for repeat

void setup()   {
// initialize the digital pin as an input:
pinMode(redled, INPUT);

}

void loop(){

if (repeat) { // Do we loop a finite number of times?
j += 3;

if (j >= repeat) { // Are we there yet?

digitalWrite(redled,HIGH);
delay(50);
digitalWrite(redled,LOW);
delay(50);

exit(j);         // If so, stop.

}
}
}
``````

I really thought that this would work? What do you think? why does it only flash once??

looks like your trying to over complicate things.

``````const int solenoid = 3;
int redled = 13;

int repeat =10;     // How many times should we loop before stopping? (0 for no stop)

void setup()   {
// initialize the digital pin as an input:
pinMode(redled, INPUT);

}

void loop(){

if (repeat <=10) { // Do we loop a finite number of times?
repeat--;
digitalWrite(redled,HIGH);
delay(50);
}
else
{
digitalWrite(redled, LOW);
delay(50);
}
}
``````

You can also reverse the logic and have repeat start at 0 and build up to 10 in the loop.

Hi thank you for the fast response!

It is still not working though, I copied your code and ran it but it doesn't stop after it has flashed 10 times?? What else can you suggest? Are there other ways of doing this? I've been looking at codes with "i" and "j" s but it is just too abstract for me to understand.

This:

``````if (repeat <=10) { // Do we loop a finite number of times?
repeat--;
digitalWrite(redled,HIGH);
delay(50);
}
else
{
digitalWrite(redled, LOW);
delay(50);
}
``````

should be:

``````if (repeat > 0) { // Do we loop a finite number of times?
repeat--;
digitalWrite(redled,HIGH);
delay(50);
digitalWrite(redled, LOW);
delay(50);
}
``````

Since repeat starts at 10, and is decremented, we want the led to come on for 50 milliseconds and then go off for 50 milliseconds only while repeat is positive. As soon as repeat goes negative, quit.

sorry there was a simple error i overlooked. The pinMode() should be output not input. that was the main problem.

``````int redled = 13;

int repeat = 0;     // How many times should we loop before stopping? (0 for no stop)

void setup()   {
// initialize the digital pin as an input:
pinMode(redled, OUTPUT);

}

void loop(){

if (repeat <=10)
{ // Do we loop a finite number of times?
repeat++;
digitalWrite(redled, HIGH);
delay(500);
digitalWrite(redled, LOW);
delay(500);
}

}
``````

Its always the little things.

Thanks Paul that's it working!

I just have a quick question to help me understand this procedure:

So when i put " int repeat = 10 " , am i only limited to using the text 'repeat' or can write anything there? Is it the layout of the code below what controls the loop?

if (repeat > 0) { // Do we loop a finite number of times? repeat--;

I am just wondering if you could have multiple loops, looping different no of times like:

int repeat = 10; int cycle = 20;

Thanks Digimike, that code is working too. What is the significance of

repeat++; repeat--;

Is this counting up and counting down?

Anything can be used in place of repeat. Well just about anything.

The repeat++ takes the value of repeat and adds 1 and stores the new value back to repeat. Likewise repeat-- subtracts one.

You could also write it like this.

repeat=1+repeat
or
repeat=1-repeat

Ok Ok, I like this. I was just playing around with the looping and decided to add another loop 1 second after the first one had stopped… I thought a delay would do, but it combined the loops together and added a second inbetween every two flashes.

``````if (repeat > 0) { // Do we loop a finite number of times?
repeat--;
digitalWrite(redled,HIGH);
delay(500);
digitalWrite(redled, LOW);
delay(500);
}

delay(1000);

if (repeat > 0) { // Do we loop a finite number of times?
repeat--;
digitalWrite(redled,HIGH);
delay(500);
digitalWrite(redled, LOW);
delay(500);
}
}
``````

I tried changing the repeat to cycle but i got the same result… any thoughts??

Sorry for the harassment! You don’t need to reply, you have done enough already!

The loop function is called in an infinite loop. What we were helping you do was to make loop do something each of the first 10 times it was called.

The changes you’ve made change what it does each time it is called.

If you want to have it do one thing (or set of things) the first 10 times it is called, and do something else (or set of things) the next n times it is called, you can do this:

``````int firstCount = 10;
int secondCoun t = 33;

void loop()
{
if(firstCount > 0)
{
// Do the first thing(s)
firstCount--;
}

if(secondCount > 0 && firstCount <= 0)
{
// Do the second thing(s)
secondCount--;
}
}
``````

Also note that there is a difference (in C) between saying "repeat++;" and "++repeat;"...

Used alone, they mean (virtually) the same thing, but used this way:

``````if (repeat++ > 10) { // do something
``````

vs

``````if (++repeat > 10) { // do something
``````

will give you slightly different results (and may even lead to some hair pulling when used in a for() loop!) - I leave figuring out what those results are as an exercise for the reader...

(hint: this is called the increment operator in C)

;)

In that case you may want to do this as a for() statement rather then if(). In this case the first for() statement will run till r=9. Keep in mind the count starts at 0 not 1 so it will flash 10 times. Once that is complete then it will move on to the next for() statement. After that it starts right back at the top again.

``````int redled = 13;

int repeat = 0;     // How many times should we loop before stopping? (0 for no stop)

void setup()   {
// initialize the digital pin as an input:
pinMode(redled, OUTPUT);

}

void loop(){

for(int r=0; r<10; r++)
{
if(r<10)
{
digitalWrite(redled, HIGH);
delay(500);
digitalWrite(redled, LOW);
delay(500);
}
}
delay(1000);
for(int s=0; s<10; s++)
{
if(s<10)
{
digitalWrite(redled, HIGH);
delay(100);
digitalWrite(redled, LOW);
delay(100);
}
}
delay(1000);
}
``````

I realize that this is a typo but just to keep novices from being confused repeat-- is the same as repeat = repeat - 1, which is not the same as repeat = 1-repeat

The repeat++ takes the value of repeat and adds 1 and stores the new value back to repeat. Likewise repeat-- subtracts one.

You could also write it like this.

repeat=1+repeat or repeat=1-repeat

Wow guys this is all great! Thank you very much! I’m going to have a wee play!

@digimike:

``````for(int r=0; r<10; r++)
{
if(r<10)
``````

Why bother?

what does the " r " and " s " refer to your code digimike? Can these letters be anything?

int repeat = 0; // How many times should we loop before stopping? (0 for no stop)

Also I thought this line above would mean you could make the loops stop after x no. of times? As i tried it and it didn't work? Do i need to add in something totally different to achieve that?

``````int repeat = 0;
``````

Simply declares a variable called "repeat" and initialises it to the value zero. You can only use it loop zero times if you use it in a comparison against some other variable that is actually keeping track of the number of times you have looped.

if you use it in a comparison against some other variable that is actually keeping track of the number of times you have looped.

How would i begin to achieve this if it is with the same variable? Or does that not work?? I have been trying with the LEDcolorMixerWith3Pots example and i am now thoroughly confused! Can you point me in the right direction please?

goal: To make it stop after it has looped x amount of times? (Just using an LED and giving it patterns with different loops)

The simplest way of making something stop after looping ‘n’ times is with a “for” loop in “setup()”, and nothing at all in “loop ()”:

``````const int loopTimes  = 10;

void setup ()
{
// setup pinModes and stuff here:

for (int i = 0; i < loopTimes; ++i) {
//whatever you want to do in here
}
}

void loop ()
{
}
``````

So. Using the example digimike gave, as i like this one and seem to understand it, i should be able to make it complete the loops 10 times all within setup?!

``````int redled = 13;

int repeat = 3;     // How many times should we loop before stopping? (0 for no stop)

const int loopTimes  = 10;

void setup()   {
// initialize the digital pin as an input:
pinMode(redled, OUTPUT);

for (int i = 0; i< loopTimes; ++1){
for(int r=0; r<10; r++)
{
if(r<15)
{
digitalWrite(redled, HIGH);
delay(50);
digitalWrite(redled, LOW);
delay(50);
}
}
delay(3000);
for(int s=0; s<10; s++)
{
if(s<10)
{
digitalWrite(redled, HIGH);
delay(100);
digitalWrite(redled, LOW);
delay(100);
}
}
delay(1000);

}
}

void loop(){

}
``````

" I get this error: Ivalue required as increment operand "

I can’t get it to work and I don’t know how to correct it to test what i want! Would you not have to call it in void loop to activate the loop?!

Any help / advice you could share with me with looping loops?! I’m sorry if this is all very silly!