I made a egg incubator project but randomly the screen flashes random numbers and symbols any help is greatly apprieciated

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);
}

Checking the schematics first, then the code, is a wish here.

1 Like

Welcome! How to Get the Right Help Faster:

You can spend weeks spinning your wheels, or you might get lucky and solve your problem quickly. To avoid unnecessary delays, it’s crucial to provide an annotated schematic of your circuit as you have it wired, showing all connections, including power, ground, and supplies.

Why Detailed Information Matters:

  • Annotated Schematics: These are essential because they show exactly how your circuit is set up. Without them, it's difficult for anyone to understand what you’ve done, which makes troubleshooting nearly impossible. Fritzing diagrams or unclear pictures are not enough.
  • Technical Information: Many modules look similar and may even have the same name, but they can function differently. This is why we always ask for links to detailed technical information—not just sales pages like those on Amazon, which often lack the specifics we need.
  • Show All Connections: It’s important to include every connection, especially power and ground, in your schematic. Missing these details makes it hard to determine if a setup issue might be causing your problem.

If you watch the serial monitor, you might get a warning... with a bunch of (seemingly) misplaced characters... which leads me to believe your randomness is in the code.

0:0:0(0Error 253 Reading from DHT11 timed out.
0:0:1(0Error 253 Reading from DHT11 timed out.
0:0:2(0Error 253 Reading from DHT11 timed out.

Also, look for LCD cursor placement before printing. Some of your LCD text is being printed over, leaving extraneous characters on the LCD.

And... you are only giving hours, minutes and seconds one character width... try padding these numbers when less than 10 with a zero, like this...

  lcd.setCursor(0, 1);
  if (hour < 10) lcd.print(0);
  lcd.print(hour);
  lcd.print(":");
  if (minute < 10) lcd.print(0);
  lcd.print(minute);
  lcd.print(":");
  if (second < 10) lcd.print(0);
  lcd.print(second);
  lcd.print(" (");
  lcd.print(days);
  lcd.print(") ");
1 Like

Here is where you can find one "random" character in the LCD... that last line should be Serial.print(")"); rather than lcd.print(")");

sketch.ino for wokwi
//* 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;
#define servoPin 10

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(servoPin);
  lcd.createChar(1, Humidity);
  lcd.createChar(2, Bulb);
  lcd.createChar(3, Degrees);
  lcd.createChar(4, Temperature);
  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() {
  if (hour < 10) Serial.print(0);
  Serial.print(hour);
  Serial.print(":");
  if (minute < 10) Serial.print(0);
  Serial.print(minute);
  Serial.print(":");
  if (second < 10) Serial.print(0);
  Serial.print(second);
  Serial.print("(");
  if (days < 10) Serial.print(0);
  Serial.print(days);
  Serial.print(")\t");

  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(180);            // Move to 180 degrees
    delay(1000);
    myservo.write(90);
    turns++;
    EggTurnTimer.start();
  }

  lcd.setCursor(0, 1);
  if (hour < 10) lcd.print(0);
  lcd.print(hour);
  lcd.print(":");
  if (minute < 10) lcd.print(0);
  lcd.print(minute);
  lcd.print(":");
  if (second < 10) lcd.print(0);
  lcd.print(second);
  lcd.print("(");
  if (days < 10) lcd.print(0);
  lcd.print(days);
  lcd.print(")");

  lcd.setCursor(13, 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:");
    if (tempF > 100) Serial.print(" ");
    if (tempF > 10 && tempF <= 100) Serial.print("  ");
    if (tempF > 0 && tempF <= 10) Serial.print("   ");
    if (tempF > -10 && tempF <= 0) Serial.print("  ");
    if (tempF > -100 && tempF <= -10) Serial.print(" ");
    Serial.print(tempF);
    Serial.print("°F\tHumidity:");
    if (humidity < 10) Serial.print(" ");
    Serial.print(humidity);
    Serial.println("%");

    lcd.setCursor(0, 0);
    lcd.write(4);
    if (tempC >  100) lcd.print(" ");
    if (tempC >=  10 && tempC < 100) lcd.print("  ");
    if (tempC >=   0 && tempC <  10) lcd.print("   ");
    if (tempC >= -10 && tempC <   0) lcd.print("  ");
    if (tempC >= -100 && tempC < -10) lcd.print(" ");
    lcd.print(tempC);
    lcd.setCursor(8, 0);
    lcd.write(3);
    lcd.print("C");
    lcd.setCursor(11, 0);
    lcd.write(1);
    if (humidity < 10) lcd.print(" ");
    lcd.print(humidity);
    lcd.setCursor(14, 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(15, 0);
    lcd.print(" ");
  }

  if (tempF <= minTemp) {
    digitalWrite(TSWITCH, HIGH);
    lcd.setCursor(15, 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);
}
diagram.json for wokwi
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 4.8, "left": -0.5, "attrs": {} },
    { "type": "wokwi-lcd1602", "id": "lcd1", "top": -236.57, "left": -22.4, "attrs": {} },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": -32.2,
      "left": -57.6,
      "attrs": { "color": "green" }
    },
    { "type": "wokwi-dht22", "id": "dht1", "top": -86.1, "left": -120.6, "attrs": {} },
    { "type": "wokwi-servo", "id": "servo1", "top": -107.6, "left": 201.6, "attrs": {} },
    { "type": "wokwi-vcc", "id": "vcc1", "top": -95.24, "left": 172.8, "attrs": {} },
    { "type": "wokwi-potentiometer", "id": "pot1", "top": -1.3, "left": 182.2, "attrs": {} },
    {
      "type": "wokwi-stepper-motor",
      "id": "stepper1",
      "top": -249.61,
      "left": 377.82,
      "attrs": { "size": "8" }
    },
    { "type": "wokwi-a4988", "id": "drv1", "top": -168, "left": 292.8, "attrs": {} },
    { "type": "wokwi-vcc", "id": "vcc2", "top": -258.44, "left": 355.2, "attrs": {} },
    { "type": "wokwi-gnd", "id": "gnd1", "top": -28.8, "left": 373.8, "attrs": {} }
  ],
  "connections": [
    [ "lcd1:RS", "nano:2", "green", [ "v57.6", "h77.6" ] ],
    [ "lcd1:E", "nano:3", "green", [ "v48", "h48.5" ] ],
    [ "lcd1:D4", "nano:4", "green", [ "v9.6", "h-9.6" ] ],
    [ "lcd1:D5", "nano:5", "green", [ "v19.2", "h-28.7" ] ],
    [ "lcd1:D6", "nano:6", "green", [ "v28.8", "h-57.4" ] ],
    [ "lcd1:D7", "nano:7", "green", [ "v38.4", "h-66.9" ] ],
    [ "nano:12", "btn1:1.r", "green", [ "v0" ] ],
    [ "nano:GND.2", "btn1:2.r", "black", [ "v0" ] ],
    [ "nano:GND.1", "dht1:GND", "black", [ "v9.6", "h-201.6" ] ],
    [ "nano:13", "dht1:SDA", "green", [ "v19.2", "h-115.3" ] ],
    [ "servo1:PWM", "nano:10", "green", [ "h0" ] ],
    [ "vcc1:VCC", "servo1:V+", "red", [ "v0" ] ],
    [ "nano:GND.2", "servo1:GND", "black", [ "v0" ] ],
    [ "nano:GND.2", "lcd1:K", "black", [ "v-67.2", "h28.8" ] ],
    [ "vcc1:VCC", "lcd1:A", "red", [ "v19.2", "h-38.4", "v-57.6" ] ],
    [ "pot1:GND", "nano:GND.1", "black", [ "v9.6", "h-67.2" ] ],
    [ "pot1:VCC", "nano:5V", "red", [ "v28.8", "h-106.4" ] ],
    [ "dht1:VCC", "nano:5V", "red", [ "v67.2", "h163.2" ] ],
    [ "drv1:2B", "stepper1:A+", "green", [ "h0" ] ],
    [ "drv1:2A", "stepper1:A-", "green", [ "h0" ] ],
    [ "drv1:1A", "stepper1:B-", "green", [ "h0" ] ],
    [ "drv1:1B", "stepper1:B+", "green", [ "h0" ] ],
    [ "vcc2:VCC", "drv1:VMOT", "red", [ "v0" ] ],
    [ "gnd1:GND", "drv1:GND.2", "black", [ "v0" ] ],
    [ "vcc2:VCC", "drv1:VDD", "red", [ "v0" ] ],
    [ "gnd1:GND", "drv1:GND.1", "black", [ "v0" ] ],
    [ "drv1:RESET", "drv1:SLEEP", "green", [ "h-9.6", "v9.6" ] ],
    [ "nano:8", "drv1:DIR", "green", [ "v-28.8", "h182.4", "v-57.6", "h48", "v-19.2" ] ],
    [ "nano:9", "drv1:STEP", "green", [ "v-19.2", "h182.4", "v-76.8", "h48", "v-9.6" ] ],
    [ "pot1:SIG", "nano:A0", "green", [ "v19.2", "h-163.6" ] ]
  ],
  "dependencies": {}
}

