Program keep on crashing on nan sensor don't work

I all , I am fairly newbie , after watching several youtube videos that show how to to build a device that waters the plant , I managed to build one , and by god it works to a point where it keeps triggering the motor off and over flooding the plant pot.

here is my code
#include “DHT.h”
#include “U8glib.h” // for the OLED screen
//**************************************************************
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);
//**************************************************************
#define DHTPIN 2 // what pin we’re connected to digital pin
#define DHTTYPE DHT22 // DHT 22 (AM2302
// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
//Variables
int chk;
float hum; // Stores humidity value
float temp; // Stores temperature value
int soilSensorPin=0; // intialise soil A0 pin
int sensorValue =0;
//2-Way motor control

int motorPin =9; // Pin that motor is connected to
int x=0;

void draw(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.setFont(u8g_font_unifont);

//************************************************************
// Soil sensor / Humminity sensor data
//***********************************************************

// Read data and store it to variables hum and temp
hum= dht.readHumidity();
temp= dht.readTemperature();

// Read soil data value from sensor:
sensorValue = analogRead(soilSensorPin);
//map the value to a percentage
soilSensorPin = map(sensorValue, 485, 1023, 100, 0);
delay(1000); // delay for one second

// Print temp and humidity values to serial monitor
// print out the soil water percentage you calculated:
Serial.print("Soil M : ");// output to serial monitor for testing purpose.
Serial.print(soilSensorPin);
delay(1000);// wait for one second…
//********************************************
// OLED Soil screen OUTPUT
//********************************************
u8g.drawStr( 0, 49, "Soil M: "); // leave this at postion 0 , 30
u8g.setPrintPos(78,49); // move soil postion back if you want to 0,50
u8g.print(soilSensorPin);
delay(5000);// delay for 5 second to allow OLED screen to refresh

Serial.print(" , Humidity: ");// output to serial monitor for testing purpose.
Serial.print(hum);
delay(1000); // delay for one second …

//********************************************
// OLED Humidity screen
//********************************************

u8g.drawStr( 0, 28, “Humidity:”); // leave this at postion 0 , 30
u8g.setPrintPos(78,26); // move soil postion back if you want to 0,50
u8g.print(hum);
delay(3000);

//*********************************************
// NO space on OLED Screen code not included
//*********************************************
Serial.print(" %, Temp: “);// output to serial monitor for testing purpose.
Serial.print(temp);
Serial.println(” Celsius ");
delay(2000);

// if soilSensorPin value is less than 35 in soil moist then
// trigger the water pump keep checking until above 35 in soil moist.
if(! isnan(soilSensorPin)) // this code deals with nan values
{
soilSensorPin = soilSensorPin +1;
}
if(soilSensorPin < 35 )
{
Serial.println(" Soil dry : ");
Serial.print(soilSensorPin);
//********************************************
// OLED OUTPUT “soil dry” + value
//********************************************
u8g.drawStr( 0, 46, " Soil dry: "); // leave this at postion 0 , 30
u8g.setPrintPos(78,49); // move soil postion back if you want to 0,50
u8g.print(soilSensorPin);
digitalWrite(motorPin, HIGH); // turn motor on
delay(6000); // delay 5 seconds
Serial.println(“motor off”); // test purpose only
digitalWrite(motorPin,LOW); // turn motor off
delay(10000); // delay 10 second before turning to the top of the code.

}

else
{
Serial.println("Soil M: " +soilSensorPin);
// do nothing to OLED screen

}
}

void setup()
{
Serial.begin(9600);
dht.begin();
// initialize the digital pins as an output:

pinMode(motorPin, OUTPUT); // initialize motor mode

//**************************************************************
// OLED Code below
//**************************************************************

// flip screen, if required
// u8g.setRot180();

// set SPI backup if required
//u8g.setHardwareBackup(u8g_backup_avr_spi);

// assign default color value
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}

pinMode(8, OUTPUT);

}

// General Loop continue to check

void loop()
{

//************************************************
// OLED code below
//************************************************

u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );

}// end of loop

I am using a OLED screen to display the output unfortunately the result are not so good as it scrambles on ‘nan’ error after searching on this site I found few solution and non are working can someone look at this code and tell me if A I could do this easier and B where my logic’s is out of place.

I would so much appreciated your advice and support. if you need more details just drop me a line.

