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.
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.
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++;
}
}
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.
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)
}