 
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////////////// 
-//                                                5. SOLENOID DRAIN VALVE
+//                                                6. SOLENOID DRAIN VALVE
 
 
 int solenoidValvePin = 5; //Pin controlling the valve

    data += flow_rate_cc_per_min;
    
    Serial.println (data);
    
    last_print_time = millis();
  }
}

void data_read(int *p)
{
  int i,j,PinState,tempData;
   
  digitalWrite(TestPin, LOW);
  for(i=0;i<5;i++)
  {
  for(j=0;j<8;j++)
  {
    do{
      PinState = digitalRead(ClockPin);
    }while(PinState);
    delayMicroseconds(100);
    PinState = digitalRead(DataPin);
    if(1 == PinState)
      tempData = (tempData<<1 & 0xfe)+1;
    else
      tempData = (tempData<<1 & 0xfe);
    do{
      PinState = digitalRead(ClockPin);
    }while(PinState != 1);
  }
  *p++ = tempData;
  }
  digitalWrite(TestPin, HIGH);
}



  
  /////////////////////////////////////////////////////////Temperature
  
  if (millis() - lastTempRequest >= delayInMillis) {    // waited long enough??
    ambient_temperature = sensors.getTempCByIndex(0);
    casing_temperature = sensors.getTempCByIndex(1);
	gland_temperature = sensors.getTempCByIndex (2);
    sensors.requestTemperatures(); 
    delayInMillis = 750 / (1 << (3));
    lastTempRequest = millis(); 
  }


  //////////////////////////////////////////////////////Load Washer
 
  load1_samples[index_i] = analogRead(loadWasher1Pin);
  load2_samples[index_i] = analogRead(loadWasher2Pin);
  index_i = (index_i + 1)%load_sample_window;

 
  //////////////////////////////////////////////////////Water Level Sensor
 
  analog_water_level = analogRead(levelSensorPin);
  water_level_cm = ( analog_water_level - B_water_reference_level ) / A_water_rate;
   
  
  
  //////////////////////////////////////////////////////Printing 
  // Every PRINT_DELAY, disable ISR and do calculations which otherwise block the flow of the main loop,
  // including printing to serial that sends data to relay node.
  
  
  if (millis() - last_print_time >= PRINT_DELAY){
    detachInterrupt (0);  // disable tachometer interrupt
	
	//////////////////////////////////////////////////////Calculate load washer averages - ??? ms
	load1_value = 0;
    for (int j = 0; j < load_sample_window; j++){
      load1_value+= load1_samples[j];
    }
    load1_value = load1_value / load_sample_window;

    load2_value = 0;
    for (int j = 0; j < load_sample_window; j++){
      load2_value+= load2_samples[j];
      }
    load2_value = load2_value / load_sample_window;
 
    load1_in_lbs = ( load1_value - B_reference_level ) / A_linear_rate;
    load2_in_lbs = ( load2_value - B_reference_level ) / A_linear_rate;
		
    //////////////////////////////////////////////////////Shaft Temperature - 460 ms
    int data_buf[5] = {0};
    int tempData = 0; 
    data_read(data_buf);
    tempData = data_buf[1]*256 + data_buf[2];
    shaftTemp = (float)tempData/16-273.15;
	
	//////////////////////////////////////////////////////Solenoid Drain Valve - ??? ms
    //Start the drain if the water level is too high
    if(water_level_cm >= drain_at_cm_level){
      //TODO: If water level is way too high, then water is accumulating so send the operator a warning message
      digitalWrite(solenoidValvePin,HIGH);
      solenoid_valve_open = true;
    }

   if (solenoid_valve_open == false){
     if (water_level_cm >= (flow_calc_water_level_cm + water_level_buffer)){
       float water_level_delta = analog_water_level - flow_calc_water_level_cm;
       unsigned long time_delta = millis() - calc_time_ms;
       flow_rate_cc_per_min = ( water_level_delta ) / (time_delta);
       //   flow_calc_water_level_cm = analog_water_level;
     }
   }

   if (water_level_cm <= close_valve_at_cm_level){
     digitalWrite(solenoidValvePin,LOW);
     solenoid_valve_open = false;
     initialize=true;
   }
	
	///////////////////////////////////////////////////////Water lever sensor calculations - ??? ms
	//Set the reference water level for a calculation (when appropriate)
    if(initialize==true){
      flow_calc_water_level_cm = water_level_cm;
      calc_time_ms = millis();
      initialize=false;
    }
	
	
	//////////////////////////////////////////////////////Printing 	  
	data = "";
    data += shaftTemp;
    data += ",";
	data += ambient_temperature;
    data += ",";
    data += casing_temperature;
    data += ",";
	data += gland_temperature;
	data += "'";
    data += rpm;
    data += ",";
    data += load1_in_lbs;