"int" instead of "long" would be fine for these variables.

long days = 0;
long hour = 0;
long minute = 0;
long second = 0;

To keep the decimal point, use "float" instead of "int" here:

int maxTemp = 100.4;
int minTemp = 98.6;

Similar considerations apply to sensor data. Check what data type the library expects for the variables temperature and humidity.

  int result = dht11.readTemperatureHumidity(temperature, humidity);

Thanks, I am going to test all of these suggestions!

Well, I tried everything I know and it still freezes. I ran it overnight and at 4 hours it froze, does anyone have any ideas because I am clueless? I don't feel like having a high chance of killing the eggs I am trying to incubate.

Thanks!

*The Notes are incorrect because I used bits and pieces of other people's code.

/**
 * DHT11 Sensor Reader
 * This sketch reads temperature and humidity data from the DHT11 sensor and prints the values to the serial port.
 * It also handles potential error states that might occur during reading.
 *
 * Author: Dhruba Saha
 * Version: 2.1.0
 * License: MIT
 */

// 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
#define RunLight 8
int Plus = 1;
int turns = 0;
DHT11 dht11(13);
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
CSE_MillisTimer EggTurnTimer(17280000);  // should be 17280000
Servo myservo;
int days = 0;
int hour = 0;
int minute = 0;
int second = 0;

