Blink program, How to do "forward" GOTO ?

Greets,

I want to add

"if (loopcount > 9)

{goto exitprogram;}" to the Blink program.

See code below <goto exitprogram;>.

But compiler doesn’t seem to like forward references.

Is there a way to define a “label” in the beginning of program?

I know there are other ways to do this, other than “goto”,

but I specifically need to use a “goto”.

Thanks…vmars316

/*

Blink

Turns on an LED on for one second, then off for one second, repeatedly.

This example code is in the public domain.

*/

void setup() {

// initialize the digital pin as an output.

// Pin 13 has an LED connected on most Arduino boards:

pinMode(13, OUTPUT);

}

void loop()

{

int loopcount;

loopcount = loopcount + 1;

digitalWrite(13, HIGH); // set the LED on

delay(1000); // wait for a second

digitalWrite(13, LOW); // set the LED off

delay(1000); // wait for a second

if (loopcount > 9)

{goto exitprogram;}

}

exitprogram:

break; will exit the innermost loop. However, what you want to is halt the cpu.

I thought gotos were evil… (well, “considered harmful”)

I think you’re a troll.

But your problem is not that exitprogram is a forward reference, it’s that the label is not before a valid statement (or in a spot where you could have an executable statement.) Nor does the “bottom of the source” have anything to do with exiting the program. Nor does “exiting” have much meaning on a microcontroller.

void loop()
{
  int loopcount;
  loopcount = loopcount + 1;
  if (loopcount > 9)
    {goto exitprogram;}
  digitalWrite(13, HIGH); // set the LED on
  delay(1000); // wait for a second
  digitalWrite(13, LOW); // set the LED off
  delay(1000); // wait for a second
exitprogram:
  return;
}

should work fine, compilation and syntax-wise. It won’t actually exit, of course. It’ll just stop blinking.

westfw:
I think you’re a troll.

But your problem is not that exitprogram is a forward reference, it’s that the label is not before a valid statement (or in a spot where you could have an executable statement.) Nor does the “bottom of the source” have anything to do with exiting the program. Nor does “exiting” have much meaning on a microcontroller.

Wow!

That’s a lot of good information.

Thank you very much!

westfw:
I think you’re a troll.

Why, because I am using a GOTO ?

westfw:
Nor does “exiting” have much meaning on a microcontroller.

Could you elaborate on this a bit?

Thanks…Vernon

Actually, what I want to do, is this:

void loop()
{
  int loopcount;
  loopcount = loopcount + 1;
  if (loopcount > 9)
    {goto exitprogram;}
  digitalWrite(13, HIGH); // set the LED on
  delay(1000); // wait for a second
  digitalWrite(13, LOW); // set the LED off
  delay(1000); // wait for a second
}
exitprogram:
  loopcount + 1;

With exitprogram: label, outside the loop.

But I get the following errors:

Blink10Rev.cpp: In function ‘void loop()’:

Blink10Rev:18: error: label ‘exitprogram’ used but not defined

Blink10Rev.cpp: At global scope:

Blink10Rev:24: error: function definition does not declare parameters

How do I define or otherwise inform the program, that there is a forward label, namely, exitprogram: .

Also, how can I CLEAR the above (or any program) from arduino memory?

Or would I need to overlay resident program, with a blank program?

Or what is the minimallist arduino program known to mankind?

Thanks…Vernon

vmars316:
Actually, what I want to do, is this:

void loop()

{
int loopcount;
loopcount = loopcount + 1;
if (loopcount > 9)
{goto exitprogram;}
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
}
exitprogram:
loopcount + 1;

The “C” language standard simply does NOT allow a goto to jump out of a function. So you can’t do that.

What it appears you really want is to just blink the LED 9 times then stop. Stopping in a microcontroller really means just not doing something, you can’t actually exit.

So this this (I leave details out)

int loopcount = 0;

void loop()
{ 
  while( loopcount<9) {
    digitalWrite(13, HIGH); // set the LED on
    delay(1000); // wait for a second
    digitalWrite(13, LOW); // set the LED off
    delay(1000); // wait for a second
    loopcount++;
  }
  delay(some big number);
}

So the programs continues to run after the last blin but does nothing. loopcount will remain equal to 9 forever.

Oh, Ok, I get it now.

Also, how can I CLEAR the above (or any program) from arduino memory?

Or would I need to overlay resident program, with a blank program?

Or what is the ‘smallest arduino program’ known to mankind?

Thanks…vmars

// Ah, much better:

void loop()

{ // begin loop

int bigloop; int littleloop;

for (bigloop = 0; bigloop < 11; bigloop ++)

{ // begin bigloop

for (littleloop = 0; littleloop <= bigloop; littleloop ++)

{ // begin littleloop

digitalWrite(13, HIGH); // set the LED on

delay(250); // wait for .25 second

digitalWrite(13, LOW); // set the LED off

delay(250); // wait for .25 second

} // end littleloop

digitalWrite(13, LOW); // set the LED off

delay(1000); // wait for a second

} // end bigloop

} // void loop()

Thanks All…vmars