macca-nz

Humidity and relay

Mar 5th, 2024 (edited)
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.48 KB | Source Code | 0 0
  1. #include <virtuabotixRTC.h>
  2. #include <LiquidCrystal_I2C.h>
  3. #include <Wire.h>
  4. virtuabotixRTC myRTC(6, 7, 8);
  5. const byte sensorPin = A0, relay = 4;
  6. byte lastSecond, humidity;
  7. bool relayState = 0, lastRelayState = 0;
  8. LiquidCrystal_I2C lcd(0X27, 16, 2);
  9.  
  10. //Update display every second
  11. byte updateLCD(int val){
  12.   byte h = map(val,520,200,0,100);
  13.   lcd.setCursor(0, 0);
  14.   lcd.print("Temps:");
  15.   lcd.setCursor(7,0);
  16.   char buf[10];
  17.   sprintf(buf,"%02u:%02:%02", myRTC.hours, myRTC.minutes, myRTC.seconds);
  18.   lcd.print(buf);
  19.   lcd.setCursor(0,1);
  20.   lcd.print("Humidity:");
  21.   lcd.setCursor(10,1);
  22.   lcd.print(humidity,DEC);
  23.   Serial.print(myRTC.hours);
  24.   Serial.print(myRTC.minutes);
  25.   Serial.println(myRTC.seconds);
  26.   lastSecond = myRTC.seconds;
  27.   return h;
  28. }
  29.  
  30. void setup(void){
  31.   Serial.begin(9600);
  32.   pinMode(relay, OUTPUT);
  33.   digitalWrite(relay, relayState);
  34.   myRTC.setDS1302Time(00, 06, 14, 4, 22, 02, 2024);
  35.   lastSecond = myRTC.seconds;
  36.   lcd.init();
  37.   lcd.backlight();
  38.   lcd.setCursor(0, 0);
  39.   lcd.print("Saha");
  40.   delay(200);
  41.   lcd.clear();
  42. }
  43.  
  44. void loop(void) {
  45.   myRTC.updateTime();
  46.  
  47.   //Every Second update LCD
  48.   if(myRTC.seconds != lastSecond){
  49.     int sensorValue = analogRead(sensorPin);
  50.     humidity = updateLCD(sensorValue);
  51.   }
  52.  
  53.   if ((myRTC.month == 12 ||myRTC.month == 1||myRTC.month == 2) &&
  54.       (myRTC.dayofweek == 4)&&
  55.       (myRTC.hours >= 12 && myRTC.hours <= 14)){
  56.         if(humidity <= 80){
  57.           relayState = 1;
  58.         }else{
  59.           relayState = 0;
  60.         }
  61.       }
  62.   if ((myRTC.month >= 3 && myRTC.month <=  5)&&
  63.       (myRTC.dayofweek == 1 || myRTC.dayofweek == 4)&&
  64.       (myRTC.hours == 16) && (myRTC.minutes == 0)) {
  65.         if(humidity <= 90){
  66.             relayState = 1;
  67.           }else{
  68.             relayState = 0;
  69.           }
  70.       }
  71.   if ((myRTC.month >= 6 && myRTC.month <= 8)&&
  72.       (myRTC.dayofweek == 1|| myRTC.dayofweek == 3|| myRTC.dayofweek == 6)&&
  73.       (myRTC.hours == 16) && (myRTC.minutes == 0)){
  74.         if(humidity <= 90){
  75.           relayState = 1;
  76.         }else{
  77.           relayState = 0;
  78.         }
  79.       }
  80.   if ((myRTC.month >= 9 && myRTC.month <= 11)&&
  81.       (myRTC.dayofweek == 1 || myRTC.dayofweek == 4)&&
  82.       (myRTC.hours == 16) && (myRTC.minutes == 0)){
  83.         if(humidity <= 90){
  84.           relayState = 1;
  85.         }else{
  86.           relayState = 0;
  87.         }
  88.       }
  89.   if(relayState != lastRelayState){
  90.     digitalWrite(relay, relayState);
  91.   }
  92.   lastRelayState = relayState;
  93. }
  94.  
  95.  
Advertisement
Add Comment
Please, Sign In to add comment