|  | 
|  | 1 | +#include <HT1621.h> | 
|  | 2 | +#define LCD_CS_PIN 13 | 
|  | 3 | +#define LCD_WR_PIN 11 | 
|  | 4 | +#define LCD_DATA_PIN 9 | 
|  | 5 | +HT1621 lcd; | 
|  | 6 | + | 
|  | 7 | +#include <Servo.h> | 
|  | 8 | +#include "DHT.h" | 
|  | 9 | + | 
|  | 10 | + | 
|  | 11 | +#define DHTPIN 8 | 
|  | 12 | +#define DHTTYPE DHT11 | 
|  | 13 | +DHT dht(DHTPIN, DHTTYPE); | 
|  | 14 | +float dht_humi; | 
|  | 15 | +float dht_temp; | 
|  | 16 | + | 
|  | 17 | +Servo cyglyk_s; | 
|  | 18 | +Servo motor_s; | 
|  | 19 | + | 
|  | 20 | +int slow_servo_state = 0; | 
|  | 21 | +int setted_slow_servo_state = 0; | 
|  | 22 | +long slow_servo_millis; | 
|  | 23 | +int slow_servo_speed = 20; | 
|  | 24 | + | 
|  | 25 | + | 
|  | 26 | +int motor_turn_time_h = 2; | 
|  | 27 | +unsigned long motor_turn_time_ms = 10800000;//60000; ///2 * 60 * 60 * 1000; | 
|  | 28 | + | 
|  | 29 | +const int c_s_pin = 4; | 
|  | 30 | +const int m_s_pin = 5; | 
|  | 31 | +const int fan_pin = 6; | 
|  | 32 | +const int lmp_pin = 7; | 
|  | 33 | +const int pot_pin = A0; | 
|  | 34 | +const int sbtn_pin= 12; | 
|  | 35 | + | 
|  | 36 | +int pot_val; | 
|  | 37 | +int setted_T; | 
|  | 38 | +unsigned long temp_millis; | 
|  | 39 | +unsigned long motor_millis; | 
|  | 40 | + | 
|  | 41 | +int motor_s_down_val = 165; | 
|  | 42 | +int motor_s_up_val = 15; | 
|  | 43 | +int cyglyk_s_down_val = 179; | 
|  | 44 | +int cyglyk_s_up_val = 1; | 
|  | 45 | +int motor_current_state = 1; | 
|  | 46 | + | 
|  | 47 | +byte u[8] =  | 
|  | 48 | +{ | 
|  | 49 | + 0b01010, | 
|  | 50 | + 0b00000, | 
|  | 51 | + 0b10001, | 
|  | 52 | + 0b10001, | 
|  | 53 | + 0b10001, | 
|  | 54 | + 0b10011, | 
|  | 55 | + 0b01101, | 
|  | 56 | + 0b00000 | 
|  | 57 | +}; | 
|  | 58 | + | 
|  | 59 | +void setup() { | 
|  | 60 | + Serial.begin(9600); | 
|  | 61 | + dht.begin(); | 
|  | 62 | + //-----PINS----- | 
|  | 63 | + pinMode(fan_pin,OUTPUT); | 
|  | 64 | + pinMode(lmp_pin,OUTPUT); | 
|  | 65 | + pinMode(pot_pin,INPUT); | 
|  | 66 | + pinMode(sbtn_pin,INPUT); | 
|  | 67 | + | 
|  | 68 | + cyglyk_s.attach(c_s_pin); | 
|  | 69 | + motor_s.attach(m_s_pin); | 
|  | 70 | + | 
|  | 71 | + //-----LCD------ | 
|  | 72 | + lcd.begin(LCD_CS_PIN, LCD_WR_PIN, LCD_DATA_PIN); | 
|  | 73 | + lcd.backlight(); | 
|  | 74 | + lcd.clear(); | 
|  | 75 | + lcd.print("incubi"); | 
|  | 76 | + lcd.setBatteryLevel(1); | 
|  | 77 | + delay(200); | 
|  | 78 | + lcd.setBatteryLevel(2); | 
|  | 79 | + delay(200); | 
|  | 80 | + lcd.setBatteryLevel(3); | 
|  | 81 | + delay(200); | 
|  | 82 | + lcd.setBatteryLevel(0); | 
|  | 83 | + | 
|  | 84 | + | 
|  | 85 | + //checking the motor | 
|  | 86 | + cyglyk_s.write(cyglyk_s_down_val); | 
|  | 87 | + delay(200); | 
|  | 88 | + cyglyk_s.write(cyglyk_s_up_val); | 
|  | 89 | + delay(200); | 
|  | 90 | + cyglyk_s.write(cyglyk_s_down_val); | 
|  | 91 | + delay(200); | 
|  | 92 | + lcd.clear(); | 
|  | 93 | +} | 
|  | 94 | + | 
|  | 95 | + | 
|  | 96 | +void loop() { | 
|  | 97 | + temp(); | 
|  | 98 | + temp_sazlayjy(); | 
|  | 99 | + humidity_controller(); | 
|  | 100 | + motor_s_turning(); | 
|  | 101 | + servo_write_slow();  | 
|  | 102 | + sbtn_motor_manual_control(); | 
|  | 103 | +} | 
|  | 104 | + | 
|  | 105 | + | 
|  | 106 | +// collect temperature info from sensor | 
|  | 107 | +void temp(){ | 
|  | 108 | + if ((temp_millis + 5000) < millis()){  | 
|  | 109 | + dht_humi = dht.readHumidity(); | 
|  | 110 | + dht_temp = dht.readTemperature(); | 
|  | 111 | + lcd.printCelsius(dht_temp); | 
|  | 112 | + | 
|  | 113 | + temp_millis = millis(); | 
|  | 114 | + } | 
|  | 115 | +} | 
|  | 116 | + | 
|  | 117 | + | 
|  | 118 | +// Control the temperature by switching heater on/off | 
|  | 119 | +void temp_sazlayjy() { | 
|  | 120 | + pot_val = analogRead(pot_pin); | 
|  | 121 | + setted_T = map(pot_val,0,1023,36,40); | 
|  | 122 | + int view_T = map(setted_T,36,39,0,3); | 
|  | 123 | + lcd.setBatteryLevel(view_T); | 
|  | 124 | +// // printing the elapsed time to turn the holder | 
|  | 125 | +// lcd.print(((motor_millis + motor_turn_time_ms) - millis())/1000); | 
|  | 126 | +// lcd.print(" "); | 
|  | 127 | +// lcd.print(setted_T); | 
|  | 128 | +// lcd.print("*C "); | 
|  | 129 | + if (setted_T > dht_temp ) { | 
|  | 130 | + digitalWrite(lmp_pin, 1); | 
|  | 131 | + } else if (setted_T < dht_temp) { | 
|  | 132 | + digitalWrite(lmp_pin, 0); | 
|  | 133 | + }  | 
|  | 134 | +} | 
|  | 135 | + | 
|  | 136 | + | 
|  | 137 | +// Control the humidity with servo and fan | 
|  | 138 | +void humidity_controller(){ | 
|  | 139 | + if (dht_humi > 50) { | 
|  | 140 | + cyglyk_s.write (cyglyk_s_up_val); | 
|  | 141 | + digitalWrite(fan_pin, 1); | 
|  | 142 | + } | 
|  | 143 | + else{ | 
|  | 144 | + cyglyk_s.write(cyglyk_s_down_val); | 
|  | 145 | + digitalWrite(fan_pin, 0); | 
|  | 146 | + } | 
|  | 147 | + if (dht_temp > 40){ | 
|  | 148 | + digitalWrite(fan_pin,1); | 
|  | 149 | + } else { | 
|  | 150 | + digitalWrite(fan_pin,0); | 
|  | 151 | + } | 
|  | 152 | +} | 
|  | 153 | + | 
|  | 154 | + | 
|  | 155 | +void motor_s_turning() { | 
|  | 156 | + if((motor_millis + motor_turn_time_ms) < millis()){ | 
|  | 157 | + if (motor_current_state == 1){ | 
|  | 158 | + // motor_s.write(motor_s_up_val); | 
|  | 159 | + setted_slow_servo_state = motor_s_up_val; | 
|  | 160 | + motor_current_state = 0; | 
|  | 161 | + } else { | 
|  | 162 | + setted_slow_servo_state = motor_s_down_val; | 
|  | 163 | + motor_current_state = 1; | 
|  | 164 | + } | 
|  | 165 | + motor_millis = millis(); | 
|  | 166 | + } | 
|  | 167 | +} | 
|  | 168 | + | 
|  | 169 | + | 
|  | 170 | +void servo_write_slow(){ | 
|  | 171 | + if (slow_servo_state <= setted_slow_servo_state){ | 
|  | 172 | + if (slow_servo_millis + slow_servo_speed < millis()){ | 
|  | 173 | + motor_s.write(slow_servo_state); | 
|  | 174 | + slow_servo_state++;  | 
|  | 175 | + slow_servo_millis = millis(); | 
|  | 176 | + } | 
|  | 177 | + } | 
|  | 178 | + if (slow_servo_state >= setted_slow_servo_state){ | 
|  | 179 | + if (slow_servo_millis + slow_servo_speed < millis()){ | 
|  | 180 | + motor_s.write(slow_servo_state); | 
|  | 181 | + slow_servo_state--;  | 
|  | 182 | + slow_servo_millis = millis(); | 
|  | 183 | + }  | 
|  | 184 | + } | 
|  | 185 | +} | 
|  | 186 | + | 
|  | 187 | + | 
|  | 188 | +// change egg holder motor state by pressing button | 
|  | 189 | +void sbtn_motor_manual_control() { | 
|  | 190 | + int sbtn_state = digitalRead(sbtn_pin); | 
|  | 191 | + if (sbtn_state == 1){ | 
|  | 192 | + if (motor_current_state == 1){ | 
|  | 193 | + motor_current_state = 0; | 
|  | 194 | + setted_slow_servo_state = motor_s_up_val;  | 
|  | 195 | + } else { | 
|  | 196 | + motor_current_state = 1; | 
|  | 197 | + setted_slow_servo_state = motor_s_down_val; | 
|  | 198 | + } | 
|  | 199 | + | 
|  | 200 | + slow_servo_millis = 0; | 
|  | 201 | + motor_millis = millis(); | 
|  | 202 | + delay(500); | 
|  | 203 | + } | 
|  | 204 | +} | 
0 commit comments