Problem combining Water flow sensor,Temperature sensor and Motorized ball valve.

Hi,everyone I am working on a project which works as a mixer of Hot and Cold water at proper temperature and discharge it at set flow rate.The problem which i am facing is that when the arduino commands the motor to open or close the ball valve at that time the water flow sensor shows variable results.My code is using water sensor flow rate value to set the position of motorised ball valve and due to wrong value output from water flow sensor the arduino runs the wrong if statement.
I am unable to figure out why this is happening ,so i request everyone to help me with my code..thanks in advance..
apologies for my bad english

#include <OneWire.h>  //one wire library add
#include <DallasTemperature.h> //dallas library add
#define ONE_WIRE_BUS 12 //one wire port declaration
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

int speed;

// Motor 1
int dir1PinA = 17;
int dir2PinA = 16;
int speedPinA = 5; // Needs to be a PWM pin to be able to control motor speed

// Motor 2
int dir1PinB = 15;
int dir2PinB = 14;
int speedPinB = 6; // Needs to be a PWM pin to be able to control motor speed
int sensVal;


unsigned int flowSpeed;       //declaring variable for flow speed          

//water flow sensor
volatile int flow_frequency; // Measures flow sensor pulses
unsigned int l_min; // Calculated litres/min
unsigned char flowsensor = 2; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;
void flow () // Interrupt function
{
   flow_frequency++;
}

int minFlowRate=3;         
//declaring water flow range
int maxFlowRate=5;

int minTemp=28;           
//declarring water temp range
int maxTemp=30;

int Unidelay=1000;

int mot1delay=50;

int mot2delay=200;

void setup() {

 pinMode(flowsensor, INPUT);
 digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up  
 
 pinMode(dir1PinA,OUTPUT);
 pinMode(dir2PinA,OUTPUT);
 pinMode(speedPinA,OUTPUT);
 pinMode(dir1PinB,OUTPUT);
 pinMode(dir2PinB,OUTPUT);
 pinMode(speedPinB,OUTPUT);

 analogWrite(speedPinB, 255);
 digitalWrite(dir1PinB, LOW);
 digitalWrite(dir2PinB, HIGH);//motor 2 reverse
 delay(4000);
 digitalWrite(dir1PinB, LOW);
 digitalWrite(dir2PinB, LOW); //motor 2 stop

 analogWrite(speedPinA, 255);
 digitalWrite(dir1PinA, HIGH);
 digitalWrite(dir2PinA, LOW);//motor 1 reverse
 delay(4000);

 digitalWrite(dir1PinA, LOW);
 digitalWrite(dir2PinA, LOW);  // motor 1 stop


 Serial.begin(9600);
 sensors.begin();

 attachInterrupt(0, flow, FALLING); // Setup Interrupt
 sei(); // Enable interrupts
 currentTime = millis();
 cloopTime = currentTime;
    
 }

 

