This is how you should post code on this forum. Please read the sticky post!
#include <SimpleDHT.h>
#include <TM1637Display.h>
byte pinDHT11 = 11;
SimpleDHT11 dht11(pinDHT11);
byte CLK = 5;
byte DIO = 4;
TM1637Display display(CLK, DIO); //set the display.
byte humidity_sensor_pin = A1;
byte humidity_sensor_vcc = 3; //to turn on and off the sensor only when needed (to not oxidize the sensor)
byte pinLEDred = 9;
byte pinLEDblue = 8;
byte pinLEDgreen =7;
byte pinPump = 6;
byte pinAllarm= A0;
byte pinVel = 10; //set the pin for the fan speed regulation
byte pinHallSensor = 2; //setthe pin for the speed reading
volatile byte half_revolutions;
int rpm;
long timeold;
byte T = 25; //set limit values to be normalized in the greenhouse conditions for Temperature
byte y = 90; //for Terrain Hum
byte x = 50 ; //for Air hum
byte z = 200; //for Water level
void setup() {
pinMode(humidity_sensor_vcc, OUTPUT);
pinMode(pinLEDblue, OUTPUT);
pinMode(pinLEDred, OUTPUT);
pinMode(pinLEDgreen, OUTPUT);
pinMode(pinPump, OUTPUT);
pinMode(pinVel,OUTPUT);
pinMode(pinHallSensor,INPUT);
digitalWrite(2, HIGH);
digitalWrite(humidity_sensor_vcc, LOW);
Serial.begin(9600);
display.setBrightness(0);
attachInterrupt(0, rpm_fun, RISING);
half_revolutions = 0;
rpm = 0;
timeold = 0;
}
int read_humidity_sensor() {
digitalWrite(humidity_sensor_vcc, HIGH);
delay(100);
int value = analogRead(humidity_sensor_pin);
digitalWrite(humidity_sensor_vcc, LOW);
int percValue = map(value,0,1023,99,0); //map the value between 0 (air) and 99 (salty water)
return percValue;
}
void loop() {
int allarm = analogRead(pinAllarm);
Serial.println("=================================");
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Failed Read"); Serial.println(err);delay(1000);
return;
}
if (half_revolutions >= 20) {
rpm = 30*1000/(millis() - timeold)*half_revolutions;
timeold = millis();
half_revolutions = 0;
}
Serial.println("Valori: ");
Serial.print("Temperature=");
Serial.print((int)temperature); Serial.println("*C");
Serial.print("Air Humidity=");
Serial.print((int)humidity); Serial.println("%H");
Serial.print("Terrain Humidity=");
Serial.print((int)read_humidity_sensor()); Serial.println("%H");
Serial.print("RPM Fan=");
Serial.print(rpm,DEC); Serial.println("RPM");
if (allarm>z) {
Serial.println("Water Detected");
}
else if (allarm<z) {
Serial.println("Water not Detected");
}
int val = 35;
uint8_t data[] = {0x0, 0x0, 0x0, 0x0};
data[0]= display.encodeDigit(12); // C of celsius
display.setSegments(data);
delay(1000);
display.showNumberDec(temperature);
delay(1000);
data[0]= display.encodeDigit(14); // E of external
display.setSegments(data);
delay(1000);
display.showNumberDec(humidity);
delay(1000);
data[0]= display.encodeDigit(1); // I of internal
display.setSegments(data);
display.showNumberDec(read_humidity_sensor());
if (read_humidity_sensor()>y || allarm>z) //High humidity AND/OR allarm on turn the pump and the blue led off
{
digitalWrite(pinPump, LOW);
digitalWrite(pinLEDblue, LOW);
}
else if (read_humidity_sensor()<=y && allarm<z) //Low humidity AND allarm off turn the pump and the blue led on
{
digitalWrite(pinPump, HIGH);
digitalWrite(pinLEDblue, HIGH);
}
if (temperature>T) //High temperature turn the red led off
{
digitalWrite(pinLEDred, LOW);
}
else if (temperature<=T) //Low temperature turn the red led on
{
digitalWrite(pinLEDred, HIGH);
}
if (humidity>x) //High air humidity turn the fan and the green led on
{
digitalWrite(pinLEDgreen, HIGH);
digitalWrite(pinVel, HIGH);
}
else if (humidity<x) //Low air humidity turn the fan and the green led off
{
digitalWrite(pinLEDgreen, LOW);
digitalWrite(pinVel, LOW);
}
}
void rpm_fun(){
half_revolutions++;
}
What a mess! Can you draw a proper schematic for us please? Use the "Scematic" view in Fritzing.
Did you really not plug the Nano into the breadboard? That is kind of the point with a Nano!
Are you really running a fan and a motor off the Nano's 5V rail? And the Nano still works?
You seem to be trying to use a PNP transistor as a low-side switch. It should be an NPN. Also you have no flyback diode. I really am surprised you got this far without destroying the Nano!
What's that green rectangle? Is that the DHT11? If so, why is it labelled "4 pin fan". If it is a fan, where is the DHT11?
I think you have work to do before you think about upgrading to an ESP.