My mlx90614 problem

hello i am newbie programmer arduino. i make project with mlx90614 with the condition :

condition 1 : make range 27C to 37C for normal temperature
condition 2 : if higher 37C for not normal temperature
condition 3 ; if less 27C show the text ("name device") / off mlx and show the text ("name device")

i make this code like this but i know this not flexible and efficient Can anyone help me? thank you :grin:

code i make :

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>

#define I2C_ADDR 0x27 //I2C adress, you should use the code to scan the adress first (0x27) here
#define BACKLIGHT_PIN 3 // Declaring LCD Pins
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#define laser 23

LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
Adafruit_MLX90614 mlx = Adafruit_MLX90614();

void setup() {
  
  Serial.println("Temperature Sensor MLX90614");
  pinMode(laser, OUTPUT);     // Connect LASER
  digitalWrite(laser, LOW);
  
  mlx.begin();
  lcd.begin (16,2);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH); //Lighting backlight
  lcd.home ();

}

void loop() {  
 
 

 delay(1000);
 
 lcd.clear();
 lcd.setCursor(3,0);
 lcd.print("Non-contact");
 
 lcd.setCursor(2,1);
 lcd.print("Suhu: ");
 lcd.print(mlx.readObjectTempC());
 lcd.print("C     ");
 delay(2000);

  
   if(mlx.readObjectTempC() <= 37 and (mlx.readObjectTempC() >= 27)){
    digitalWrite(laser, HIGH);
    lcd.clear();
    lcd.setCursor(1,0);
    delay(500);
    lcd.print("Suhu Tubuh Anda"); 
    lcd.setCursor(5,1);
    lcd.print("Normal");
    delay(2000);}

    else if (mlx.readObjectTempC() >= 37){
      digitalWrite(laser, HIGH);
    lcd.clear();
    lcd.setCursor(1,0);
    delay(500);
    lcd.print("Suhu Tubuh Anda"); 
    lcd.setCursor(2,1);
    lcd.print("Tidak Normal");
    delay(2000);}

    
    else if (mlx.readObjectTempC() <= 25){
    digitalWrite(laser, LOW);
    lcd.clear();
    lcd.setCursor(2,0);
    delay(500);
    lcd.print("auto WITEZER"); 
    lcd.setCursor(5,1);
    lcd.print("ONLINE");
    delay(2000);}
}

Please post your complete sketch

oh sorry, i editted the post now

Why does your sketch not have a loop() function in it ?

sorry wrong code i editted now :grin:

Some suggestions

  • if the temperature has not changed since the last time you read it then you don't need to do anything. What data type does the readObjectTempC() function return ? I am guessing a float
  • there is quite a lot of common code for each case such as clearing the LCD, positioning the cursor and the delay()s. You could put the code to update the LCD into a function and call that with appropriate parameters. Something like
void updateLCD(byte laserState, char * row1Text, char * row2Text)
{
  digitalWrite(laser, laserState);
  lcd.clear();
  lcd.setCursor(2, 0);
  delay(500);
  lcd.print(row2Text);
  lcd.setCursor(5, 1);
  lcd.print(row1Text);
  delay(2000);
}