From 62b25a964326869e3f15ddb866e22e084eeb3f37 Mon Sep 17 00:00:00 2001 From: Mike Date: Sat, 2 Mar 2024 11:17:33 +0500 Subject: [PATCH] some updates of incubator --- incubator-nano/naval_incubator_HT1621.cpp | 204 ++++++++++++++++++ ...cubator.cpp => naval_incubator_LCDI2C.cpp} | 40 ++-- 2 files changed, 223 insertions(+), 21 deletions(-) create mode 100755 incubator-nano/naval_incubator_HT1621.cpp rename incubator-nano/{naval_incubator.cpp => naval_incubator_LCDI2C.cpp} (84%) diff --git a/incubator-nano/naval_incubator_HT1621.cpp b/incubator-nano/naval_incubator_HT1621.cpp new file mode 100755 index 0000000..e0f575b --- /dev/null +++ b/incubator-nano/naval_incubator_HT1621.cpp @@ -0,0 +1,204 @@ +#include +#define LCD_CS_PIN 13 +#define LCD_WR_PIN 11 +#define LCD_DATA_PIN 9 +HT1621 lcd; + +#include +#include "DHT.h" + + +#define DHTPIN 8 +#define DHTTYPE DHT11 +DHT dht(DHTPIN, DHTTYPE); +float dht_humi; +float dht_temp; + +Servo cyglyk_s; +Servo motor_s; + +int slow_servo_state = 0; +int setted_slow_servo_state = 0; +long slow_servo_millis; +int slow_servo_speed = 20; + + +int motor_turn_time_h = 2; +unsigned long motor_turn_time_ms = 10800000;//60000; ///2 * 60 * 60 * 1000; + +const int c_s_pin = 4; +const int m_s_pin = 5; +const int fan_pin = 6; +const int lmp_pin = 7; +const int pot_pin = A0; +const int sbtn_pin= 12; + +int pot_val; +int setted_T; +unsigned long temp_millis; +unsigned long motor_millis; + +int motor_s_down_val = 165; +int motor_s_up_val = 15; +int cyglyk_s_down_val = 179; +int cyglyk_s_up_val = 1; +int motor_current_state = 1; + +byte u[8] = +{ + 0b01010, + 0b00000, + 0b10001, + 0b10001, + 0b10001, + 0b10011, + 0b01101, + 0b00000 +}; + +void setup() { + Serial.begin(9600); + dht.begin(); + //-----PINS----- + pinMode(fan_pin,OUTPUT); + pinMode(lmp_pin,OUTPUT); + pinMode(pot_pin,INPUT); + pinMode(sbtn_pin,INPUT); + + cyglyk_s.attach(c_s_pin); + motor_s.attach(m_s_pin); + + //-----LCD------ + lcd.begin(LCD_CS_PIN, LCD_WR_PIN, LCD_DATA_PIN); + lcd.backlight(); + lcd.clear(); + lcd.print("incubi"); + lcd.setBatteryLevel(1); + delay(200); + lcd.setBatteryLevel(2); + delay(200); + lcd.setBatteryLevel(3); + delay(200); + lcd.setBatteryLevel(0); + + + //checking the motor + cyglyk_s.write(cyglyk_s_down_val); + delay(200); + cyglyk_s.write(cyglyk_s_up_val); + delay(200); + cyglyk_s.write(cyglyk_s_down_val); + delay(200); + lcd.clear(); +} + + +void loop() { + temp(); + temp_sazlayjy(); + humidity_controller(); + motor_s_turning(); + servo_write_slow(); + sbtn_motor_manual_control(); +} + + +// collect temperature info from sensor +void temp(){ + if ((temp_millis + 5000) < millis()){ + dht_humi = dht.readHumidity(); + dht_temp = dht.readTemperature(); + lcd.printCelsius(dht_temp); + + temp_millis = millis(); + } +} + + +// Control the temperature by switching heater on/off +void temp_sazlayjy() { + pot_val = analogRead(pot_pin); + setted_T = map(pot_val,0,1023,36,40); + int view_T = map(setted_T,36,39,0,3); + lcd.setBatteryLevel(view_T); +// // printing the elapsed time to turn the holder +// lcd.print(((motor_millis + motor_turn_time_ms) - millis())/1000); +// lcd.print(" "); +// lcd.print(setted_T); +// lcd.print("*C "); + if (setted_T > dht_temp ) { + digitalWrite(lmp_pin, 1); + } else if (setted_T < dht_temp) { + digitalWrite(lmp_pin, 0); + } +} + + +// Control the humidity with servo and fan +void humidity_controller(){ + if (dht_humi > 50) { + cyglyk_s.write (cyglyk_s_up_val); + digitalWrite(fan_pin, 1); + } + else{ + cyglyk_s.write(cyglyk_s_down_val); + digitalWrite(fan_pin, 0); + } + if (dht_temp > 40){ + digitalWrite(fan_pin,1); + } else { + digitalWrite(fan_pin,0); + } +} + + +void motor_s_turning() { + if((motor_millis + motor_turn_time_ms) < millis()){ + if (motor_current_state == 1){ + // motor_s.write(motor_s_up_val); + setted_slow_servo_state = motor_s_up_val; + motor_current_state = 0; + } else { + setted_slow_servo_state = motor_s_down_val; + motor_current_state = 1; + } + motor_millis = millis(); + } +} + + +void servo_write_slow(){ + if (slow_servo_state <= setted_slow_servo_state){ + if (slow_servo_millis + slow_servo_speed < millis()){ + motor_s.write(slow_servo_state); + slow_servo_state++; + slow_servo_millis = millis(); + } + } + if (slow_servo_state >= setted_slow_servo_state){ + if (slow_servo_millis + slow_servo_speed < millis()){ + motor_s.write(slow_servo_state); + slow_servo_state--; + slow_servo_millis = millis(); + } + } +} + + +// change egg holder motor state by pressing button +void sbtn_motor_manual_control() { + int sbtn_state = digitalRead(sbtn_pin); + if (sbtn_state == 1){ + if (motor_current_state == 1){ + motor_current_state = 0; + setted_slow_servo_state = motor_s_up_val; + } else { + motor_current_state = 1; + setted_slow_servo_state = motor_s_down_val; + } + + slow_servo_millis = 0; + motor_millis = millis(); + delay(500); + } +} diff --git a/incubator-nano/naval_incubator.cpp b/incubator-nano/naval_incubator_LCDI2C.cpp similarity index 84% rename from incubator-nano/naval_incubator.cpp rename to incubator-nano/naval_incubator_LCDI2C.cpp index 3ca2119..004c770 100755 --- a/incubator-nano/naval_incubator.cpp +++ b/incubator-nano/naval_incubator_LCDI2C.cpp @@ -8,7 +8,7 @@ LiquidCrystal_I2C lcd(0x27, 16, 2); #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); float dht_humi; -unsigned int dht_temp; +float dht_temp; Servo cyglyk_s; Servo motor_s; @@ -20,7 +20,7 @@ int slow_servo_speed = 20; int motor_turn_time_h = 2; -long motor_turn_time_ms = 10800000; ///2 * 60 * 60 * 1000; +unsigned long motor_turn_time_ms = 60000;//10800000; ///2 * 60 * 60 * 1000; const int c_s_pin = 4; const int m_s_pin = 5; @@ -31,8 +31,8 @@ const int sbtn_pin= 12; int pot_val; int setted_T; -long temp_millis; -long motor_millis; +unsigned long temp_millis; +unsigned long motor_millis; int motor_s_down_val = 165; int motor_s_up_val = 15; @@ -64,7 +64,7 @@ void setup() { lcd.setCursor(4,0); lcd.print("Salam!!"); lcd.setCursor(0,1); - lcd.print("Zayebal ;)"); + lcd.print("Kubi ;)"); //-----PINS----- pinMode(fan_pin,OUTPUT); @@ -98,26 +98,20 @@ void loop() { // collect temperature info from sensor void temp(){ - if ((temp_millis + 4000) < millis()){ + if ((temp_millis + 5000) < millis()){ dht_humi = dht.readHumidity(); dht_temp = dht.readTemperature(); lcd.setCursor(0,0); - lcd.print ("Tmp "); + lcd.print ("T "); lcd.print (dht_temp); - lcd.print ("*C "); + lcd.print ("*C "); lcd.setCursor (0,1); - lcd.print ("Cyg "); + lcd.print ("H "); lcd.print (dht_humi); lcd.print ("%"); lcd.setCursor (7,1); lcd.print (" "); - Serial.print ("Temp = "); - Serial.print (dht_temp); - Serial.print (" *C "); - Serial.print ("Cyglylyk "); - Serial.print (dht_humi); - Serial.println (" %"); temp_millis = millis(); } } @@ -127,7 +121,7 @@ void temp(){ void temp_sazlayjy() { pot_val = analogRead(pot_pin); setted_T = map(pot_val,0,1023,30,41); - lcd.setCursor(9,0); + lcd.setCursor(11,0); // printing the elapsed time to turn the holder lcd.print(((motor_millis + motor_turn_time_ms) - millis())/1000); @@ -135,11 +129,10 @@ void temp_sazlayjy() { lcd.setCursor(12,1); lcd.print(setted_T); lcd.print("*C "); - Serial.println(setted_T); if (setted_T > dht_temp ) { - digitalWrite(lmp_pin, 0); - } else if (setted_T < dht_temp) { digitalWrite(lmp_pin, 1); + } else if (setted_T < dht_temp) { + digitalWrite(lmp_pin, 0); } } @@ -154,6 +147,11 @@ void humidity_controller(){ cyglyk_s.write(cyglyk_s_down_val); digitalWrite(fan_pin, 0); } + if (dht_temp > 40){ + digitalWrite(fan_pin,1); + } else { + digitalWrite(fan_pin,0); + } } @@ -178,7 +176,7 @@ void servo_write_slow(){ motor_s.write(slow_servo_state); slow_servo_state++; slow_servo_millis = millis(); - } + } } if (slow_servo_state >= setted_slow_servo_state){ if (slow_servo_millis + slow_servo_speed < millis()){ @@ -206,4 +204,4 @@ void sbtn_motor_manual_control() { motor_millis = millis(); delay(500); } -} \ No newline at end of file +}