Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Date and time functions using a DS3231 RTC connected via I2C and Wire Lib
- #include <Wire.h>
- #include <RTClib.h>
- #include <Time.h>
- #include <TimeAlarms.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <LiquidCrystal_I2C.h>
- #define BACKLIGHT_PIN 3
- LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7);
- RTC_DS1307 RTC;
- #define ONE_WIRE_BUS 2
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- int nastaw = 24; // ustaw temperature
- int przekaznik = 3; // pin przekaznika temp
- int lightRelay = 4; // pin przkeaznika led
- int Menu=0;
- int bstart=A1;
- int stanBstart=0;
- // Start Time
- int sHour = 12;
- int sMinute = 0;
- int sSecond = 0;
- // End Time
- int eHour = 14;
- int eMinute = 0;
- int eSecond = 0;
- void setup() {
- // Set the relay to off immediately
- digitalWrite(lightRelay, HIGH);
- pinMode(przekaznik,OUTPUT);
- pinMode(lightRelay, OUTPUT);
- lcd.begin (16,2);
- lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
- lcd.setBacklight(HIGH);
- // Serial.print("Regulator Temp:");
- sensors.begin();
- Wire.begin();
- RTC.begin();
- //Notify if the RTC isn't running
- if (! RTC.isrunning())
- {
- Serial.println("RTC is NOT running");
- }
- // Get time from RTC
- DateTime current = RTC.now();
- DateTime compiled = DateTime(__DATE__, __TIME__);
- if (current.unixtime() < compiled.unixtime()) {
- Serial.println("RTC is older than compile time! Updating");
- RTC.adjust(DateTime(__DATE__, __TIME__));
- }
- // Use RTC time to set the start time
- setTime(sHour, sMinute, sSecond, current.day(), current.month(), current.year());
- time_t s = now();
- // Use RTC time to set the end time
- setTime(eHour, eMinute, eSecond, current.day(), current.month(), current.year());
- time_t e = now();
- // Use RTC time to set the current time
- setTime(current.hour(), current.minute(), current.second(), current.day(), current.month(), current.year());
- time_t n = now();
- // Test if grow light should be on
- if (s <= n && n <= e) {
- digitalWrite(lightRelay, LOW); // Sets the grow light "on"
- }
- Alarm.alarmRepeat(sHour, sMinute, sSecond, LightOn);
- Alarm.alarmRepeat(eHour, eMinute, eSecond, LightOff);
- }
- void wys(void){
- sensors.requestTemperatures();
- //Serial.setCursor(0, 2);
- // lcd.print("T=");
- // lcd.print(sensors.getTempCByIndex(0));
- // lcd.print(" ");
- // lcd.print("N=");
- // lcd.print(nastaw);
- // lcd.print("Start");
- // lcd.print(sHour);
- // lcd.print(sMinute);
- // lcd.print(" ");
- // lcd.print("End");
- // lcd.print(eHour);
- // lcd.print(eMinute);
- // lcd.println();
- if (sensors.getTempCByIndex(0) < (nastaw)) {
- digitalWrite(2,LOW);
- } else {
- digitalWrite(2,HIGH);
- }
- }
- void loop() {
- DateTime now = RTC.now();
- setTime(now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
- sensors.requestTemperatures();
- // lcd.print("Current time: ");
- // Serial.print(now.year(), DEC);
- // Serial.print('/');
- // Serial.print(now.month(), DEC);
- // Serial.print('/');
- // lcd.print(now.day(), DEC);
- // lcd.print(' ');
- // lcd.print(now.hour(), DEC);
- // lcd.print(':');
- // lcd.print(now.minute(), DEC);
- // lcd.print(':');
- // lcd.print(now.second(), DEC);
- // lcd.println();
- //Serial.setCursor(0, 2);
- // lcd.print("S");
- //lcd.print(sHour);
- // lcd.print(":");
- // lcd.print(sMinute);
- // lcd.setCursor(8,0);
- // lcd.print("T=");
- // lcd.print(sensors.getTempCByIndex(0));
- // lcd.print(" ");
- // lcd.setCursor(0,2);
- // lcd.print("E");
- // lcd.print(eHour);
- // lcd.print(":");
- // lcd.print(eMinute);
- // lcd.setCursor(8,1);
- // lcd.print("N=");
- // lcd.print(nastaw);
- // lcd.setCursor(0,2);
- if (sensors.getTempCByIndex(0) < (nastaw)) {
- digitalWrite(2,LOW);
- } else {
- digitalWrite(2,HIGH);
- }
- Alarm.delay(1000);
- if (lightRelay, HIGH)
- {
- Serial.print("ON");
- }
- else {
- Serial.print("OFF");
- }
- Serial.print(' ');
- Serial.print(' ');
- stanBstart=analogRead(bstart);
- //Serial.println(stanBstart);
- // przyciski czasu start
- // Godzina
- if (stanBstart>680 && stanBstart<690) // +
- {
- Menu=Menu+1;
- }
- if (stanBstart>160 && stanBstart<170) //-
- {
- Menu=Menu-1;
- }
- if(Menu<0)
- {
- Menu=5;
- }
- if (Menu==0)
- {
- lcd.clear();
- lcd.print("S");
- lcd.print(sHour);
- lcd.print(":");
- lcd.print(sMinute);
- lcd.setCursor(8,0);
- lcd.print("T=");
- lcd.print(sensors.getTempCByIndex(0));
- lcd.print(" ");
- lcd.setCursor(0,2);
- lcd.print("E");
- lcd.print(eHour);
- lcd.print(":");
- lcd.print(eMinute);
- lcd.setCursor(8,1);
- lcd.print("N=");
- lcd.print(nastaw);
- lcd.setCursor(0,2);
- }
- if (Menu==1)
- {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Set Temp");
- lcd.setCursor(0,1);
- lcd.print(nastaw);
- if (stanBstart>60 && stanBstart<65) // +
- {
- nastaw=nastaw+1;
- }
- if (stanBstart>10 && stanBstart<15) //-
- {
- nastaw=nastaw-1;
- }
- }
- if (Menu==2)
- {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Set godzina startu");
- lcd.setCursor(0,1);
- lcd.print("Start");
- lcd.print("=");
- lcd.print(sHour);
- lcd.print(":");
- lcd.print(sMinute);
- if (stanBstart>60 && stanBstart<65) // +
- {
- sHour= sHour+1;
- }
- if (stanBstart>10 && stanBstart<15) //-
- {
- sHour= sHour-1;
- }
- }
- if (Menu==3)
- {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Set minuta startu");
- lcd.setCursor(0,1);
- lcd.print("Start");
- lcd.print("=");
- lcd.print(sHour);
- lcd.print(":");
- lcd.print(sMinute);
- if (stanBstart>60 && stanBstart<65) // +
- {
- sMinute=sMinute+1;
- }
- if (stanBstart>10 && stanBstart<15) //-
- {
- sMinute= sMinute-1;
- }
- }
- if (Menu==4)
- {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Set godzina konca");
- lcd.setCursor(0,1);
- lcd.print("End");
- lcd.print("=");
- lcd.print(eHour);
- lcd.print(":");
- lcd.print(eMinute);
- if (stanBstart>60 && stanBstart<65) // +
- {
- eHour= eHour+1;
- }
- if (stanBstart>10 && stanBstart<15) //-
- {
- eHour= eHour-1;
- }
- }
- if (Menu==5)
- {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Set minuta konca");
- lcd.setCursor(0,1);
- lcd.print("End");
- lcd.print("=");
- lcd.print(sHour);
- lcd.print(":");
- lcd.print(sMinute);
- if (stanBstart>60 && stanBstart<65) // +
- {
- eMinute=eMinute+1;
- }
- if (stanBstart>10 && stanBstart<15) //-
- {
- eMinute= eMinute-1;
- }
- }
- if(Menu>5)
- {
- Menu=0;
- }
- }
- void LightOn() {
- Serial.println("Turning Light On");
- digitalWrite(lightRelay, LOW);
- }
- void LightOff() {
- Serial.println("Turning Light Off");
- digitalWrite(lightRelay, HIGH);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement