Why is my code not Working

I'm trying to write a programm to let a stepper motor drive to two destinations if my rotary encoder reached a certain value and if a button is pressed. So far everything works but when i try to tell my stepper to do a movement once and repeat this again it wont. It will only do the process once.

#include <Button.h>
#include <AccelStepper.h>
#include <Encoder.h>

AccelStepper stepper;

Button button1(A0);  // Connect your button between pin 2 and GND
int State = 0;

Encoder myEnc(A4, A5);

void setup() {
  button1.begin();

  stepper.setMaxSpeed(200);
  stepper.setAcceleration(200);

  while (!Serial) {};  // for Leos
  Serial.begin(9600);
}

long oldPosition = -999;

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }

  if(newPosition == 0) {
    digitalWrite(6, LOW);
    digitalWrite(7,LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);

// Spielerzahl 3 Motor dreht 120 Grad pro Spieler

    if(newPosition == 8) {
    digitalWrite(6, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);
    digitalWrite(13, LOW);
     
  if (button1.pressed()) {
    State = 1;
    Serial.print(State);
  }
  while (State == 1) {
    stepper.moveTo(802);
    stepper.run();

    if (stepper.distanceToGo() == 0) {
      stepper.setCurrentPosition(0);
      stepper.moveTo(802);
      stepper.run();
      State = 0;
      Serial.print(State);
  }
  }
  }

}

When i read my values in the Serial monitor it shows me state 0 after the first stepper movement but it just ignores the second move command.

Your second if clause is within your first one. You'll need to add a } before the second one, and remove one from the end of the file.

???
Infinite loop?

No, that doesn't fit with @erik3dprints 's description. So we can conclude @erik3dprints is not using an Arduino model with native USB.

Im using an elegoo with usb, but i dont know what you guys mean with infinte loop.

That didnt work out. What wonders me is that i get the serial.print (0) on my serial monitor but it just skips the other commands in the if statement.

But not an Arduino with native USB, I suspect.

It means that the Arduino's MCU chip does not need an extra USB-Serial chip, it can natively connect to USB without one. Leonardo and Pro Micro are examples, plus many of the more modern models.

You don't have the infinite loop problem @anon92864395 was hinting at, your description of the problem seems to prove that.

"Elegoo" is a make (a clone manufacturer), not a model, so that doesn't tell us much unfortunately.

Its the Uno R3 from Elegoo, sorry forgot to add that :sweat_smile:

Uno R3 does not have native USB.

So what does this mean now for me? Cant my Uno R3 run this code or how can i understand this?

No, it just means that @anon92864395 was not correct about the problem you are facing. It's something else.

(If you had been using a Leonardo, Pro-micro or a clone of those models, or any other model with native USB, then @anon92864395 may well have been correct about the infinite loop. But from your description of the problem, we know that wasn't it).

It would help if you could Auto-Format your code and re-post it. Correct indentation, you may have heard, does not affect how your sketch runs, it won't fix anything. But it sure makes it a hell of a lot easier for humans to spot an error in the code!

#include <Button.h>
#include <AccelStepper.h>
#include <Encoder.h>

AccelStepper stepper;

Button button1(A0);  // Connect your button between pin 2 and GND
int State = 0;

Encoder myEnc(A4, A5);

void setup() {
  button1.begin();

  stepper.setMaxSpeed(200);
  stepper.setAcceleration(200);

  while (!Serial) {};  // for Leos
  Serial.begin(9600);
}

long oldPosition = -999;

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }

  if (newPosition == 0) {
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);

    // Spielerzahl 3 Motor dreht 120 Grad pro Spieler

    if (newPosition == 8) {
      digitalWrite(6, HIGH);
      digitalWrite(8, HIGH);
      digitalWrite(9, HIGH);
      digitalWrite(11, HIGH);
      digitalWrite(12, HIGH);
      digitalWrite(13, LOW);

      if (button1.pressed()) {
        State = 1;
        Serial.print(State);
      }
      while (State == 1) {
        stepper.moveTo(802);
        stepper.run();

        if (stepper.distanceToGo() == 0) {
          stepper.setCurrentPosition(0);
          stepper.moveTo(802);
          stepper.run();
          State = 0;
          Serial.print(State);
        }
      }
    }
  }

Your code can only reach that second if-statement when newPosition is zero. If it's zero, it can't possibly be 8. So that must be a coding error.

This is what @camsysca was pointing out in post #2, but you don't seem to have fixed it.

Sorry that was my error after the first if statement it does get closed i forgot to add that because ive shorten the code for you guys to make it read easier.

DO. NOT. DO. THAT. You've got us debugging artificial code, with artificial bugs. That does not help your cause at all, at all.

I've added something to the code which is working fine, kind of tried to find a way around my problem but the actuall probelm still stays. Here is my full code now.

#include <Button.h>
#include <AccelStepper.h>
#include <Encoder.h>
#include <Servo.h>

Servo myservo;
int pos = 0;

AccelStepper stepper;

Button button1(A0);
int State = 0;

Encoder myEnc(A4, A5);

void setup() {
  myservo.attach(A1);

  button1.begin();

  stepper.setMaxSpeed(200);
  stepper.setAcceleration(200);

  while (!Serial) {};  // for Leos
  Serial.begin(9600);
}

long oldPosition = -999;

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }

  if(newPosition == 0) {
    digitalWrite(6, LOW);
    digitalWrite(7,LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);
  }

// Spielerzahl 2, Motor dreht 360 Grad

  if(newPosition == 4) {
    digitalWrite(12, HIGH);
    digitalWrite(13, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(8, HIGH);
     
  if (button1.pressed()) {
    State = 1;
    Serial.print(State);
  }
  while (State == 1) {
    stepper.moveTo(2048);
    stepper.run();

    if (stepper.distanceToGo() == 0) {
      stepper.setCurrentPosition(0);
      State = 0;
      Serial.print(State);
    }
  }
  }

// Spielerzahl 3 Motor dreht 120 Grad pro Spieler

    if(newPosition == 8) {
    digitalWrite(6, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);
    digitalWrite(13, LOW);
     
  if (button1.pressed()) {
    State = 1;
    Serial.print(State);
  }
  while (State == 1) {
    stepper.moveTo(802);
    stepper.run();

    if(stepper.distanceToGo() == 0) {
    myservo.write(180);
    delay(15);
    myservo.write(0);
    delay(1000);
    if(stepper.distanceToGo() == 0) {
    stepper.setCurrentPosition(0);
    stepper.moveTo(802);
    stepper.run();
    State = 0;
    Serial.print(State);
    
    }
    }

  }
  }
  

}


It still gives me the "0" from the serial print at the end of the last if statement but it still just ignores the move command right above.

Because the while loop ends with setting state=0. I think you missunderstand how the .run() command works. It creates at max 1 step per call. Therfore you must call .run() as often as possible. It doesn't matter if you call it too often.
Also the combination of these two statements:

doesn't make sense. The .moveTo command tells Accelstepper where to go, and after that you must only call .run() very often to create the necessary steps.

P.S. if you want your sketch to block while the stepper is moving, use .runToNewPosition(targetposition) and omit the .run().

Yes.

Thanks a lot that solved my problem!