DigitalWrite is going AI on me

When I connect my nano board to USB (+ external 5VDC supply), it runs as I expect, I can hear the relay switching on/off at the correct time.
When I disconnect the USB, and let the nano run on a (cheap) 5VDC supply, the program enters my “if” statement as if the conditions were true all the time.
Hints to why it does that?

#define zapRelay 7  // relay connected to pin 7
#define echoPin 4   // Echo Pin
#define trigPin 3   // Trigger Pin

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

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

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(zapRelay, OUTPUT);      // sets the digital pin as output
}
void loop()
{
//-----------------------------------ultrasound begin
/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 
 if (distance >= maximumRange || distance <= minimumRange){
 /* Send a negative number to computer and Turn LED ON 
 to indicate "out of range" */
 Serial.println("-1");
 }
 else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance); 
 }
 
 //Delay 50ms before next reading.
 delay(50);
//-----------------------------------ultrasound end
 
  if(distance < 10)
  {  
    Serial.println("Zap");
    digitalWrite(zapRelay, HIGH);   // sets the relay pin high
    Serial.println("Zap ON");
    delay(500);                    // 
    digitalWrite(zapRelay, LOW);    
    Serial.println("Zap OFF");
    delay(5000);
  }
}

How do you connect the CHEAP 5VDC volt power supply to the Nano?

Is the CHEAP 5VDC power supply regulated? .

You have not told us what you are using to transmit sound waves.
You have not told us what you are using to receive sound waves.
You have not shown us a schematic.
You have not shown us a wiring diagram.

Obviously, the cheap 5VDC supply has issues, perhaps too much ripple, perhaps something else. However, I cannot play these kind of guessing games. An oscilloscope would be really handy about now.

5VDC smps from DX, I am pretty sure it was a regulated one. I connect the 5VDC to the nano's VIN and GND pins

I connect the 5VDC to the nano's VIN and GND pins

Doesn't VIN require the input voltage be higher than 5V ?

sorry for shortcomings in my post. HC-SR04 for sound, getting its power from the nano, the relay is getting power from the SMPS, and a signal wire from the nano. Where would you hook up the oscillator?......I have one

1st, measure the output of the power supply. Adjust as needed. 2nd, connect the power supply to the 5 volt pin on the Nano. .

power output is 5.22V which should be fine, it is connected to the 5V Vin pin

eXistenZ: it is connected to the 5V Vin pin

There is a 5V pin and there is a Vin pin. They aren't the same. Which one are you using?

Vin connected to the ps

eXistenZ: Vin connected to the ps

Vin goes through the voltage regulator. You need 7.5V or more there. Power through the 5V pin with your 5V supply.