#include <LiquidCrystal.h>
#include  <OneWire.h>
#include <DallasTemperature.h>

const int rs = 3, en = 4, d4 = 5, d5 = 6, d6 = 7, d7 = 8;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // Initialize the LCD and  tell it which pins is to be used for communicating
#define ONE_WIRE_BUS 2  // Data wire is plugged  into port 2 on the Arduino
#define precision 9  // OneWire precision Dallas  Sensor
#define contrast 9  // Define the pin that controls the contrast of  the screen
#define bright 10  // Define the pin the controls the brightness  of the screen
#define rows 2  // Define the rows in display LCD
#define  columns 16  // Define the columns in display LCD
int licznik = 0;        //  Counter of Dallas sensors 

// Setup a oneWire instance to communicate with  any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature  sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.

DeviceAddress  t1, t2, t3; // arrays to hold device addresses
const int SvdPin = 12; // Pin for drain solenoid valve (Svd)    

const unsigned long DRAIN_TIME_MS = 10000; // 10 seconds in milliseconds

void setup() {
  Serial.begin(9600);
  pinMode(SvdPin, OUTPUT);
  digitalWrite(SvdPin,LOW);

  lcd.begin(columns, rows);    //Tell the LCD that it is a Columns x Rows  LCD
  pinMode(contrast, OUTPUT);   //set pin contrast to OUTPUT
  pinMode(bright,  OUTPUT);     //Set pin brightness to OUTPUT
  digitalWrite(bright, HIGH);  //LOW  and HIGH <- digitalWrite OR analogWrite value 0-255 240
  analogWrite(contrast,  50);   //LOW and HIGH <- digitalWrite OR analogWrite value 0-255 54
  lcd.noCursor();              //Disable cursor on LCD
  lcd.clear();                 //Clear  Screen on LCD
  // Start  up the library
  sensors.begin();

  // locate devices on the bus
  Serial.print("Found: ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" Devices.");

  // report parasite power requirements
  Serial.print("Parasite power is: ");
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");
  // Search for devices on the bus and assign based on an index. 
  //Ideally, you would do this to initially discover addresses on the bus 
  //and then use those addresses
  //and manually assign them (see above) once you know
 // the devices on your bus (and assuming they don't change).
 //if (!sensors.getAddress(t1, 0)) Serial.println("No  Found Sensor 1");
 //if (!sensors.getAddress(t2, 1)) Serial.println("No Found  Sensor 2");
 //if (!sensors.getAddress(t3, 2)) Serial.println("No Found Sensor  3");
 // show the addresses we found  on the bus
 //for (int ilosc =0; ilosc < sensors.getDeviceCount(); ilosc++)  {
   // Serial.print("Sensor "); Serial.print(ilosc+1);
    //Serial.print("  Address: ");
   // if (ilosc == 0) { printAddress(t1); Serial.println();
   // } else if (ilosc == 1) { printAddress(t2); Serial.println();
    //} else if (ilosc  == 2) { printAddress(t3); Serial.println();
   // }
 //}
  // set the resolution  to 9 bit per device
  sensors.setResolution(t1, precision);
  sensors.setResolution(t2, precision);
  sensors.setResolution(t3, precision);
  for (int ilosc =0; ilosc < sensors.getDeviceCount(); ilosc++)  {
    Serial.print("Sensor "); Serial.print(ilosc+1);
    Serial.print("  Resolution: ");
    if (ilosc == 0) { Serial.print(sensors.getResolution(t1),  DEC); Serial.println();
    } else if (ilosc == 1) { Serial.print(sensors.getResolution(t2),  DEC); Serial.println();
    } else if (ilosc == 2) { Serial.print(sensors.getResolution(t3),  DEC); Serial.println();
    }
  }
  // function to print a device  address
  //void printAddress(DeviceAddress deviceAddress) 
  //{ 
    //for (uint8_t i = 0; i < 8; i++)
    //{
    // zero pad the address if necessary
    //if (deviceAddress[i] < 16) Serial.print("0");
    //Serial.print(deviceAddress[i], HEX);
    //}
  //}
}
  // function to print the temperature for a device
  void  printTemperature(DeviceAddress deviceAddress)
  {
    float tempC = sensors.getTempC(deviceAddress);
    Serial.print("Temp : ");
    Serial.print(tempC);
    Serial.print(" Celcius  degrees ");
  }
    // function to print a device's resolution
    void printResolution(DeviceAddress  deviceAddress)
    {
      //  Serial.print("Resolution: ");
      //  Serial.print(sensors.getResolution(deviceAddress));
      //  Serial.println();
    }
    // main function to print information about a device
  void  printData(DeviceAddress deviceAddress)
  {
    Serial.print("Device Address:  ");
    //printAddress(deviceAddress);
    Serial.print(" ");
    printTemperature(deviceAddress);
    Serial.println();
  }
  /* Main function, calls the temperatures in a loop.
  */
  void loop()
  {
    // Open Svd to drain any residue for 10 seconds
  digitalWrite(SvdPin, HIGH);
  delay(DRAIN_TIME_MS);
  digitalWrite(SvdPin, LOW);


    // call sensors.requestTemperatures()  to issue a global temperature request to all devices on the bus
    Serial.print("Reading  DATA..."); sensors.requestTemperatures();
    Serial.println("DONE");
    
    // print the device information
    for (int ilosc =0; ilosc < sensors.getDeviceCount();  ilosc++) {
      Serial.print("Sensor "); Serial.print(ilosc+1); Serial.print("  ");
      if (ilosc == 0) { printData(t1);
      } else if (ilosc == 1) {  printData(t2);
      } else if (ilosc == 2) { printData(t3);
      }
    }
 if (licznik  == sensors.getDeviceCount()) {
    licznik = 0;    // reset counter
    //
    lcd.clear();  // clear screen on LCD
 }
  lcd.setCursor(0,0);
  lcd.print("Sensor Number "); lcd.print(licznik+1);
  lcd.setCursor(0,1);
  lcd.print(" Temp: ");
  if (licznik == 0) { lcd.print(sensors.getTempC(t1));  lcd.write((char)223); lcd.print("C  ");
  } else if (licznik == 1) { lcd.print(sensors.getTempC(t2));  lcd.write((char)223); lcd.print("C  ");
  } else if (licznik == 2) { lcd.print(sensors.getTempC(t3));  lcd.write((char)223); lcd.print("C  ");
  }
  Serial.print("Licznik=");  Serial.println(licznik);
  delay(1750);
  licznik++ ;

  
}

  
NOCOMNCVCCGNDINLED1PWRRelay Module