My school has an egg incubator project so I decided to try to automate it but randomly the screen flashes random symbols and numbers It also had an issue where it would randomly freeze. Also, I am sure some things are wrong with my code because I just started learning C++ 1 week ago.
Any help is appreciated,
Thanks!
I am also still learning how to use Arduino forums, so if there are some things I am doing wrong, sorry!
The code has random notes because I pieced it together from different pieces of code.
Here is the code:
* DHT11 Sensor Reader
* This sketch reads temperature and humidity data from the DHT11 sensor and prints the values to the serial port.
// Include the DHT11 library for interfacing with the sensor.
#include "CSE_MillisTimer.h"
#include "Wire.h"
#include "Servo.h"
#include "DHT11.h"
#include "LiquidCrystal.h"
#include "Stepper.h"
#define TSWITCH 12
#define tempPin A0
#define HumLight A1
#define TempLight A2
int Plus = 1;
int turns = 0;
DHT11 dht11(13);
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
CSE_MillisTimer EggTurnTimer(600000); // should be 17280000
Servo myservo;
long days = 0;
long hour = 0;
long minute = 0;
long second = 0;
int maxTemp = 100.4;
int minTemp = 98.6;
byte Temperature[] = {
B00100,
B01010,
B01010,
B01110,
B01110,
B11111,
B11111,
B01110
};
byte Humidity[] = {
B00100,
B00100,
B01010,
B01010,
B10001,
B10001,
B10001,
B01110
};
byte Bulb[] = {
B00000,
B01110,
B10001,
B10001,
B10001,
B01110,
B01110,
B01110
};
byte Degrees[] = {
B00111,
B00101,
B00111,
B00000,
B00000,
B00000,
B00000,
B00000
};
// Create a timer instance
// Create an instance of the DHT11 cl;ass.
// - For Arduino: Connect the sensor to Digital I/O Pin 2.
// - For ESP32: Connect the sensor to pin GPIO2 or P2.
// - For ESP8266: Connect the sensor to GPIO2 or D4.
void setup() {
// Initialize serial communication to allow debugging and data readout.
// Using a baud rate of 9600 bps.
Serial.begin(9600);
pinMode(TSWITCH, OUTPUT);
pinMode(tempPin, INPUT);
pinMode(HumLight, OUTPUT);
pinMode(TempLight, OUTPUT);
EggTurnTimer.start();
lcd.begin(16, 2);
myservo.attach(10);
lcd.createChar(4, Temperature);
lcd.createChar(1, Humidity);
lcd.createChar(2, Bulb);
lcd.createChar(3, Degrees);
lcd.setCursor(0, 0);
lcd.print(" EGG ");
lcd.setCursor(0, 2);
lcd.print(" INCUBATOR ");
delay(4000);
lcd.clear();
myservo.write(45); // Move to zero degrees
// Uncomment the line below to set a custom delay between sensor readings (in milliseconds).
// dht11.setDelay(500); // Set this to the desired delay. Default is 500ms.
}
void loop() {
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.print(second);
Serial.print("(");
Serial.print(days);
lcd.print(")");
second ++; //Clock
if(second == 60){
minute ++;
second = 0;
}
if(minute == 60){
hour ++;
minute = 0;
}
if(hour == 24){
days ++;
hour = 0;
}
if (EggTurnTimer.isElapsed()) { // Check if the timer has elapsed.
myservo.write(1); // Move to 180 degrees
delay(1000);
myservo.write(90);
turns++;
EggTurnTimer.start();
}
lcd.setCursor(0, 1);
lcd.print(hour);
lcd.print(":");
lcd.print(minute);
lcd.print(":");
lcd.print(second);
lcd.print("(");
lcd.print(days);
lcd.print(")");
lcd.setCursor(12, 1);
lcd.print("R:");
lcd.print(turns);
if(turns == 5){
turns = 0;
}
int tempReading = analogRead(tempPin);
// This is OK
double tempK = log(10000.0 * ((1024.0 / tempReading - 1)));
tempK = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * tempK * tempK)) * tempK); // Temp Kelvin
float tempC = tempK - 273.15; // Convert Kelvin to Celcius
float tempF = (tempC * 9.0) / 5.0 + 32.0; // Convert Celcius to Fahrenheit
//Serial.println(tempC);
int temperature = 0;
int humidity = 0;
// Attempt to read the temperature and humidity values from the DHT11 sensor.
int result = dht11.readTemperatureHumidity(temperature, humidity);
// Check the results of the readings.
// If the reading is successful, print the temperature and humidity values.
// If there are errors, print the appropriate error messages.
if (result == 0) {
Serial.print("Temperature: ");
Serial.print(tempF);
Serial.print(" °F\tHumidity: ");
Serial.print(humidity);
Serial.println(" %");
lcd.setCursor(0, 0);
lcd.write(4);
lcd.print(tempC);
lcd.setCursor(6, 0);
lcd.write(3);
lcd.print("C ");
lcd.setCursor(9, 0);
lcd.write(1);
lcd.print(humidity);
lcd.setCursor(12, 0);
lcd.print("%");
} else {
// Print error message based on the error code.
Serial.println(DHT11::getErrorString(result));
}
if (tempF >= maxTemp) { // Chicken Eggs should be incubated from 100-102 F
digitalWrite(TSWITCH, LOW);
lcd.setCursor(14, 0);
lcd.print(" ");
}
if (tempF <= minTemp) {
digitalWrite(TSWITCH, HIGH);
lcd.setCursor(14, 0);
lcd.write(2);
}
if (humidity >= 60) { digitalWrite(HumLight, HIGH); } // Should be 60
if (humidity <= 55) { digitalWrite(HumLight, LOW); } // Should be 55
if (humidity <= 30) { digitalWrite(HumLight, HIGH); } // Should be 30
if (humidity >= 50) { digitalWrite(HumLight, LOW); } // Should be 50
if (tempF >= 104) { digitalWrite(TempLight, HIGH); } // Should be 103
if (tempF <= 102) { digitalWrite(TempLight, LOW); } // Should be 102
if (tempF <= 97) { digitalWrite(TempLight, HIGH); } // Should be 96
if (tempF >= 99) { digitalWrite(TempLight, LOW); } // Should be 100
delay(500);
}