I need help fixing my code

how can I stop the mlx90614 to detect temperature? for example, I scanned myself near the MLX sensor and it get my temperature and I want that the temperature will stop scanning unless someone is near the sensor or next to me.

This is my code. thank you for your help.

#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>

Adafruit_SH1106 display(-1); //Declaring the display name (display)
Adafruit_MLX90614 mlx = Adafruit_MLX90614();

#define echoPin 11 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 12 // attach pin D3 Arduino to pin Trig of HC-SR04
int maximumRange = 5;
int minimumRange = 0;
int duration, distance;

int redLED = 10;
int greenLED = 9;

void setup() {  
  pinMode (trigPin, OUTPUT);
  pinMode (echoPin, INPUT);
  mlx.begin(); 
  display.begin(SH1106_SWITCHCAPVCC, 0x3C); //Start the OLED display
  display.clearDisplay();
  display.display();

  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);

}

void loop() {
  display.clearDisplay();

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = duration*0.034/2;

  display.setTextSize(2);
  display.setCursor(17,20);
  if (distance > maximumRange){
    display.print("CLOSER");
  }
 
  if ((distance >= minimumRange) && (distance <= maximumRange)){
  display.setTextSize(4);
  display.setCursor(17,20);
  display.println(mlx.readObjectTempC(),1);
  }
  if (mlx.readObjectTempC() >= 37.5){
  tone(3,4000,100);
  delay(100);
  noTone(3);

  digitalWrite(redLED, HIGH);
  delay(1000);
  digitalWrite(redLED, LOW);
  delay(1000);
  } 
  
  
  display.setTextSize(1);                    
  display.setTextColor(WHITE);             
  display.setCursor(17,6);                
  display.println("Your Temperature"); 
  
  display.setTextSize(1);
  display.setCursor(45,55);
  display.println("Celsuis");
  
  display.display();
  
  delay(1000);
 

}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Hi randy,

I tried hard to understand what you mean by that description. I don't understand it.

well I guess the only way is to switch off the power-supply of the sensor

you are the one that wants help. So there are several steps you should do.
You are the one that wants help. To increase the probality that you will really get help you should provide much more information.
Writing what type of sensor is a mlx90614 . Providing a link to the datasheet of that sensor or module.

this is by far a tooooo unprecise description of what you mean. It sounds contradictory
either the second person is near to you or near to the sensor? or both? or even something else?

Makes no sense at all. If you stop scanning how should your program ever have any kind of condition to start scanning again??

I'm pretty sure your program shall react differently on different situations = measuring results but keep on scanning all the time.

So basically there are two ways to proceed in your project:

way 1: keep on posting always too short descriptions of what you really want. Always just invoking another asking back repeated 50 times frustrating you and frustrating your potential helpers time to fix your code 10 days = 240 hours or maybe never.

way 2: investing 2 hours of your time to write down a very detailed description of what your code shall do including re-editing your text a dozen times for saying it in a better and easier to understand way.

And then get precise suggestions how your code can be fixed. Time to fix your code 3 days = 72 hours.

You are free to choose which way makes more fun to you
as all the voluntary users here a free to choose how much and which user they will support

best regards Stefan

Hi sir, my concern is how can I return my program from the start? My MLX90614 which is an IR contactless sensor continues to scan temperature. My project is a contactless thermometer and it should detect temperature one after another, if there is nothing near the sensor it will not detect temperature. And I provided my code for someone to look at what is lacking in my code or needs fixing.

I guess you want to show respect by writing Hi "Sir".
Well saying "Sir" is not nescessary. If you really want to show respect
You take time to write an at least 10 sentences long description of the program's functionality that you want to have.

This is a tooo short description to really understand what functionality you want to have.

This doesn't help at all if the

wanted

functionality is unclear

what is so hard about writing down 10 sentences like
If a person comes closer to the sensor ......

if condition A then Z shall happen

if condition B then Y shall happen

if condition C then X shall happen

repeat / don't repeat "ABC" until condition "D" is true
etc.

