user38250: I am trying to create an if statement that only runs once, the very first time the void loop is started and switches to another code after that. But the following only executes stepper1a(). Any help would be great. Thx !
int var;
int var2;
bool executed = false;

const int M1dirpin = 4; //Stepper Dir Pin
const int M1steppin = 5; //Stepper Step Pin

void setup() {

pinMode(A2,INPUT);
pinMode(M1dirpin,OUTPUT);
pinMode(M1steppin,OUTPUT);
Serial.begin(9600);

}

void stepper1() { 
  int j;
    delayMicroseconds(10);
    digitalWrite(M1dirpin,HIGH);
  for(j=0;j<=200;j++){
    digitalWrite(M1steppin,LOW);
    delayMicroseconds(10);
    digitalWrite(M1steppin,HIGH);
    delay(2);
  }
}

void stepper1a() {
  int j;
    delayMicroseconds(10);
    digitalWrite(M1dirpin,HIGH);
  for(j=0;j<=1600;j++){
    digitalWrite(M1steppin,LOW);
    delayMicroseconds(10);
    digitalWrite(M1steppin,HIGH);
    delay(2);
  }
}

void loop() {

if (executed == false) {
var = analogRead(A2);
delay(10);
if (var > 500) {
  stepper1a();
  delay(2000);
  executed = true;
  Serial.println("turned 1600");
  Serial.println(executed);
  }
else if (executed == true) {
 var2 = analogRead(A2);
delay(10);
if (var > 500) {
  stepper1();
  delay(2000);
  Serial.println("turned 200");
}
}
}
}

Updated void loop:
void loop() {

    static bool yes_we_did_the_one_time_job = false;

    if (yes_we_did_the_one_time_job) {
        var = analogRead(A2);
        delay(10);
        if (var > 500) {
            stepper2();
            delay(2000);
            Serial.println("turned 1600");
        } 
    }
    else {
        var2 = analogRead(A2);
        delay(10);
        if (var > 500) {
            stepper1();
            delay(2000);
            Serial.println("turned 200");
            yes_we_did_the_one_time_job = true;
        }
    }
}

This works now, thx !!!: 
void loop() {
  static bool yeswedid = false;

  if(yeswedid) {
    if(analogRead(A2) > 500) {
    stepper1();
    stepper2();
    }
  }
  else {
    if(analogRead(A2) > 500) {
    stepper1();
    yeswedid = true;
  }

  }
}


user7508: Generic solution:
void loop() {
    static bool yes_we_did_the_one_time_job = false;
    if (yes_we_did_the_one_time_job) {
        do_the_other_jobs();
    } else {
        do_the_one_time_job();
        yes_we_did_the_one_time_job = true;
    }
}


user34529: If you have any code you want to run only once at the beggining of your program you can use the setup() method. It can actually do much more than just setting up pins and default values.
The code of main function of Arduino (main.cpp) looks like this:
int main(void)
{
    //(...) I cut some stuff from here

    setup();

    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }

    return 0;
}

As you can see setup() is just a method and it can do almost exactly the same things as loop().
The setup() method can actually contain a for(;;) loop that will stop the program from running the loop() method. From inside it you can check the sensor value and break from it when condition is met.
void setup()
{
  for (;;)
  {
    var = analogRead(A2);
    delay(10);
    if (var > 500) {
      stepper1();
      delay(2000);
      Serial.println("turned 200");
      break;
    }
  }
}

in this case the loop() is very simple:
void loop()
{
  var = analogRead(A2);
  delay(10);
  if (var > 500)
  {
    stepper2();
    delay(2000);
    Serial.println("turned 1600");
  }
}


38250: <p>I am trying to create an if statement that only runs once, the very first time the void loop is started and switches to another code after that. But the following only executes stepper1a(). Any help would be great. Thx !</p>



<pre class="lang-c++ prettyprint-override"><code>int var;
int var2;
bool executed = false;

const int M1dirpin = 4; //Stepper Dir Pin
const int M1steppin = 5; //Stepper Step Pin

