Pages: [1]   Go Down
Author Topic: How to repeat a loop only a certain number of times?  (Read 2580 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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! smiley
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50166
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

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

Code:
// 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:
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
« Last Edit: January 02, 2012, 06:16:45 pm by techdutchy » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50166
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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() ...
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am still getting an error on the following code:

Code:
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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


The following code compiles however the loop does not stop at 10 smiley-sad

Code:
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
}
}
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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;
    }
}
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


The following code compiles however the loop does not stop at 10 smiley-sad

Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

YAY! Got it working! Thanks everyone!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Shoot I feel so frustrated.

Still cant get this code to work.

Code:
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 smiley

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!






Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26484
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try declaring blink2  outside of loop.
Logged

"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.

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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 ...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

lloyddean - this code works even better    smiley-lol

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

Toronto, Canada
Offline Offline
Edison Member
*
Karma: 2
Posts: 1234
"Keep it R.E.I.L. - "Research, Experiment, Investigate and Learn"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

void blink2()
{

}

void loop()
{

}

void setup()
{

}


My "standard" is :

Code:
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

Logged

Pages: [1]   Go Up
Jump to: