Need Help With goto Function

Hello!
I Am New To Arduino and have a question.
I am doing a morse code project and need to know how to use the goto function (I'm A Disgrace) to go to a second loop, and keep on getting this error : "In function 'void setup()':
error: 'sos' was not declared in this scope At global scope:"
Thanks!
-Ishtar.

First off - you shouldn't use goto, ever (unless you really understand what you are doing - and even then, you shouldn't need it). Restructure your code to avoid it. It really isn't needed.

If you are at a loss as to how to restructure your code to avoid using it, then perhaps you should post your code (use the # button!)...

:slight_smile:

If you don't mind cheating see reply #8 at:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1274407868/1

Cr0sh, Thus The "(I'm A Disgrace)" Part.

Here's My Code, I Used The Delay Function To Use "Dots" and "Dashes"
I Just Need A Way To Shorten My Code So I Don't Get A Full Flash Drive To Take To My Beginners Workshop.
Note That I Have Only Had This For Less Than A Week So I'm New.
This Code Will Have Multiple Errors, So Beware.

int ledPin12 =  12;
int ledPin13 =  13;// LED connected to digital pin 13

// The setup() method runs once, when the sketch starts

void setup()   {                
  // initialize the digital pin as an output:
  pinMode(ledPin12, OUTPUT); 
  pinMode(ledPin13, OUTPUT);   
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()  
sos: 
{
  digitalWrite(ledPin12, HIGH);   // set the LED on
  delay(500);                  // wait for a second
  digitalWrite(ledPin12, LOW);   // set the LED on
  delay(500);                  // wait for a second
  digitalWrite(ledPin12, HIGH);   // set the LED on
  delay(500);                  // wait for a second
  digitalWrite(ledPin12, LOW);   // set the LED on
  delay(500);                  // wait for a second
  digitalWrite(ledPin12, HIGH);   // set the LED on
  delay(500);                  // wait for a second
  digitalWrite(ledPin12, LOW);   // set the LED on
  delay(500);                  // wait for a second
  digitalWrite(ledPin12, HIGH);   // set the LED on
  delay(1000);                  // wait for a second
  digitalWrite(ledPin12, LOW);   // set the LED on
  delay(1000);                  // wait for a second
  digitalWrite(ledPin12, HIGH);   // set the LED on
  delay(1000);                  // wait for a second
  digitalWrite(ledPin12, LOW);   // set the LED on
  delay(1000);                  // wait for a second
  digitalWrite(ledPin12, HIGH);   // set the LED on
  delay(1000);                  // wait for a second
  digitalWrite(ledPin12, LOW);   // set the LED on
  delay(1000);                  // wait for a second
  digitalWrite(ledPin12, HIGH);   // set the LED on
  delay(500);                  // wait for a second
  digitalWrite(ledPin12, LOW);   // set the LED on
  delay(500);                  // wait for a second
  digitalWrite(ledPin12, HIGH);   // set the LED on
  delay(500);                  // wait for a second
  digitalWrite(ledPin12, LOW);   // set the LED on
  delay(500);                  // wait for a second
  digitalWrite(ledPin12, HIGH);   // set the LED on
  delay(500);                  // wait for a second
  digitalWrite(ledPin12, LOW);   // set the LED on
  delay(500);                  // wait for a second
  {if(ledPin12, HIGH == 18) { goto help;} 
}

void loop()
help:
{}
if(ledPin12 > 15

You set it to 12; how is it ever going to be 16 or more?

Can you explain what you want this code to do?

This:

if (sos(0) == 2) { goto: help;

makes no sense at all to me.

I Labeled My SOS Morse Code Loop Section As sos: and wanted to break that loop and go to a morse code that would spell out "help" after using the SOS line two times.

I'm not sure why that was there AWOL, that needs to be deleted.
Like I Said, I've Never Programmed Before So I'm Terrible Right Now And Need Watered Down Versions Of Stuff.

Just slow down and explain simply what you want to happen.

Ishtar,

these days, goto is a very advanced instruction used properly only in a few special cases, which beginners won't encounter for a long time. In nearly all cases where beginners use goto - and also your case here - it's the worst possible solution and using loops is better.

In your case, I advise to forget about goto until you mastered all the rest of C++.

Korman

Kormans Right.

@AWOL
What I Want To Do Is Have Two Loops, That Would Run A First Loop For A Certain Amount Of Times And Then Have It Go To A Second Loop For A Certain Amount Of Times And Have It Repeat That Process.
I Just Want To Know How To Do That, And On First Look, goto Looked Appropriate For The Situation

It looks to me like you just need two loops, one after the other. Unless I've missed something crucial, I can't see a need for a goto.

(What's wrong with your shift key?)

Ishtar - take a look at:

http://www.arduino.cc/en/Reference/For
http://arduino.cc/en/Reference/While

As Korman noted - "goto" is something which, if used incorrectly can lead to all sorts of havoc (ok, maybe not much on a microcontroller - but on a larger machine - eeesh!) - mainly to do with memory leakage and such caused by stack/heap issues...

Anyhow - ultimately, as also noted "goto" is only used in a very small, rare number of situations which you are likely not to encounter on small microcontrollers like the Arduino is based on. Generally, if you find yourself in those situations (deeply nested if-then constructs, for instance), refactoring of the code may be a better use of your time (unless you have a deadline, no other choice, and know exactly what you are doing).

The best thing to do is just pretend "goto" doesn't exist...

:slight_smile:

Ahhh...
So Now Do I Have To Make A Variable Up In Setup?

Probably Not. What Do You Think You Need A Variable For? What Value Would It Hold? Where Would It Be Used For?

PS. How Do You Manage To Capitalize Every Word? That Is The Strangest Thing To Do.

Sorry, I'm New And Stupid.

It Just Looks More Professional And Neater Than Others Writing :stuck_out_tongue:

Not so much!

PaulS does some of the best writing in his responses within the Arduino forums. If you care to take a look at some of his other post you'll find something worth aspiring to that looks more professional.

I wouldn't worry too much about using goto in code. It works exactly as designed. Computers have absoluely no problem with it. The issue is with people. It totaly confuses some people who have trouble following code logic flow. Posting code in this forum with goto in it is really alot of fun. Kind of like tossing a rock on the tin roof of a hen house and listening to all the old hens flapping about and cackling something about the sky is falling. Also, saying goto causes memory leaks is like saying baseballs cause constipation. Anything used inappropriately can cause issues. So what was the origional question????

(Note to self: lay off the baseballs at lunchtime)

Old school programmers consider goto in a 'proper' (as in not BASIC) language to be heresy. I'm with zoomcat, posting stuff about goto on here is akin to posting stuff in favour of Arduino on hackaday........

Replace the phrase "Old school programmers" with the word "experienced"!

experienced consider goto in a 'proper' (as in not BASIC) language to be heresy.

Who is this 'experienced' ? I don't get it :wink:

I've used the goto, and it did not cause my Arduino to explode!