put above code inside

How can I stop the mlx90614 to detect temperature? for example, I scanned myself near the MLX sensor and it get my temperature and I want that the temperature will stop scanning unless someone is near the sensor or next to me.

#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>

Adafruit_SH1106 display(-1); //Declaring the display name (display)
Adafruit_MLX90614 mlx = Adafruit_MLX90614();

#define echoPin 11 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 12 // attach pin D3 Arduino to pin Trig of HC-SR04
int maximumRange = 5;
int minimumRange = 0;
int duration, distance;

int redLED = 10;
int greenLED = 9;

void setup() {  
  pinMode (trigPin, OUTPUT);
  pinMode (echoPin, INPUT);
  mlx.begin(); 
  display.begin(SH1106_SWITCHCAPVCC, 0x3C); //Start the OLED display
  display.clearDisplay();
  display.display();

  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);

}

void loop() {
  display.clearDisplay();

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = duration*0.034/2;

  display.setTextSize(2);
  display.setCursor(17,20);
  if (distance > maximumRange){
    display.print("CLOSER");
  }
 
  if ((distance >= minimumRange) && (distance <= maximumRange)){
  display.setTextSize(4);
  display.setCursor(17,20);
  display.println(mlx.readObjectTempC(),1);
  }
  if (mlx.readObjectTempC() >= 37.5){
  tone(3,4000,100);
  delay(100);
  noTone(3);

  digitalWrite(redLED, HIGH);
  delay(1000);
  digitalWrite(redLED, LOW);
  delay(1000);
  } 
  
  
  display.setTextSize(1);                    
  display.setTextColor(WHITE);             
  display.setCursor(17,6);                
  display.println("Your Temperature"); 
  
  display.setTextSize(1);
  display.setCursor(45,55);
  display.println("Celsuis");
  
  display.display();
  
  delay(1000);
 

}

Try here

usually people asking how to start to detect temperature.

#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>

Adafruit_SH1106 display(-1);
Adafruit_MLX90614 mlx = Adafruit_MLX90614();

#define echoPin 11
#define trigPin 12
#define redLED  10

unsigned int maximumRange = 5;
unsigned int minimumRange = 0;

void setup() {
  pinMode (trigPin, OUTPUT);
  pinMode (echoPin, INPUT);
  pinMode(redLED, OUTPUT);
  mlx.begin();
  display.begin(SH1106_SWITCHCAPVCC, 0x3C); //Start the OLED display
  display.clearDisplay();
  display.display();
}

void loop() {
  display.clearDisplay();

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  unsigned int distance = ((pulseIn(echoPin, HIGH) * 17)) / 1000;

  display.setTextSize(2);
  display.setCursor(17, 20);
  if (distance > maximumRange) {
    display.print("CLOSER");
    display.display();
  } else {
    if (distance >= minimumRange) {
      float OT = mlx.readObjectTempC();

      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(17, 6);
      display.println("Your Temperature");

      display.setTextSize(4);
      display.setCursor(17, 20);
      display.println(OT), 1);

      display.setTextSize(1);
      display.setCursor(45, 55);
      display.println("Celsuis");

      display.display();

      if (OT >= 37.5) {
      digitalWrite(redLED, HIGH);
        tone(3, 4000, 100);
        delay(100);
        noTone(3);

        delay(1000);
        digitalWrite(redLED, LOW);
      }
    }
  }
}

Topics merged

Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to [url=https://forum.arduino.cc/index.php?topic=710766.0]Learn How To Use The Forum[/url].

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

You need to have a sensor that can tell when someone is near the sensor or next to you.

?! _

he means probably : a sensor that can tell when someone is near the sensor or when next to you.

from looking at the code the TO has posted I guess next to sensor and next to i one and the same thing.
So reducing the if-condition to "next to the sensor" is sufficient.
To really know it the TO has to post a more precise description.
best regards Stefan

What part do you call 'scanning'?
Is it this part?

Or is it this part?

Or is it both?

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