2

I'm a beginner of Arduino programming, when I dumped the following code into Arduino NANO board, at the first the output is as expected. But After few trials the 16*2 LCD display is getting off and DC motor pump is running continuously.

      #include <Wire.h>
      #include <Adafruit_MLX90614.h>
      #include <LiquidCrystal_I2C.h>
      LiquidCrystal_I2C lcd(0x27, 16,2);
      
      char *typeName[]={"Object","Ambient"};
      
      Adafruit_MLX90614 mlx = Adafruit_MLX90614();

      const int trigPin = 13;
      const int echoPin = 12;
      #define DCwater_pump 11
   
      long duration;
      int distance;

     void setup() 
     {
          Serial.begin(9600);
          Serial.println("S.O.A.P. DISPENS");

          Serial.print("Ambient = ");
          Serial.print(mlx.readAmbientTempF());
          Serial.print("*F\tObject = ");
          Serial.print(mlx.readObjectTempF());
          Serial.print("*F");
          
                    
          Serial.println();
           
          delay(500);
    

          if (!mlx.begin()) 
          {
              lcd.print("MLX90614 Failed");
              lcd.setCursor(0,1);
              lcd.print("check wiring!");      
              while (1)
              ;
          }  
          Wire.begin();
          lcd.begin(16,2);
          lcd.backlight();  
          lcd.print("S.O.A.P.D. Temp");
          lcd.setCursor(0,1);
          lcd.print("S.O.A.P.D. Temp");          
          delay(500);       
          clearCharacters(2-1,0, 16-1);

          pinMode(trigPin, OUTPUT); 
          pinMode(echoPin, INPUT); 
          pinMode(DCwater_pump, OUTPUT);
       
    }           


void loop() 
{
  
        printTemp('C');
        delay(1000);
        
        printTemp('D');
        delay(1500);

}

float getTemp(char type)
{
  
  float value;
    float tempObjec = mlx.readObjectTempC();
    float tempAmbient = mlx.readAmbientTempC();
   
   if(type =='C')
   {
    value = tempObjec;
   }
   else if(type =='D')
   {
    value = tempAmbient;
   }
   return value;
    
}

            void printTemp(char type)
  {
            clearCharacters(1,0, 16-1 );  
            float TmpC =getTemp(type);
            float TemF;
            TemF = (TmpC*1.8)+32;
            lcd.setCursor(0,1);
            if(type =='C')
            
              {
                lcd.print(typeName[0]); 
                lcd.print(" ");
                lcd.print(TemF);        
                lcd.print((char)223);
                lcd.print("F");      
               }       

                  else if(type =='D')
                  {
                    lcd.print(typeName[1]); 
                    lcd.print(" ");
                    lcd.print(TemF);        
                    lcd.print((char)223);
                    lcd.print("F");        
                  }
  
                  
  }


void clearCharacters(uint8_t row,uint8_t start, uint8_t stop )
{
    for (int i=start; i<=stop; i++)
    {
    lcd.setCursor (i,row);   
    lcd.write(254);
    } 

    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    
    distance= duration*0.034/2;
    
    Serial.print("Distance: "); 
    Serial.println(distance);
    
           if (distance < 30)
          {
           digitalWrite(DCwater_pump,HIGH);
           Serial.println("DC Pump is ON Now!!");
           delay(500);
          }
          else
          {
           digitalWrite(DCwater_pump,LOW);
           Serial.println("DC Pump is OFF Now!!");
           delay(500);
          }

}
PMF
  • 1,184
  • 3
  • 16
Swathi
  • 21
  • 1
  • Try to fix the formatting of your code first. This is very hard to read. – PMF Jan 20 '21 at 06:04
  • 2
    Your code is very confusingly organized. You `clearCharacters` function, which suposedly should clear the display, does a distance measurement (using an HSR-04 module?) and enables/disables the water pump as a side effect. – PMF Jan 20 '21 at 06:10
  • 2
    How is the pump motor connected? Do you have a flyback diode? Did you try using external pullups in the range of 4.7kOhm at the I2C lines (they might get problems with the motor generated noise, when you only use the internal pullups)? – chrisl Jan 20 '21 at 06:56
  • I connected DC pump using a relay. – Swathi Jan 20 '21 at 09:09

0 Answers0