C programming question

Hi, I'm a beginner to C programming.
From what i've read, "void setup" is for code I want to execute just once at the beginning. And "void loop* is for main code that gets executed repeatedly.

I want to initialize a motor and leadscrew to a home position once. Then execute my main code repeatedly.
When I put the initialize code in "void setup" I get an error message to the effect that I cant do that.

The code is good because when I put it at the top my "void loop" code it works fine, but repeatedly that I dont want it to do.

So my questions are:

  1. does any coding get done in "void setup"?
  2. how can I make code in "void loop" only execute once the first time through?

Thanks,
Pete

Please show your code; place it between [code] and [/code].
Please show the errors that your get; "to the effect that I cant do that" does not mean anything.

Yes, you can put plenty of code in setup().
2)
You can keep a flag (e.g. called firstRun)
Check in loop() if the flag is set or not and use appropriate action.
Change the flag afterwards

//Edit
Note: a sketch needs a setup() and a loop() function; either of them can be empty. If you omit one of them, the linker will complain.

See Arduino Sketch Structure

I'll figure out how to attach my code and error message

Please follow the advice on posting a programming question given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

One general concept mentioned is to use a boolean flag that is initialized to "false". Execute all the code you need to based on that flag being false, set it true and it won't run again until the next reset. For example:

void setup( void )
{
    
}//setup

void loop( void )
{
    static bool
        bLeadscrewHomed = false;

    if( !bLeadScrewHomed )
    {
        HomeLeadScrew();
        bLeadScrewHomed = true;
        
    }//if
    else
    {
        //do everything else
        .
        .
        .
        
    }//else
    
}//loop

A very good way to do something "at the start of loop(), but only once" is to put it at the end of setup():

void setup( void )
{


    HomeLeadScrew();
}//setup


void loop( void )
{
    //do everything else
    .
    .
    .
}//loop

The difference is - supposing you want to restart your sequence without resetting the Arduino. Then you can't use code in setup(). This is the case where you would use a flag inside loop().

aarg:
The difference is - supposing you want to restart your sequence without resetting the Arduino. Then you can’t use code in setup(). This is the case where you would use a flag inside loop().

You could also use this inside loop() to re-home at any time:

  if (TimeToRehome)
  {
    HomeLeadScrew();
    return;  // Exit from loop() to re-start loop()
  }

petecap:
Hi, I'm a beginner to C programming.
From what i've read, "void setup" is for code I want to execute just once at the beginning. And "void loop* is for main code that gets executed repeatedly.

I want to initialize a motor and leadscrew to a home position once. Then execute my main code repeatedly.
When I put the initialize code in "void setup" I get an error message to the effect that I cant do that.

Then show us the full error - its likely not for the reason you think - there is nothing at all special about
setup() or loop(), they are both just regular C++ functions, they just happen to be called in the pattern you
describe from the main() function in main.cpp in the Arduino runtime.

The code is good because when I put it at the top my "void loop" code it works fine, but repeatedly that I dont want it to do.

The error message will explain what the mistake is. That's what we need to see to figure anything out.
Be assured you will be able to run your initialation code inside setup(), its what its for.

There is no closing bracket for the loop() function.

Also there is nothing to make this false again after setting true:

initializeElevator = true;

That forces the code to run only once.

The sketch also doesn’t declare ‘stepCount’, ‘myStepper’, or ‘bottomSwitch’.

petecap:
I only posted the part that is not working.

Why does nobody ever Read this before posting a programming question?

By not posting your whole sketch you have wasted some time. First by well-meaning individuals trying to figure out what is wrong in your code, then you by having to explain that you only posted "relevant" code.

All to often the error is NOT where you think it is, which is why we ask for the whole sketch.