Problems breaking while/for loops

Hi guys,

I'm fairly new to the Arduino and programming in general. I've been messing around with the starter kit and in one of the projects it gets you to create a 'digital hourglass'. This consists of a set of LEDs which switch off as time elapses, it is reset using a tilt switch.

Getting the initial sketch to work was simple enough. Though when I tried to add a condition such that when all the LEDs are off and the specified time period has elapsed I get some feedback such as light or sound which is reset by flipping the tilt switch, I couldn't get it to work. I can get the LEDs to flash and piezo to sound, the problem is once it begins the 'while' loop it doesn't seem to be registering a change in the state of the tilt switch and so continues indefinitely until I reset the Arduino. If the tilt switch is flipped DURING the countdown of the timer then it resets fine.

One thing I will say is that I've inserted some serial commands into the code to check observe the switch state and time elapsed during the countdown. Once it reaches the for/while loops however it stops registering any change in the switch and just prints a 1.

Here's the code:

const int switchPin = 8; //Tilt switch pin
const int piezoPin = 9;//Piezo output pin

unsigned long previousTime = 0;//placeholder for time after a certain interval

int switchState = 0;//initial state of the tilt switch 
int prevSwitchState = 0;//previous tilt switch state

int led = 7;//controls the number of LEDs switched on

long interval = 1000;//time interval between switching off LEDs

void setup() {
  for(int x = 7;x>1;x--){ 
    pinMode(x, OUTPUT);
  }
  
  pinMode(switchPin, INPUT);
  pinMode(piezoPin, OUTPUT);
  
  Serial.begin(9600);
}

void loop() {
  unsigned long currentTime = millis();//total time elapsed since last reset
  
  Serial.print("Swtich state: ");
  Serial.print(switchState);
  Serial.print(", Time elapsed: ");
  Serial.print(currentTime);
  Serial.print(", Previous Time: ");
  Serial.println(previousTime);
  
  if(currentTime - previousTime > interval){
    previousTime = currentTime;
    digitalWrite(led, LOW);
    led--;
    
    if(led == 2){
      [color=red]while(switchState == 1){ // while the tilt switch is in it's natural state (1) the loop will run
        
        if(switchState==1){ 
          
        Serial.print("Swtich state: ");
        Serial.println(switchState);
        
        for(int x = 7; x>1; x--){ //Cycle through LED 
          digitalWrite(x, HIGH);
          delay(100);
          digitalWrite(x, LOW);
          
        }
     }
        if(switchState != 1){ // if switch is flipped loop should break?
          Serial.print("switch 0");
          led = 7;
          break;[/color]
         }
          
        }
      
      }
       
  } 
     
  switchState = digitalRead(switchPin);
  
  if(switchState != prevSwitchState){
    for(int x = 7; x>1; x--){
      digitalWrite(x, HIGH);
    }
    
    led = 7;
    previousTime = currentTime;
  }
  
  prevSwitchState = switchState;
  
}

The part in red is where I think I am going wrong but I don't know where.

Any help would be much appreciated.

The part in red is where I think I am going wrong but I don't know where.

These is no part in red. You can't color code.

If you put each { on a new line, and used Tools + Auto Format, your code would be a lot easier to read.

Where in the while loop does switchState get a new value? It looks to me like never is the answer. Once the while loop starts, it can never end.

You actually need to read the switch pin somewhere.

You are not reading a new value for the switchState variable from within the loop, so it will never break out of it.

Doh!

Thanks for the help guys!