Relay module rapidly cycling

I'm working on a parking indicator for my garage. I'm using a HC-SR04 to measure the distance from the front wall to my car, a proximity sensor that detects if my garage door is open or closed, and a red and green light that light up based on the state of the garage door and my distance from the wall. I was able to get everything to work in the simulator on tinkerCAD, but in the porcess of settign it up in the real world the light relay module wouldn't work correctly. To troubleshoot this I started removing coding until I was able to get things working again. I then started adding bits of code back into the program. The issue I'm having is that when I added in the code for the HC-SR04, and I trigger my proximity switch it causes the relay modules controlling the lights to rapidly cycle. Here is the code:


int south_sensorTrig = 11; // Sets the South sensor Trigger to pin to 11
int south_sensorEcho = 10;  // Sets the South sensor Echo to pin 10
int south_Red = 7;    //sets pin 7 as the South Red light
int south_Green = 6;   //sets pin 6 as the South Green light
long south_time;       //time variable for south sensor
int south_distance;    //distance variable for south sensor
int north_sensorTrig = 9; // Sets the North sensor Trigger to pin to 9
int north_sensorEcho = 8;  // Sets the North sensor Echo to pin 8
int north_Red = 5;
int north_Green = 4;
long north_time;
int north_distance;

void setup() {
  // put your setup code here, to run once:
  pinMode(south_sensorTrig, OUTPUT);
  pinMode(south_sensorEcho, INPUT);
  pinMode(south_Red, OUTPUT); // South RED LED output
  pinMode(south_Green, OUTPUT); // South GREEN LED output
  pinMode(north_Red, OUTPUT); // North RED LED output
  pinMode(north_Green, OUTPUT); // North GREEN LED output  
  pinMode(3, INPUT_PULLUP); // Input from PROX Switch relay (R1 on print)
   Serial.begin(9600); // Starts Serial Monitor
}

void loop() {

  int ProxState = digitalRead(3); //Sets Prox switch variable

  digitalWrite(south_sensorTrig, LOW); //(Clears pin)
  delayMicroseconds(2); // Wait for 2
  digitalWrite(south_sensorTrig, HIGH);
  delayMicroseconds(10);
  digitalWrite(south_sensorTrig, LOW);
  
  south_time = pulseIn(south_sensorEcho, HIGH);
  south_distance = south_time * 0.034 / 2; //Speed of sound 



 if(digitalRead(ProxState) == LOW )
  {
  digitalWrite(south_Red, HIGH);
  digitalWrite(south_Green, HIGH);
  digitalWrite(north_Red, HIGH);
  digitalWrite(north_Green, HIGH);
  
  }
  else
  {
  digitalWrite(south_Red, LOW);
  digitalWrite(south_Green, LOW);
  digitalWrite(north_Red, LOW);
  digitalWrite(north_Green, LOW);
  }
  Serial.print("Prox State:");
  Serial.println(ProxState);
  Serial.print("Distance in cm:");
  Serial.println(south_distance);
  
}

I can get it to stop rapidly cycling if I comment out the line:

south_time = pulseIn(south_sensorEcho, HIGH);

Any ideas?

Seems odd to be reading the state of a digital pin depending on the state of another digital pin. Your code does not show what is connected to pin 0 or 1, which are the only two possible values for HIGH and LOW that are stored in ProxState.

Did you possibly mean to use " if (ProxState == LOW)"

Did you possibly mean to use " if (ProxState == LOW)"

Yep. That's fixed it. Thanks! Weird that it worked in TinkerCAD like that.