Arduino Nano runs code only after a couple seconds

I am trying to create a project where I control a couple of stepper motors with an Arduino nano. The code works fine and all but every time the Arduino is reset it takes like 5 seconds before it starts running the code properly, which is annoying because in those five seconds it just moves the motors randomly and messes everything up. When I run it on Arduino Uno it works just fine. Any ideas what could cause that?

Probably wrong setup, some undefined state or something. You have to ensure it in the setup.
Post the code.

#include <CapacitiveSensor.h>
CapacitiveSensor capSensor = CapacitiveSensor(5,3);
int state;
long sensorval;


void setup() {
  Serial.begin(9600);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(4, OUTPUT);
  state = 0;
}

void loop() {
  long sensorval = capSensor.capacitiveSensor(30);
  Serial.println(sensorval);
   if(state == 0) {
    Serial.print("1");
   digitalWrite(7, LOW);
   digitalWrite(6, LOW);
   digitalWrite(8, LOW);
   digitalWrite(9, LOW);
   digitalWrite(4, LOW);
   if(sensorval > 500) {
      state = 1;
    }
    else {
    }
  }
  else if (state == 1){
    Serial.print("2");
    digitalWrite(4, HIGH);
    digitalWrite(6, LOW);
    for(int i = 0; i <200; i++) {
      digitalWrite(7, HIGH);
      delayMicroseconds(2000);
      digitalWrite(7, LOW);
      delayMicroseconds(2000);
    }
    Serial.print("3");
    delay(5000);
    Serial.print("4");
    digitalWrite(8, LOW);
    for(int i = 0; i <200; i++) {
      digitalWrite(9, HIGH);
      delayMicroseconds(2000);
      digitalWrite(9, LOW);
      delayMicroseconds(2000);
    }
    delay(5000);
    state = 2;
  }
  else if (state == 2) {
    Serial.print("5");
    digitalWrite(7, LOW);
   digitalWrite(6, LOW);
   digitalWrite(8, LOW);
   digitalWrite(9, LOW);
   digitalWrite(4, LOW);
    if(sensorval > 500) {
      state = 3;
    }
    else {
      state = 2;
    }
  }
  else {
    Serial.print("6");
    digitalWrite(4, HIGH);
     digitalWrite(6, HIGH);
    for(int i = 0; i <200; i++) {
      digitalWrite(7, HIGH);
      delayMicroseconds(2000);
      digitalWrite(7, LOW);
      delayMicroseconds(2000);
    }
    Serial.print("7");
    delay(5000);
    Serial.print("8");
    digitalWrite(8, HIGH);
    for(int i = 0; i <200; i++) {
      digitalWrite(9, HIGH);
      delayMicroseconds(2000);
      digitalWrite(9, LOW);
      delayMicroseconds(2000);
    }
    delay(5000);
    Serial.println("9");
    state = 0;
  }
  delay(10);
   

}

It looks like some trouble with @sensorval and @state. If sensorval > 500, state goes 0(beginning), 1,2,3,0...

PS: This topic belongs more under the Programming section.