Please read the post by Nick Gammon at the top of this Forum on how to post code here. It also contains many suggestions that will increase the number of reader responses you can get. Also, before you post your code, use Ctrl-T in the IDE’s source code window to reformat your code to a common C style, Then make sure you use code tags (the </> icon) when your post your code.

Pin numbers should be constants. Change the declaration to:

const int soilSensorPin=A0; // intialise soil A0 pin

That will allow the compiler to show you your other mistake.

Firstly thanks for showing me how to properly upload code , will ensure I do this next time around and place tags to get more responses. "I shall try:   const int soilSensorPin=A0; // intialise soil A0 pin  and let you guru's know. one last thing how can I become a expert like you guys. I guess the answer is keep coding :)

thanks again

:o error HELP !!!. very confuse here what does "Error while setting serial port parameters: 9,00 N 81 " mean will be googling this after this tread.

change the code to const int SoilSensorPin=0; modify few lines where I was incrementing the variable by 1 and the code compile okay uploaded fine.

then I got the above error. am reading this post Help Please: Error while setting serial port parameters: 115,200 N 8 1
did not make any sense so I closed IDE and reopen it , this seem to have worked and I got rid of the error.

my revise code for the plant pot is as follows :

//*******************************************************
// Autor: Waheed Rafiq Research Student Birmingham City University 
// Date Written: 27/06/2016
// Project: iPot (c) (TM) 2001 , Plant Pot that water itself , 
// able to tell user Humidity within the pot dome. 
// Version : 1.1 
// All CopyRight Reserve (c) 1988
//Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
//Everyone is permitted to copy and distribute verbatim copies of this 
//license document, Modification is allowed please do credit the autor.
// Happy Coding , Keep the Peace , Love Technology
// www.waheedrafiq.net 






#include "DHT.h"
#include "U8glib.h" // for the OLED screen
//**************************************************************
  U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);
//**************************************************************
#define DHTPIN 2     // what pin we're connected to digital pin 
#define DHTTYPE DHT22   // DHT 22  (AM2302
// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
//Variables 
int chk;
float hum; // Stores humidity value
float temp; // Stores temperature value
const int soilSensorPin=0; // intialise soil A0 pin 
int sensorValue =0;
//2-Way motor control

int motorPin =9; // Pin that motor is connected to
int x=0;

void draw(void) {
  // graphic commands to redraw the complete screen should be placed here  
  u8g.setFont(u8g_font_unifont);

//************************************************************
//      Soil sensor / Humminity sensor data 
//***********************************************************

// Read data and store it to variables hum and temp
   hum= dht.readHumidity(); 
   temp= dht.readTemperature(); 
    
    // Read soil data value from sensor:
    sensorValue = analogRead(soilSensorPin);
    //map the value to a percentage
    sensorValue = map(soilSensorPin, 485, 1023, 100, 0);   
    delay(1000); // delay for one second 
    
    // Print temp and humidity values to serial monitor
    // print out the soil water percentage you calculated:
    Serial.print("Soil M : ");// output to serial monitor for testing purpose.
    Serial.print(sensorValue);
    delay(1000);// wait for one second... 
    //********************************************    //********************************************
    u8g.drawStr( 0, 49, "Soil M: "); // leave this at postion 0 , 30
    u8g.setPrintPos(78,49);    // move soil postion back if you want to 0,50 
    u8g.print(sensorValue);
    delay(5000);// delay for 5 second to allow OLED screen to refresh
  
    Serial.print(" , Humidity: ");// output to serial monitor for testing purpose.
    Serial.print(hum);
    delay(1000); // delay for one second ... 
    
    //********************************************
    //    OLED Humidity screen
    //********************************************
     
    //    OLED Soil screen OUTPUT

    u8g.drawStr( 0, 28, "Humidity:"); // leave this at postion 0 , 30
    u8g.setPrintPos(78,26);    // move soil postion back if you want to 0,50
    u8g.print(hum);
    delay(3000); 


    //*********************************************
    //    NO space on OLED Screen code not included
    //*********************************************
    Serial.print(" %, Temp: ");// output to serial monitor for testing purpose.
    Serial.print(temp);
    Serial.println(" Celsius ");
    delay(2000);
   
   // if soilSensorPin value is less than 35 in soil moist then 
   // trigger the water pump keep checking until above 35 in soil moist.
   if(! isnan(sensorValue)) // this code deals with nan values 
   {
    sensorValue = sensorValue +1;
    //soilSensorPin = soilSensorPin +1;
    Serial.print("New value soil: ");
    Serial.println(sensorValue);
   }
    if(sensorValue < 35 )
    {
      Serial.println(" Soil dry : ");
      Serial.print(sensorValue);
      //********************************************
     //    OLED  OUTPUT "soil dry" + value
    //********************************************
      u8g.drawStr( 0, 46, " Soil dry: "); // leave this at postion 0 , 30
      u8g.setPrintPos(78,49);    // move soil postion back if you want to 0,50 
      u8g.print(sensorValue);
      digitalWrite(motorPin, HIGH); // turn motor on
      delay(6000); // delay 5 seconds 
      Serial.println("motor off"); // test purpose only 
      digitalWrite(motorPin,LOW); // turn motor off 
      delay(10000); // delay 10 second before turning to the top of the code. 
    
    }
   
    else
    {
      Serial.print("Soil M: ");
      Serial.println(sensorValue); 
     // do nothing to OLED screen  
     
    }  
}


