Switch Case beging skipped

I’ve been having some troubles with the entire ‘switch case’ of my code being skipped over the first time void loop() runs. But the second time through and ever after it runs fine.

Here’s my code,

int dirpin = 2;                                 // pin 3 is direction pin
int steppin = 12;                              // pin 12 "starts" motor
int val1 = 0;                                   // val1 will store sensor1 value
int val2 = 0;                                   // val2 will store sensor2 value
int val3 = 0;                                  // val3 will store okbutton value
int val4 = 0;                                  // stores STARTBUTTON count
int SENSOR1 = 4;                              // input pin for sensor1
int SENSOR2 = 5;                              // input pin for sensor2
int OKBUTTON = 6;                              // input pin for ok button
int STARTBUTTON = 3;                            // on switch
int LED1 = 9;                                 // output pin for led1
int LED2 = 8;                                  // output pin for led2
int LED3 = 7;                                  // output pin for "both" led3
int firstSwitch;                               // variable for first switch case
int i;
void setup() {

Serial.begin(9600);

pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(SENSOR1, INPUT);
pinMode(SENSOR2, INPUT);
pinMode(OKBUTTON, INPUT);
pinMode(STARTBUTTON, INPUT);
}

void loop() {    
  Serial.println("absolute");
  while (val4 == 0) {
    val4 = digitalRead(STARTBUTTON);             // check start button
    int firstSwitch = 1;                         // set "starting" switch case
    Serial.println("Starting Here");
  } 



// it skips this entire switch case first time through

  switch (firstSwitch) {
    case 1:                                        // start up procedure
    Serial.println("starting - case 1");
    do {
        val1 = digitalRead(SENSOR1);
        val2 = digitalRead(SENSOR2);
        if (val1 == HIGH) {
          digitalWrite(LED1, HIGH);
        } else {
          digitalWrite(LED1, LOW);
        }
        if (val2 == HIGH) {
          digitalWrite(LED2, HIGH);
        } else {
          digitalWrite(LED2, LOW);
        }
      if ((val1 == HIGH) && (val2 == HIGH)) {
        digitalWrite(LED3, HIGH);
        do {
          val3 = digitalRead(OKBUTTON);
        } while (val3 == LOW);
      }
      } while ((val1 == LOW) || (val2 == LOW));
      digitalWrite(LED1, LOW);
      digitalWrite(LED2, LOW);
      digitalWrite(LED3, LOW);
      break;
    case 2:                                      // bad procedure, add in buzzer   
      Serial.println("bad - case 2");
      do {
        val1 = digitalRead(SENSOR1);
        val2 = digitalRead(SENSOR2);
        if (val1 == HIGH) {
          digitalWrite(LED1, HIGH);
        } else {
          digitalWrite(LED1, LOW);
        }
        if (val2 == HIGH) {
          digitalWrite(LED2, HIGH);
        } else {
          digitalWrite(LED2, LOW);
        }
      if ((val1 == HIGH) && (val2 == HIGH)) {
        digitalWrite(LED3, HIGH);
        do {
          val3 = digitalRead(OKBUTTON);
        } while (val3 == LOW);
      }
      } while ((val1 == LOW) || (val2 == LOW));
      digitalWrite(LED1, LOW);
      digitalWrite(LED2, LOW);
      digitalWrite(LED3, LOW);
      break;
    case 3:                                            // good procedure
      Serial.println("good - case 3");
      delay(4000);
      break;
  } 

// and goes directly to this point


  
  digitalWrite(dirpin, LOW);                        // Set the direction.
  delay(100);

  for (i = 0; i<10000; i++) {                     // 10000 microsteps
     digitalWrite(steppin, LOW);                  // low to high for rising edge
     digitalWrite(steppin, HIGH); 
     delayMicroseconds(200);                     // close to top speed 
  }
  Serial.println("motor has moved");
  val1 = digitalRead(SENSOR1);
  val2 = digitalRead(SENSOR2);
  if ((val1 == HIGH) && (val2 == HIGH)) {    // if val1 & val2 are HIGH
    Serial.println("sensors good");
    Serial.flush();                                        // clear the serial port
    Serial.print("!");                                   // send "!" to serial port
    firstSwitch = 0;                                   // set firstSwitch to zero
    do {
      if (Serial.available() > 0) {                   // if serial is availabe
        firstSwitch = Serial.read();                // read serial into firstSwitch
        firstSwitch = (firstSwitch - 48);
      }
    } while (firstSwitch == 0);             // keep checking if processing has finished     
    Serial.println(firstSwitch, DEC);
  } else {
    firstSwitch = 2;                                   // if val1 or val2 are low set firstSwitch to bad
    Serial.println("sensors did not line up");
  }   
}

Any suggestions would be awesome.

Thanks!

int firstSwitch = 1;

There's your problem. The global "firstSwitch" is initialised to zero.

Thanks for the reply! Granted I haven't been working with the arduino and C programming for long. But its amazing how the little thinks make a big difference. Thanks for the second pair of eyes, it works perfectly now!