Go Down

Topic: How to repeat a loop only a certain number of times? (Read 3261 times) previous topic - next topic

mobman

Hello all,

I am a noob to Arduino.

I am trying to make a LED blink 10 times when I press a button then stop until I press the button again. Could someone assist me with how to write this in code?

I am currently using the loop() procedure with the delay function from the beginners page.

Button input is pin 2.
Led output is pin 13. 

Thank you in advance! :)

PaulS

Quote
I am trying to make a LED blink 10 times when I press a button then stop until I press the button again.

The for loop is just what you need.

#2
Jan 03, 2012, 12:11 am Last Edit: Jan 03, 2012, 12:16 am by techdutchy Reason: 1
Hi,

Working from the cookbook on page 38 there is a method called: void blink  2  
And then the count function.

Code: [Select]
// blink an LED the number of times given in the count parameter
void blink2(int count)
{
while(count > 0 )                                   // repeat until count is no longer greater than zero
{
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
count = count -1;                                  // decrement count
}
}


I am getting errors on this piece of simple code, so I think my guess is the count function isn't compatible with Arduino 1 right ?
Code: [Select]
core.a(main.cpp.o): In function `main':
\Arduino1\arduino-1.0\hardware\arduino\cores\arduino/main.cpp:11: undefined reference to `setup'
\Arduino1\arduino-1.0\hardware\arduino\cores\arduino/main.cpp:14: undefined reference to `loop


Is there somewhere a list where I can find compatible problems with Arduino 1.0 ? (can't seem to find it on the website)

Thank you very much,

Happy new year btw,

Patrick

PaulS

Quote
I am getting errors on this piece of simple code, so I think my guess is the count function isn't compatible with Arduino 1 right

The function is called blink2, and it takes one argument. The argument is stored in a variable called count.

There is nothing in that function that is not compatible with the Arduino 1.0 IDE. Look at the compiler messages again.

Where is your setup() function? Where is your loop() function?

James C4S


I am getting errors on this piece of simple code, so I think my guess is the count function isn't compatible with Arduino 1 right ?


That would be a completely wrong guess.

Is that the entire code you are trying to compile?  If not, please post the entire thing.  If so, the compiler is quite obviously telling you that there is no void setup() or void loop() ...
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

mobman

I am still getting an error on the following code:

Code: [Select]

void setup() {               

  pinMode(13, OUTPUT);     
}

void blink2 (int count)
{
   while (count > 0)
   
   {
void loop ()
{
   digitalWrite(13, HIGH);   // set the LED on
   delay(13);              // wait for a second
   digitalWrite(13, LOW);    // set the LED off
   delay(13);              // wait for a second
   count= count -1;

   }
}


Am I missing something?

mobman


The following code compiles however the loop does not stop at 10 :(

Code: [Select]

void setup(){
pinMode(13,OUTPUT);
}

void loop()
{
for (int x = 0; x < 10; x++) {
   digitalWrite(13, HIGH);   // set the LED on
   delay(13);              // wait for a second
   digitalWrite(13, LOW);    // set the LED off
   delay(13);              // wa
}
}

lloyddean

Code: [Select]

bool    fHasLooped  = false;

void setup()
{
   pinMode(13, OUTPUT);
}

void loop()
{
   if ( fHasLooped == false )
   {
       for ( int x = 0; x < 10; x++ )
       {
           digitalWrite(13, HIGH);
           delay(13);
           digitalWrite(13, LOW);
           delay(13);
       }

       fHasLooped = true;
   }
}


WizenedEE



The following code compiles however the loop does not stop at 10 :(

Code: [Select]

void setup(){
pinMode(13,OUTPUT);
}

void loop()
{
for (int x = 0; x < 10; x++) {
   digitalWrite(13, HIGH);   // set the LED on
   delay(13);              // wait for a second
   digitalWrite(13, LOW);    // set the LED off
   delay(13);              // wa
}
}



Remember that the loop() function is called again and again. Your own for loop will end after ten times, but then it will start over at the beginning again. Setup only runs once, so you can put the loop in there.


Shoot I feel so frustrated.

Still cant get this code to work.

Code: [Select]
void setup() {               

  pinMode(13, OUTPUT);     
}




void loop()
// blink an LED the number of times given in the count parameter
{
void blink2(int count)

while(count > 0 )                                   // repeat until count is no longer greater than zero
{
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
count = count -1;                                  // decrement count
}
}


I smuggled in a set up() and a loopvoid () Like you can see in the copied code, but then it is whining about:
sketch_jan03f.cpp: In function 'void loop()':
sketch_jan03f:15: error: expected initializer before 'while'
I don't know what initializer is can't find a single thing about it in the reference guide.

Placed a million brackets on a billion places, swapped the void blink, placed it everywhere I could, before setup, in the setup in the place of void loop, sigh nothing works.


The code of lloyddean works, so thank you for that very much!   :smiley-mr-green:

But I still have restless sleeps over why the frigging code from the Arduino cookbook doesn't work. (page 38)
And thx to the succes of mobman, I never felt so dumb before :)

Great job mobman btw,

And in other cases I get a count is not declared yet, which make perfect sense if you ask me. But how to declare it ?

Maybe I should just give it a rest. This piece of code.

Thanks all for your previous help!







AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

lloyddean

Since I don't have access to the book in question I'll improvise with -

Code: [Select]

void blink2(int count)
{
    while ( count-- )
    {
        digitalWrite(13, HIGH);
        delay(500);

        digitalWrite(13, LOW);
        delay(500);
    }
}

void loop()
{
}

void setup()
{
    pinMode(13, OUTPUT);
   
    blink2(10);
}


One of the things to note is that the Arduino starts by running your code in 'setup'.  Once 'setup' is done 'loop' runs thru whatever code is present until it runs out of code and then exits and then is called again over and over and over and ...

mobman

lloyddean - this code works even better    XD

If this is improvising to you I dont even know what you can cook up when you are getting serious  :smiley-eek-blue:

Techone

I am sorry to interrupt gentlements... when I program a code, what ever is in C++, Arduino, Processing, Java,  Java ME, Pascal, C, BASIC, Assembly... I always do a "standard" of coding ( structures wise ) for each of the language that I am learning. In that way, it is less confusing.

I notice this :

Code: [Select]


void blink2()
{

}

void loop()
{

}

void setup()
{

}



My "standard" is :

Code: [Select]

Libraries to include
my variables

void setup()
{
// my setup code
}

void loop()
{
// my main program who is looping, looping, looping...
}

void my function_one()
{
// my function  one
}

void function_two()
{
// my function two
}

// the rest of the functions goes under this line


Go Up