void setup()
{
  Serial.begin(9600);
  dht.begin(); 
  // initialize the digital pins as an output:

  pinMode(motorPin, OUTPUT); // initialize motor mode 

//**************************************************************
//          OLED Code below 
//**************************************************************

// flip screen, if required
  // u8g.setRot180();
  
  // set SPI backup if required
  //u8g.setHardwareBackup(u8g_backup_avr_spi);

  // assign default color value
  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }
  
  pinMode(8, OUTPUT);

   
}


// General Loop continue to check 

void loop()
{

//************************************************
//  OLED  code below 
//************************************************

  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
    
}// end of loop

Special thanks to John Wasser & EconJack for correcting me with the logics

my error was due to human error as I was reading the pin value rather than SensorValue which the above revise code has been modify to reflect this.

Later on this week I shall upload PDF for anyone to follow this project am well please with it. happy coding , Keep the Peace and love Technology

I tried compiling the sketch but got some errors in the DHT library. What DHT library are you using?

In file included from /Users/john/Documents/Arduino/sketch_jun29a/sketch_jun29a.ino:21:0:
/Users/john/Documents/Arduino/libraries/DHT/DHT.h: In constructor 'DHT::DHT(uint8_t, uint8_t, boolean, uint16_t)':
/Users/john/Documents/Arduino/libraries/DHT/DHT.h:172:10: warning: 'DHT::m_bPullupEnabled' will be initialized after [-Wreorder]
  boolean m_bPullupEnabled;
          ^
/Users/john/Documents/Arduino/libraries/DHT/DHT.h:169:16: warning:   'long unsigned int DHT::m_lastreadtime' [-Wreorder]
  unsigned long m_lastreadtime;
                ^
/Users/john/Documents/Arduino/libraries/DHT/DHT.h:80:2: warning:   when initialized here [-Wreorder]
  DHT(uint8_t pin, uint8_t type = DHT_AUTO, boolean pullup = false,
  ^
In file included from /Users/john/Documents/Arduino/libraries/DHT/DHT.cpp:53:0:
/Users/john/Documents/Arduino/libraries/DHT/DHT.h: In constructor 'DHT::DHT(uint8_t, uint8_t, boolean, uint16_t)':
/Users/john/Documents/Arduino/libraries/DHT/DHT.h:172:10: warning: 'DHT::m_bPullupEnabled' will be initialized after [-Wreorder]
  boolean m_bPullupEnabled;
          ^
/Users/john/Documents/Arduino/libraries/DHT/DHT.h:169:16: warning:   'long unsigned int DHT::m_lastreadtime' [-Wreorder]
  unsigned long m_lastreadtime;
                ^
/Users/john/Documents/Arduino/libraries/DHT/DHT.h:80:2: warning:   when initialized here [-Wreorder]
  DHT(uint8_t pin, uint8_t type = DHT_AUTO, boolean pullup = false,
  ^
/Users/john/Documents/Arduino/libraries/DHT/DHT.cpp: In member function 'bool DHT::read()':
/Users/john/Documents/Arduino/libraries/DHT/DHT.cpp:402:22: error: 'pullup' was not declared in this scope
   pullup(m_kSensorPin);
                      ^
exit status 1
Error compiling for board Arduino/Genuino Uno.