Arduino code for toilet flush using relay

Hello i’m a newbie in arduino programming , i just needed a little help.
I have written the code and it is running but the thing is when i reach the distance of greater than 60 and less than 65, the relay turns off at first which is correct, but then the relay doesn’t turn on when i reach the distance second time…i want the relay to turn on when it comes in that distance(distance>60 && distance<65).
.
.
.
const int trigpin1 = 7;
const int echopin1 = 6;
const int trigpin2 = 9;
const int echopin2 = 8;
int relay1 = 4;
int relay2 = 5;
int counta=0;
int countb=0;
int udis1=0;
int udis2=0;
const unsigned long relayperiod1=5000;
const unsigned long relayperiod2=6000;
unsigned long previousMillis1=0;
unsigned long previousMillis2=0;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigpin1, OUTPUT); // Sets the trigPin as an Output
pinMode(echopin1, INPUT);
pinMode(trigpin2,OUTPUT);
pinMode(echopin2,INPUT);
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);// Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop(){
unsigned long currentmillis=millis();

ultrason(trigpin1,echopin1);
udis1=distance;
ultrason(trigpin2,echopin2);
udis2=distance;
Serial.print("\nudis1::");
Serial.print(udis1);
Serial.print("\nudis2::");
Serial.print(udis2);
if(currentmillis-previousMillis1>=relayperiod1){
if(udis1>40 && udis1<45){
counta+=1;
Serial.print("\nCOUNT1::");
Serial.print(counta);
}
if(udis1>60 && udis1<65 && counta>0){
digitalWrite(relay1,LOW);
digitalWrite(relay1,HIGH);
counta=0;
previousMillis1=currentmillis;
}}
if(currentmillis-previousMillis2>=relayperiod2){
if(udis2>40 && udis2<45){
countb+=1;
Serial.print("\nCOUNT2::");
Serial.print(countb);
}
if(udis2>60 && udis2<65 && countb>0){
digitalWrite(relay2,LOW);
digitalWrite(relay2,HIGH);
countb=0;
previousMillis2=currentmillis;
}}}
void ultrason(int trigpin,int echopin){
// Clears the trigPin
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echopin, HIGH);
// Calculating the distance
distance= (duration*0.034/2);
}

Please help!!

I can’t tell what you’re doing. What do the relays do? Are they active LOW or HIGH?

It seems odd that you change the state of the relays so rapidly. In the second iteration, relay1 will be low so briefly that if it’s a mechanical one, I doubt it will actually change state.