void setup() {

pinMode(A2,INPUT);
pinMode(M1dirpin,OUTPUT);
pinMode(M1steppin,OUTPUT);
Serial.begin(9600);

}

void stepper1() { 
  int j;
    delayMicroseconds(10);
    digitalWrite(M1dirpin,HIGH);
  for(j=0;j&lt;=200;j++){
    digitalWrite(M1steppin,LOW);
    delayMicroseconds(10);
    digitalWrite(M1steppin,HIGH);
    delay(2);
  }
}

void stepper1a() {
  int j;
    delayMicroseconds(10);
    digitalWrite(M1dirpin,HIGH);
  for(j=0;j&lt;=1600;j++){
    digitalWrite(M1steppin,LOW);
    delayMicroseconds(10);
    digitalWrite(M1steppin,HIGH);
    delay(2);
  }
}

void loop() {

if (executed == false) {
var = analogRead(A2);
delay(10);
if (var &gt; 500) {
  stepper1a();
  delay(2000);
  executed = true;
  Serial.println("turned 1600");
  Serial.println(executed);
  }
else if (executed == true) {
 var2 = analogRead(A2);
delay(10);
if (var &gt; 500) {
  stepper1();
  delay(2000);
  Serial.println("turned 200");
}
}
}
}
</code></pre>

<p>Updated void loop:</p>

<pre class="lang-c++ prettyprint-override"><code>void loop() {

    static bool yes_we_did_the_one_time_job = false;

    if (yes_we_did_the_one_time_job) {
        var = analogRead(A2);
        delay(10);
        if (var &gt; 500) {
            stepper2();
            delay(2000);
            Serial.println("turned 1600");
        } 
    }
    else {
        var2 = analogRead(A2);
        delay(10);
        if (var &gt; 500) {
            stepper1();
            delay(2000);
            Serial.println("turned 200");
            yes_we_did_the_one_time_job = true;
        }
    }
}
</code></pre>

<p>This works now, thx !!!: </p>

<pre class="lang-c++ prettyprint-override"><code>void loop() {
  static bool yeswedid = false;

  if(yeswedid) {
    if(analogRead(A2) &gt; 500) {
    stepper1();
    stepper2();
    }
  }
  else {
    if(analogRead(A2) &gt; 500) {
    stepper1();
    yeswedid = true;
  }

  }
}
</code></pre>
 7508: <p>Generic solution:</p>

<pre class="lang-cpp prettyprint-override"><code>void loop() {
    static bool yes_we_did_the_one_time_job = false;
    if (yes_we_did_the_one_time_job) {
        do_the_other_jobs();
    } else {
        do_the_one_time_job();
        yes_we_did_the_one_time_job = true;
    }
}
</code></pre>
 34529: <p>If you have any code you want to run only once at the beggining of your program you can use the <code>setup()</code> method. It can actually do much more than just setting up pins and default values.</p>

<p>The code of main function of Arduino (<a href="https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/main.cpp" rel="nofollow noreferrer">main.cpp</a>) looks like this:</p>

<pre><code>int main(void)
{
    //(...) I cut some stuff from here

    setup();

    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }

    return 0;
}
</code></pre>

<p>As you can see <code>setup()</code> is just a method and it can do almost exactly the same things as <code>loop()</code>.</p>

<p>The <code>setup()</code> method can actually contain a <code>for(;;)</code> loop that will stop the program from running the <code>loop()</code> method. From inside it you can check the sensor value and <code>break</code> from it when condition is met.</p>

<pre><code>void setup()
{
  for (;;)
  {
    var = analogRead(A2);
    delay(10);
    if (var &gt; 500) {
      stepper1();
      delay(2000);
      Serial.println("turned 200");
      break;
    }
  }
}
</code></pre>

<p>in this case the <code>loop()</code> is very simple:</p>

<pre><code>void loop()
{
  var = analogRead(A2);
  delay(10);
  if (var &gt; 500)
  {
    stepper2();
    delay(2000);
    Serial.println("turned 1600");
  }
}
</code></pre>
