Loop in a Function?

I am stuck on how to simply loop code inside of a Function.

I am trying to:

void onError(){
   void loop {
      digitalWrite(LED_R, LOW);
      delay(1000);  
      digitalWrite(LED_R, HIGH);
      delay(1000);
   }
}

I know this does not work, But I need a simple way to accomplish the same result.
In a large sketch, all I want to do is blink an LED if an error is present.

In this case, if an SD Card is not in the reader, my code knows and calls onError. I can light the LED, but I can't get it to blink.

void onError(){
   while(1) {
      digitalWrite(LED_R, LOW);
      delay(1000);  
      digitalWrite(LED_R, HIGH);
      delay(1000);
   }
}

When you say,

   void loop {
..
   }

You are declaring, i.e. defining, a function, not calling it.
You can not declare a function inside another function.

The predefined 'loop()' function only repeats because the core software calls it repeatedly from inside a 'while' statement block. It is not part of the C/C++ language.

1 Like

Thanks!

I tried

 while(i<10) {
      digitalWrite(LED_R, LOW);
      delay(1000);  
      digitalWrite(LED_R, HIGH);
      delay(1000);
      i++;

and it kept telling me I had n error.

But yours worked, again, thank you!

All's well that ends well, but if you have errors, please cut and paste them verbatim, rather than just mentioning or paraphrasing them. If you wanted to do the loop10 times you could do that too, but your example is chopped off so I can't see what you did.

I would venture to guess, you didn't declare the variable 'i'.

void onError(){
int i = 0;
while(i<10) {
      digitalWrite(LED_R, LOW);
      delay(1000);  
      digitalWrite(LED_R, HIGH);
      delay(1000);
      i++;
  }
}
1 Like

No you did exactly what I was trying to accomplish, thank you. I am new to this language and tried a dozen different options. I didn’t past all those errors because I didn’t know what was even the right path.

I knew my example was incorrect, but it visually represented what I was trying to do.

you can use one of the iteration statements

if you want an infinite loop:

while (true) {
  // your repeating code goes here
}

or

for (;;) {
  // your repeating code goes here
}

if you want to repeat just a certain number of time, the for loop makes it easy

for (size_t n = 0;  n < 100; n++) {
  // your repeating code goes here
  // n will vary from 0 to 99 (so the loop is executed 100 times)
}

size_t is a type that is suitable for counting or array indexes etc. It's unsigned so if you need to variable to go from -42 to 58 then use a different type like int or long

for (int n = -42;  n < 58; n++) {
  // your repeating code goes here
  // n will vary from -42 to 57 (so the loop is executed 100 times)
}

if you need a blinking led, Arduino has prepared the example "Blink Without Delay"

You can even find that example in your IDE examples.

My understanding was that he needed the code to be stuck in the blinking loop. (Sure you could use that as well but the basic blink would do)

Why not --
while(true) {

1 Like

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