Problem doing two things at once...

I'm using the Duemilanove with an ultrasonic sensor to measure distance. Everything was fine until I added code to light an LED when the sensor measures a specific distance. The code looks like this:

  // this condition turns on the red LED indicator when the sensor measures 50 cm
  if (cm == 50) 
  {
    digitalWrite(ledPin1, HIGH);
    delay(1000);                   // wait for a second
    digitalWrite(ledPin1, LOW);    // turn the red LED off
    delay(1000);                   // wait for a second
  }
  else if (cm > 40 && cm < 50) // this condition turns on the yellow LED indicator when the sensor is approaching 50 cm
  {
    digitalWrite(ledPin2, HIGH);
    delay(1500);                   
    digitalWrite(ledPin2, LOW);    
    delay(1500);    
  }
  delay(100);
}

When the "if" condition is met, the sensor stops transmitting. I need the sensor to continue functioning in the "if" control structure so that the LED can be on and the sensor continues measuring the distance. Any ideas on how I might do this?

Problem fixed! The delays were causing the problem. This http://arduino.cc/en/Tutorial/BlinkWithoutDelay helped me find a solution by using states to keep track of the status of each LED.

Why using delay loops in early PC games was intensely funny. What worked on an XT was clock frequency x delay loop faster when loaded on an AT and the games were unplayable until you loaded a program that NOP'd the system clock execution down to being XT equivalent.

In the real world, once you get past flashing an LED, delay loops are your enemy as they steal processing cycles in exchange for doing nothing.