Run state machine if condition met

Oh gosh, I didn't copy it all! Sorry. Here is the full code for real!

//state machine setup
const int S_stopped = 0;
const int S_sensorTrigger = 1;
const int S_seriesAOn = 2;
const int S_seriesAWait = 3;
const int S_seriesAOff = 4;
const int S_seriesAOffWait = 5;
const int S_seriesBOn = 6;
const int S_seriesBWait = 7;
const int S_seriesBOff = 8;
const int S_seriesBOffWait = 9;
 

//piezo buzzers set up
const int buzzerA = 5;
const int buzzerB = 6;
int frequency = 1;
unsigned long int reg;

//ultasonic sensor set up
const int trigPin = 7;
const int echoPin = 8;

//Nitinol springs set up
const int springA = 9;
const int springB = 10;


void setup() {
  Serial.begin(9600);

  pinMode(buzzerA, OUTPUT);
  pinMode(buzzerB, OUTPUT);
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  pinMode(springA, OUTPUT);
  pinMode(springB, OUTPUT);

  reg = 0x55aa55aaL; //seed for white noise bitstream
}

 
void loop()
{
  
 long duration, , cm;
 
  static int state = S_stopped ;
  static unsigned long ts;


      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(5);
      digitalWrite(trigPin, LOW); //sends out ping
        
      duration = pulseIn(echoPin, HIGH); //reads the echo
    
      cm = microsecondsToCentimeters(duration); //time to distance

      Serial.print(cm);
      Serial.print("cm");
      Serial.println();
      
  switch (state)
  { 
    case S_stopped:

      state = S_sensorTrigger;

      break;

    case S_sensorTrigger:
    
      if ((cm <= 40) && (cm >= 2)) {
        Serial.print("starting state machine");
        state = S_seriesAOn;
      }
      else {
        state = S_stopped;
      }

      break;
    
    case S_seriesAOn:
      Serial.print("SpringA heating");
      digitalWrite( springA, HIGH);
      generateNoise(frequency);
 
      ts = millis();  // Remember the current time
 
      state = S_seriesAWait;  // Move to the next state
 
      break;
 
    case S_seriesAWait:
      // If x seconds have passed, then move on to the next state.
      if (millis() > ts + 12000)
      {
        state = S_seriesAOff;
      }
 
      break;
 
    case S_seriesAOff:
      Serial.print("SpringA cooling");
      digitalWrite( springA, LOW);
 
      ts = millis(); 
 
      state = S_seriesAOffWait;
 
      break;
 
    case S_seriesAOffWait:
 
      if (millis() > ts + 10000)
      {
        state = S_seriesBOn;
      }
 
      break;
 
    case S_seriesBOn:
      Serial.print("SpringB heating");
      digitalWrite( springB, HIGH);
      generateNoise(frequency);
      
 
      ts = millis();
 
      state = S_seriesBWait;
 
      break;
 
    case S_seriesBWait:
 
      if (millis() > ts + 12000)
      {
        state = S_seriesBOff;
      }
 
      break;

     case S_seriesBOff:
      Serial.print("SpringB cooling");
      digitalWrite( springB, LOW);

      ts = millis();
 
      state = S_seriesBOffWait;
 
      break;
 
    case S_seriesBOffWait:

      if (millis() > ts + 12000)
      {
       Serial.print("restart");
       state = S_stopped;
      }
 
      break;
 
  } // end of switch
 
} // end of loop

void generateNoise(int frequency) {
  unsigned long int newr;
  unsigned char lobit;
  unsigned char b31, b29, b25, b24;
   
  b31 = (reg & (1L << 31)) >> 31;
  b29 = (reg & (1L << 29)) >> 29;
  b25 = (reg & (1L << 25)) >> 25;
  b24 = (reg & (1L << 24)) >> 24;

  lobit = b31 ^ b29 ^ b25 ^ b24;
  newr = (reg << 1) | lobit;
  reg = newr;
  
  digitalWrite(buzzerA, reg & 1);
  digitalWrite(buzzerB, reg & 1);
  
  delayMicroseconds(frequency);    // Changing this value changes the frequency.
}

long microsecondsToCentimeters(long microseconds) {
  return microseconds / 29 / 2;
}