Having trouble with comparison statement

I've only ever used arduino one time before now so my code is pieced together from things I've found online and from the arduino examples. I'm using an arduino mega 2560 and 2 HC-SR04 ultrasonic sensors. I'm trying to get 2 LED's to light at 2 different frequencies for 2 different distances. If limit the code to one distance it works fine. Once I try to get it going for both distances it only works for 1 of them. I've been playing around with the first set of code so the second run doesn't match the first.

// Pin 13 has an LED connected on most Arduino boards.
    // give it a name:
    int led = 13;
    int led1 = 12;
    
    // Variables will change:
    int ledState = LOW;             // ledState used to set the LED
    int ledState1 = LOW;
    
    long previousMillis = 0;        // will store last time LED was updated
    long previousMillis1 = 0;

    // the follow variables is a long because the time, measured in miliseconds,
    // will quickly become a bigger number than can be stored in an int.
    long interval = 5000;           // interval at which to blink (milliseconds)
    long interval1 = 1000;
    
    //pin which triggers ultrasonic sound
    const int pingPin1 = 11;
    const int pingPin2 = 9;
     
    //pin which delivers time to receive echo using pulseIn()
    int inPin1 = 10;
    int inPin2 = 8;
 
    //range in cm which is considered safe to enter, anything
    //coming within less than 5 cm triggers red LED
    int safeZone = 10
   ;
    int safeZone1 = 25;
    
    void setup() 
    {
      // initialize serial communication
      Serial.begin(9600);
      
      // initialize the digital pin as an output.
      pinMode(led, OUTPUT); 
      pinMode(led1, OUTPUT);
    }
     
    void loop()
    {
      //raw duration in milliseconds, cm is the
      //converted amount into a distance
      long duration, cm;
      
      // check to see if it's time to blink the LED; that is, if the 
      // difference between the current time and last time you blinked 
      // the LED is bigger than the interval at which you want to 
      // blink the LED.
      unsigned long currentMillis = millis();
      unsigned long currentMillis1 = millis();
     
      //initializing the pin states
      pinMode(pingPin1, OUTPUT);
      pinMode(pingPin2, OUTPUT);
     
      //sending the signal, starting with LOW for a clean signal
      digitalWrite(pingPin1, LOW);
      delayMicroseconds(2);
      digitalWrite(pingPin1, HIGH);
      delayMicroseconds(5);
      digitalWrite(pingPin1, LOW);
     
      //setting up the input pin, and receiving the duration in
      //microseconds for the sound to bounce off the object infront
      pinMode(inPin1, INPUT);
      duration = pulseIn(inPin1, HIGH);
     
      // convert the time into a distance
      cm = microsecondsToCentimeters(duration);
     
      //printing the current readings to ther serial display
      Serial.print(cm);
      Serial.print("cm1");
      Serial.println();
      delay(50);
        
              if (cm > safeZone && cm < safeZone1)
  {
        if(currentMillis - previousMillis > interval) 
  {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(led, ledState);
  }
  } 
  
  if (cm < safeZone && cm < safeZone1)
  {
  if(currentMillis - previousMillis > interval1) 
  {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(led, ledState);
  }
  
  }
  else              
  {ledState = LOW;
    // save the last time you blinked the LED 
   // previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
   // if (ledState == LOW)
     // ledState = HIGH;
   // else
     // ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(led, ledState);
  }

 
  delay(20);
    
     
      //run 2
     
      //sending the signal, starting with LOW for a clean signal
      digitalWrite(pingPin2, LOW);
      delayMicroseconds(2);
      digitalWrite(pingPin2, HIGH);
      delayMicroseconds(5);
      digitalWrite(pingPin2, LOW);
     
      //setting up the input pin, and receiving the duration in
      //microseconds for the sound to bounce off the object infront
      pinMode(inPin2, INPUT);
      duration = pulseIn(inPin2, HIGH);
     
      // convert the time into a distance
      cm = microsecondsToCentimeters(duration);
     
      //printing the current readings to ther serial display
      Serial.print(cm);
      Serial.print("cm2");
      Serial.println();
     
      delay(50);
      
 
  
if (cm < safeZone)
  {
    if(currentMillis1 - previousMillis1 > interval) 
  {
    // save the last time you blinked the LED 
    previousMillis1 = currentMillis1;   

    // if the LED is off turn it on and vice-versa:
    if (ledState1 == LOW)
      ledState1 = HIGH;
    else
      ledState1 = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(led1, ledState1);
  }
  } 
  else              
  {
 // save the last time you blinked the LED 
  //  previousMillis1 = currentMillis1;   

    // if the LED is off turn it on and vice-versa:
    //if (ledState1 == LOW)
      //ledState1 = HIGH;
   // else
      ledState1 = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(led1, ledState1);
 }
    }
     
    long microsecondsToCentimeters(long microseconds)
    {
      // The speed of sound is 340 m/s or 29 microseconds per centimeter.
      // The ping travels out and back, so to find the distance of the
      // object we take half of the distance travelled.
      return microseconds / 29 / 2;
    }

I'm not sure what you're trying to achieve there, but you obviously have quite a lot of code duplication.

Before you get bogged down into the logic for threshold comparisons and timing, have you confirmed that both distance measurements are giving you reliable results? You've got print statements there, but you don't show us what they told you.

Also, which LED is working and which one isn't, and in which scenario?

I have verified that I'm getting good readings from both sensors. And apologies for the sloppy code, I'm sure there are cleaner ways to do it, but running the code individually for each sensor was the only way i could get it to work. Right now I'm only working with the one LED to see if i could get the code working.

This is the statement that doesn't seem to work. I'm trying to flash the light when the sensor is reading between 10cm and 25cm.

if (cm > safeZone && cm < safeZone1)

This works fine. It flashes at 10cm.

if (cm < safeZone && cm < safeZone1)

and of course, Thank you.

rcuello80:
This is the statement that doesn't seem to work. I'm trying to flash the light when the sensor is reading between 10cm and 25cm.

if (cm > safeZone && cm < safeZone1)

This works fine. It flashes at 10cm.

if (cm < safeZone && cm < safeZone1)

Hi, stripping the logic to the bare minimum you have:

  if (cm > safeZone && cm < safeZone1)
  {
// blink at interval frequency
  } 
  if (cm < safeZone && cm < safeZone1)
  {
// blink at interval1 frequency
  }
  else              
  {
    ledState = LOW;
    digitalWrite(led, ledState);
  }

Note that the else block is executed whenever (cm < safeZone && cm < safeZone1) is false, thus including the case when (cm > safeZone && cm < safeZone1) is true.

The moral of this story is that whenever you have multiple if statements, there is a good possibility that some of them should be else if statements, instead.

Also, duplication between those different else if clauses should be minimised. It would be much cleaner as:

if (cm < critical)
{
  blinkFast();
}
else if (cm < marginal)
{
  blinkSlow();
}
else
{
  noBlink();
}

Also, any time you find yourself numbering your variables or functions, you should take this as a hint that your design is wrong. In this case, the design could be improved considerably by using arrays instead of numbered variables.

The else if statement made it work perfect, or at least as well as I needed. Thank you guys for your help.