void loop() {

 {
   currentTime = millis();
   // Every second, calculate and print litres/hour
   if(currentTime = (cloopTime + 1000))
   {
     
      cloopTime = currentTime; // Updates cloopTime
      // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
      l_min = (flow_frequency / 3.50); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
      flow_frequency = 0; // Reset Counter
      
    Serial.print(l_min, DEC); // Print litres/min
   Serial.println(" L/min");
     
      
   }

   
 }
 

  unsigned int flowSpeed = l_min ;  //setting flowSpeed equals to output of water flow sensor
 
 
  {
   sensors.requestTemperatures(); // Send the command to get temperatures
  
  // After we got the temperatures, we can print them here.
  // We use the function ByIndex, and as an example get the temperature from the first sensor only.
  
   int sensVal= (sensors.getTempCByIndex(0)); //declaring variable for sensor reading
   Serial.print("Temperature for the device 1 (index 0) is: ");
   Serial.println(sensVal);

  

  }


 if((sensVal>=minTemp && sensVal<=maxTemp) && (flowSpeed>=minFlowRate && flowSpeed<=maxFlowRate)){
  digitalWrite(dir1PinA, LOW); //motor 1 stop
  digitalWrite(dir2PinA, LOW); 
  Serial.println("1 STOP");

  digitalWrite(dir1PinB, LOW);
  digitalWrite(dir2PinB, LOW);//motor 2 stop
  Serial.println("2 STOP");
  
  Serial.println("TEMP AND FLOW RATE STABLE");
  delay(200);
  
 }

else if((sensVal>maxTemp) && (flowSpeed<minFlowRate))  {

  analogWrite(speedPinB, 255);
  digitalWrite(dir1PinB, HIGH);
  digitalWrite(dir2PinB, LOW);//motor 2 forward
  delay(mot2delay);
  digitalWrite(dir1PinB, LOW);
  digitalWrite(dir2PinB, LOW); //motor 2 stop
 
  
  Serial.println("2 forward");
  Serial.println("statement 2");
  }

else if ((sensVal>maxTemp) && (flowSpeed>maxFlowRate)) {

 
  
  analogWrite(speedPinA, 255);
  digitalWrite(dir1PinA, HIGH);
  digitalWrite(dir2PinA,LOW);//motor 1 reverse
  delay(mot1delay);
  digitalWrite(dir1PinA, LOW);
  digitalWrite(dir2PinA,LOW);  //motor 1 stop
  Serial.print("1 reverse");
  Serial.println("statement new 1");
  delay(Unidelay);//wait sensor to stablize
}

else if((sensVal<minTemp ) && (flowSpeed<minFlowRate))  {

  analogWrite(speedPinA, 255);
  digitalWrite(dir1PinA, LOW);
  digitalWrite(dir2PinA, HIGH);//motor 1 forward
  delay(mot1delay);
  Serial.println("1 forward");
  digitalWrite(dir1PinA, LOW);//motor 1 stop
  digitalWrite(dir2PinA, LOW);
  //analogWrite(speedPinB, 200);
  //digitalWrite(dir1PinB, HIGH);
  //digitalWrite(dir2PinB, LOW);//motor 2 forward
  //delay(mot2delay);
  //Serial.println("2 forward");
  Serial.println("statement 3");
  //digitalWrite(dir1PinB, LOW);//motor 2 stop
  //digitalWrite(dir2PinB, LOW);
  delay(Unidelay);//wait sensor to stablize
 } 

else if((sensVal<minTemp)&&(flowSpeed>maxFlowRate))  {

  
  
  analogWrite(speedPinB, 255);
  digitalWrite(dir1PinB, LOW);
  digitalWrite(dir2PinB, HIGH);//motor 2 reverse
  delay(mot2delay);
  Serial.println("2 reverse");
  Serial.println("statement 2 new");
  digitalWrite(dir1PinB, LOW);//motor 2 stop
  digitalWrite(dir2PinB, LOW);
  delay(Unidelay);//wait sensor to stablize

  
}

 else if((sensVal<minTemp)&&(flowSpeed>=minFlowRate && flowSpeed<=maxFlowRate)) {

 analogWrite(speedPinA, 255);
 digitalWrite(dir1PinA, LOW);
 digitalWrite(dir2PinA, HIGH);//motor 1 forward
 delay(mot1delay);
 Serial.println("1 forward");
 digitalWrite(dir1PinA, LOW);//motor 1 stop
 digitalWrite(dir2PinA, LOW);
 
 analogWrite(speedPinB, 255);
 digitalWrite(dir1PinB, LOW);  
 digitalWrite(dir2PinB, HIGH);//motor 2 reverse
 delay(100);
 Serial.println("2 reverse");
 digitalWrite(dir1PinB, LOW);//motor 2 stop
 digitalWrite(dir2PinB, LOW);
 Serial.println("statement 4");
 delay(Unidelay);//wait sensor to stablize

 
 }


 else if((sensVal>maxTemp) && (flowSpeed>=minFlowRate && flowSpeed<=maxFlowRate)) {

 analogWrite(speedPinB, 255);
 digitalWrite(dir1PinB, HIGH);
 digitalWrite(dir2PinB, LOW);//motor 2 forward
 delay(mot2delay);
  Serial.println("2 forward");
 digitalWrite(dir1PinB, LOW);//motor 2 stop
 digitalWrite(dir2PinB, LOW);

 analogWrite(speedPinA, 255);
 digitalWrite(dir1PinA, HIGH);
 digitalWrite(dir2PinA, LOW);//motor 1 reverse
 delay(mot1delay); 
  Serial.println("1 reverse");

 digitalWrite(dir1PinA, LOW);//motor 1 stop
 digitalWrite(dir2PinA, LOW);
 Serial.println("statement 5");
 delay(Unidelay);//wait sensor to stablize
 
 }


 else if((sensVal>=minTemp && sensVal<=maxTemp) && (flowSpeed<minFlowRate))  {

 analogWrite(speedPinA, 255);
 digitalWrite(dir1PinA, LOW);
 digitalWrite(dir2PinA, HIGH);//motor 1 forward
 delay(mot1delay);
  Serial.println("1 forward");
 digitalWrite(dir1PinA, LOW);//motor 1 stop
 digitalWrite(dir2PinA, LOW);
 analogWrite(speedPinB, 255);
 digitalWrite(dir1PinB, HIGH);
 digitalWrite(dir2PinB, LOW);//motor 2 forward
 delay(mot2delay);
  Serial.println("2 forward");
Serial.println("statement 6");
 
 digitalWrite(dir1PinB, LOW);//motor 2 stop
 digitalWrite(dir2PinB, LOW);
 delay(Unidelay);//wait sensor to stablize
 
  
 }

 else if((sensVal>=minTemp && sensVal<=maxTemp) && (flowSpeed>maxFlowRate))  {

 analogWrite(speedPinA, 255);
 digitalWrite(dir1PinA, HIGH);
 digitalWrite(dir2PinA, LOW);//motor 1 reverse
 delay(mot1delay); 
 Serial.println("1 reverse");
 digitalWrite(dir1PinA, LOW);//motor 1 stop
 digitalWrite(dir2PinA, LOW);
 analogWrite(speedPinB, 255);
 digitalWrite(dir1PinB, LOW);
 digitalWrite(dir2PinB, HIGH);//motor 2 reverse
 delay(100);
 Serial.println("2 reverse");
 Serial.println("statement 7");

 
 digitalWrite(dir1PinB, LOW);//motor 2 stop
 digitalWrite(dir2PinB, LOW);
 delay(Unidelay);//wait sensor to stablize
 
 
  
 }

   }

when the arduino commands the motor to open or close the ball valve at that time the water flow sensor shows variable results.

That usually indicates that there is noise or droop induced onto the power supply by inductive or high current components. That can often be solved with proper filtering and bypassing or a separate (from processor power) power supply for the high current devices. Can you post a schematic of your system?

Actually my motorised ball valve is powered from a 2amp smps through motor controller.and the water flow sensor is just using the power from the arduino which gains power from usb connection to my laptop..