I am trying to understand Arduino programming

Hi all I am trying to learn to program Arduino , I don't understand how to create a label that i can go to in a program, as i would have done in Microchip ASM code .
I would do something like this:
Program data
Main program
1: program
2; go to chkflg
3: more program
20: chkflg
21: do what i require, but without a return .
22 continue with program
If someone could explain in a similar way, so I understand, I tried void chkflg (), but that did not work, also tried int chkflg. but get errors.
I have followed some you tube video's but still cant get to grips with this. Thanks

You should not have posted in this forum section. There are clear directions about not posting in Uncategorised. I will move your post. Please take more care where you post in future. Each forum section has a sticky post explaining what the section is about, so please read those.

There is a goto label; statement in C/C++. However, using it is seriously frowned upon except under very rare circumstances. As a beginner in C/C++, you should never use goto.

Instead, structure your code using if, else, for, while statements, and by declaring functions, especially for any repeated code.

I would use your example pseudocode as a C/C++ example, but I don't understand it because of this part:

How would this part of the code ever get executed? It appears to get unconditionally jumped over.

1 Like

Forget how you did it in ASM. I made a similar move from PIC ASM to C. Trying to translate from how I did things in ASM to how C works made life more difficult than necessary.

Your ASM code will have been structured in whatever way you devised for it. C/C++ has its own way of doing things that's probably different to what you did. Look at the examples in the Arduino IDE and in the tutorials on here to see how code can be structured into functions. I completely agree with @PaulRB , using labels like you are used to is not a good idea and will cause you problems.

1 Like

Maybe a bit more context would help here. Have you worked through any of the built in examples from the Arduino IDE? Do any of them approximate your project goal?

What is your project goal?

void setup()
{
  Serial.begin(9600);
BEGIN:
  // my code
  goto BEGIN;
}

void loop()
{}
1 Like

the Arduino supports a file containing at least two functions, setup() and loop(). They are both called from a main() that the user doesn't need to worry about.

setup() is invoked once to perform initialization and loop() is called repeatedly.

without an OS, code typically "polls" for changes at its interfaces but can also use interrupts when higher performance is reqiured

the Arduino Reference provide a list of common utilites. Serial I/O functions support user output and input thru IDE the serial monitor

you might be interested in online hands-on Arduino programming which demonstrates Arduino programming using the online wokwi.com simulator

1 Like

Here is a Demo-Code as a WOKWI-Simulation that shows the basic principle

In ASM you defined a label to jump to
In C++ you define a sub-program which is called a function.
The exact term is declaring a function.

Two things to do

  1. declaring the function
  2. calling the function

outside loop() you declare the function

In inside loop() you call the function

Serial printing of the code looks like that

Setup-Start
top of loop() executing 1: program
next thing entering function chkflg
entering function chkflg which is 2; go to chkflg
c1
executing 2 chkflg
c2
exiting function with name chkflg
going on after returning from function chkflg
3: more program
bottom of loop()

top of loop() executing 1: program
next thing entering function chkflg
entering function chkflg which is 2; go to chkflg
c1
executing 2 chkflg
c2
exiting function with name chkflg
going on after returning from function chkflg
3: more program
bottom of loop()

Here is the code

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  // additional initialisation-code here
}

/*
1: program
2; go to chkflg
3: more program
20: chkflg
*/
void loop() { // 
  Serial.println("top of loop() executing 1: program");
  Serial.println("next thing entering function chkflg");
  chkflg(); // CALL of function with name "chkflg"
  Serial.println("going on after returning from function chkflg");
  Serial.println("3: more program");
  delay(3000); // BLOCKING delay which freezes microcontroller for 3 seconds
  Serial.println("bottom of loop()");
  Serial.println();
}


// declaring of the function 
void chkflg() {
  Serial.println("entering function chkflg which is 2; go to chkflg");
  Serial.println("c1");
  Serial.println("executing 2 chkflg");
  Serial.println("c2");
  Serial.println("exiting function with name chkflg");
}
1 Like

Thanks, Stefent 38 for the response, I will try to get my head around this, it's been many years since I programmed using PIC chips with microchip ASM code, and this new Arduino code is quite different. But it seems a lot easier to use, just a lot of syntax to remember.

You can use labels, no one will shoot you if you do.
see post #6

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