Żaden kod nie będzie pasował idealnie do tego co wykluło się w Twojej głowie. Zacznij od poradników jeśli chcesz się nauczyć programować lub zmień dział na dam pracę. Na pewno wrzucałem gdzieś to:
Kod:
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
//Piny przekaźników
const int PPIN1= LED_BUILTIN; //Tu definiujesz do jakiego pinu podlaczas przelacznik1 LED_BUILTIN=13 w UNO
const int PPIN2 = 3; //Tu definiujesz do jakiego pinu podlaczas przelacznik2
const int PPIN3 = 4; //Tu definiujesz do jakiego pinu podlaczas przelacznik3
const int PPIN4 = 5; //Tu definiujesz do jakiego pinu podlaczas przelacznik4
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
//Zmienne
float temp1, temp2, temp3, temp4; //zmienne dla DS18B20 jako liczby float
uint8_t ntemp1=22, ntemp2=24, ntemp3=25, ntemp4=30, htemp1=1,htemp2=2,htemp3=1,htemp4=2;// nastawy termperatur i zmienne histerezy dla temperatur jako liczby calkowite 8bit
unsigned long previousMillis; // zmienna do przechowywania liczby ms
uint8_t liczbaDS,setkims,sekundy,minuty,godziny, dni,flaga100ms, flaga1m,flaga1s, flaga1h, flaga1d;//zmienne 8bit do przechowywania czasu
uint16_t interwal=1000UL; //najmniejsza rozdzielczość zdarzeń wymagana w programie, tu przyjęto 1000ms, im mniejsza tym większy błąd czasu przy zliczaniu mintu/godzin itp.
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature DS18B20(&oneWire);
//FUNCJE
// Mamy 4 przekaźniki, więc napiszy do nich funkcje by było krócej w kodzie. Jak argumenty podamy nr przekaźnika, nastawa i histerezę temperatury oczekiwanej, temperatura odczytana
bool wlacz(uint8_t przekaznik, uint8_t ntemp, uint8_t hist, float temp) //funkcja zwraca 1 lub 0 do ustawienia przekaznika
{
uint8_t grzanie=1; //przekaznik wlacza grzanie dla 1, wlacza chlodzenie dla 0
if( digitalRead(przekaznik)&&grzanie) // sprawdzamy czy przekaźnik był włączony i w jakim trybie pracuje
{
if(temp>(ntemp+hist)) return 0;
else return 1;
}
else //jeśli przekaźnik był wyłączony
{
if(temp<(ntemp-hist)) return 1;
else return 0;
}
}
// ustawianie czasu dla każdego komponentu
void obslugaczasu(void)
{
unsigned long currentMillis = millis(); //zmienna systemowa zliczajaca milisekundy od uruchomienia?
if ((currentMillis - previousMillis) >= interwal) //jeśli minelo interwal ms czasu
{
previousMillis=currentMillis; //Reset licznika ms
flaga1s=1;
}
/*{
previousMillis=currentMillis; //Reset licznika ms
flaga100ms=1;
setkims++;
if (setkims>=9)
{
flaga1s=1;
setkims=0;
}
}*/
if(flaga1s) //gdy minęła sekunda
{
sekundy++;
if (sekundy>=60)
{
flaga1m=1;
sekundy=0;
}
}
if(flaga1m) //gdy minęła minuta
{
minuty++;
if (minuty>=60)
{
flaga1h=1;
minuty=0;
}
}
if(flaga1h) //gdy minęła minuta
{
godziny++;
if (godziny>=24)
{
flaga1d=1;
godziny=0;
}
}
if(flaga1d) //gdy minęła minuta
{
dni++;
}
}
String czas(void) //do testów można sobie skalibrować o ile czas się rozjeżdża na 1h czy dobę i zmodyfikować
{
String wydruk="";
if(dni<10) wydruk=wydruk+"0";
wydruk=wydruk+String(dni)+"d:";
if(godziny<10) wydruk=wydruk+"0";
wydruk=wydruk+String(godziny)+"g:";
if(minuty<10) wydruk=wydruk+"0";
wydruk=wydruk+String(minuty)+"m:";
if(sekundy<10) wydruk=wydruk+"0";
wydruk=wydruk+String(sekundy)+"s";
return wydruk;
}
/*
* The setup function. We only start the sensors here
*/
void setup(void)
{
//ustawiamy piny wyjsc:
pinMode(PPIN1, OUTPUT);
pinMode(PPIN2, OUTPUT);
pinMode(PPIN3, OUTPUT);
pinMode(PPIN4, OUTPUT);
// start serial port
Serial.begin(115200);
Serial.println("Dallas Temperature IC Control Library Demo");
//setWaitForConversion(0);//no wait for conversion, wait itsalve
// Start up the library
DS18B20.begin();
liczbaDS=DS18B20.getDeviceCount();
DS18B20.setWaitForConversion(0); //wylaczamy oczekiwanie na przeliczenie temperatury przez DS, odczytamy w innej sekundzie
DS18B20.requestTemperatures();
}
/*
* Main function, get and show the temperature
*/
void loop(void)
{
obslugaczasu();
if (flaga1s) //coś co zrobimy gdy piknie sekunda
{
Serial.println(czas());
if(sekundy%2==0) //coś co zrobimy co druga pełną sekunde
{
temp1=DS18B20.getTempCByIndex(0);
temp2=DS18B20.getTempCByIndex(1);
temp3=DS18B20.getTempCByIndex(2);
temp4=DS18B20.getTempCByIndex(3);
digitalWrite(PPIN1,wlacz(PPIN1, ntemp1, htemp1, temp1)) ;
digitalWrite(PPIN2,wlacz(PPIN2, ntemp2, htemp2, temp2)) ;
digitalWrite(PPIN3,wlacz(PPIN3, ntemp3, htemp3, temp3)) ;
digitalWrite(PPIN4,wlacz(PPIN4, ntemp4, htemp4, temp4)) ;
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
//należy używać funkcji bez delay itp. opóźnień
DS18B20.requestTemperatures();
Serial.println("DONE");
}
if(sekundy%3==0) //coś co zrobimy co trzecią pełną sekunde
{
// 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.
Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println( temp1);
Serial.print("Temperature for the device 2 (index 1) is: ");
Serial.println(temp2);
Serial.print("Temperature for the device 3 (index 2) is: ");
Serial.println(temp3);
Serial.print("Temperature for the device 4 (index 3) is: ");
Serial.println(temp4);
}
if(sekundy%5==0) //coś co zrobimy co piątą pełną sekunde
{
// 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.
Serial.print("stan przekaznika 1: ");
Serial.println( digitalRead(PPIN1));
Serial.print("stan przekaznika 2: ");
Serial.println( digitalRead(PPIN2));
Serial.print("stan przekaznika 3: ");
Serial.println( digitalRead(PPIN3));
Serial.print("stan przekaznika 4: ");
Serial.println( digitalRead(PPIN4));
}
}
if (flaga1m) //coś co zrobimy gdy piknie minuta
{
}
//zerujemy flagi
flaga1s=0;
flaga1m=0;
flaga1h=0;
flaga1d=0;
}
Obsługę wilgotności robi się analogicznie.
Jest kurs na Forbot i tysiące filmów na Youtube.