float maxTemp = 100.4;
float 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(RunLight, OUTPUT);
  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() {
  digitalWrite(RunLight, HIGH);

  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(90);            // Move to 180 degrees
    delay(4000);
    myservo.write(45);
    delay(1000);
    myservo.write(1);
    delay(4000);
    myservo.write(45);
    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 >= 62) { digitalWrite(HumLight, HIGH); }  // Should be 60
  if (humidity <= 60) { digitalWrite(HumLight, LOW); }   // Should be 55

  if (humidity <= 50) { digitalWrite(HumLight, HIGH); }  // Should be 30
  if (humidity >= 55) { digitalWrite(HumLight, LOW); }   // Should be 50

  if (tempF >= 101) { digitalWrite(TempLight, HIGH); }  // Should be 103
  if (tempF <= 100.4) { digitalWrite(TempLight, LOW); }   // Should be 102

  if (tempF <= 97) { digitalWrite(TempLight, HIGH); }  // Should be 96
  if (tempF >= 98.6) { digitalWrite(TempLight, LOW); }   // Should be 100
  
  digitalWrite(RunLight, LOW);


  delay(500);
}

'''
type or paste code here

Your problem may very well be in your hardware setup...
That we cannot see...

I will see about getting a photo.

  • Where did you get this code from ? :thinking:

  • Yes, we need to see a schematic so we can fully understand what is happening.
    At one glance we can see all interconnections.

  • As mentioned, images of the actual wiring helps us help you.

lcd.begin(16, 2);
. . .
lcd.setCursor(0, 2);

  • Explain your understanding of the two lines of code above.




  if (humidity >= 62) { digitalWrite(HumLight, HIGH); }  // Should be 60
  if (humidity <= 60) { digitalWrite(HumLight, LOW); }   // Should be 55

  if (humidity <= 50) { digitalWrite(HumLight, HIGH); }  // Should be 30
  if (humidity >= 55) { digitalWrite(HumLight, LOW); }   // Should be 50

  if (tempF >= 101) { digitalWrite(TempLight, HIGH); }  // Should be 103
  if (tempF <= 100.4) { digitalWrite(TempLight, LOW); }   // Should be 102

  if (tempF <= 97) { digitalWrite(TempLight, HIGH); }  // Should be 96
  if (tempF >= 98.6) { digitalWrite(TempLight, LOW); }   // Should be 100
 

  • Do you know what else if( . . . ) does ? :thinking:

Yes, but you dismiss them without trying.

How is that my fault?

Use an RTC.

1 Like
  • OP, follow this advice.

  • At a minimum, second ++; //Clock is very ineffective, at least use a millis( ) based TIMER.

No. This is not a schematic:

this is not a schematic

Nor is this:

image

THIS is a schematic:

Hand-drawn is perfectly acceptable - be sure to have the image well lit.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.