Having Trouble staying in side a function

what i want to do is request temperature from user if conditions are met.

if (Check_timeIn == "null" && Type == "Lecturer") {

      Serial.println("temperature needed for card IN");
      tempRequest();
      Serial.println("New Status = IN");
      NewStatus = "IN";

    } else if (Check_timeOut == "null" && Type == "Lecturer") {

      Serial.println("No temperature needed");
      Serial.println("New Status = OUT");
      NewStatus = "OUT";

    } else if (Check_timeOut != "null" && Type == "Lecturer") {

      Serial.println("temperature needed for card IN");
      tempRequest();
      Serial.println("New Status = IN");
      NewStatus = "IN";

    } else {

      Serial.println("temperature needed for card IN");
      tempRequest();
      Serial.println("New Status = IN");
      NewStatus = "IN";
    }

when tempRequest function is called it is not staying inside that function.This is the code for tempRequest Function:

void tempRequest() {

  pinMode(echoPin, INPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);

  /******************************Based on the speed of sound, Distance Calculated in cm*********************/
  distance = duration / 58.2;
  /*****************************Reading object (to) and ambient (ta) temperature, and rounding value********/
  to = mlx.readObjectTempC();                   // Discrepancy in MLX90614 incremented
  ta = round(mlx.readAmbientTempC() * 10) * .1;

  Serial.println("Dist:" + String(distance) + "cm");

  if (distance > maximumRange) {
    Serial.println("GET CLOSER");
  }
  if (distance < minimumRange) {
    Serial.println("TOO CLOSE!");
  }
  if ((distance >= minimumRange) && (distance <= maximumRange)) {
    if (readcount == 5) {                                              // Take Reading for 5 consecutive time
      to = stemp / 5;                                                  // Average reading for a more accurate reading
      Serial.println("YOUR TEMP:");


      Temperature = String(to).substring(0, 4);
      Serial.print(Temperature  + "C");
      readcount = 0;
      stemp = 0;
      if (to >= 37.5) {
        play_alert();
      } else {
        play_ok();
      }
      loop_interval = 5000;                                            // Hold for 5 seconds
    } else {
      Serial.println("HOLD ON");                                        // when in range, ask user to hold position
      stemp = stemp + to;
      readcount++;
      loop_interval = 200;                                             // until approx. 5 x 200 ms = 1 sec
    }
  } else {                                                             // if user is out of range, reset calculation
    loop_interval = 100;
    readcount = 0;
    stemp = 0;
  }
  delay(loop_interval);
}

/*********************Three Sequential notes are played when object temperature is below 37.5C*************************/
void play_ok() {
  tone(BUZZER, 600, 200);
  delay(100);
  tone(BUZZER, 750, 200);
  delay(100);
  tone(BUZZER, 1000, 200);
  delay(100);
  noTone(BUZZER);
}

/**********************Beep three times when object Temperature is greater than 37.5C************************************/
void play_alert() {
  tone(BUZZER, 1000, 500);
  delay(1000);
  tone(BUZZER, 1000, 500);
  delay(1000);
  tone(BUZZER, 1000, 500);
  delay(1000);
  noTone(BUZZER);
}

but when i test the code separately it works fine. that code is shown below:

/******************************************Libraries******************************************************************/
#include <Wire.h>                                                       // Wire Library used for 12C Communication Protocol
#include <Adafruit_MLX90614.h>                                          // Temperature Sensor MLX90614 Library used to read MLX90614 Tempearture Values
Adafruit_MLX90614 mlx = Adafruit_MLX90614();                            // MLX90614 Sensor defined as an Obj

/******************************************Ultrasonic Sensor preparation**********************************************/
#define echoPin 13                                                       // Echo Pin
#define trigPin 12                                                       // Trigger Pin
 
#define BUZZER D8 //buzzer pin

int maximumRange = 40;                                                  // Maximum range needed
int minimumRange = 10;                                                  // Minimum range needed
long duration, distance;                                                // Duration used to calculate distance
String Temperature;
float ta;                                                               // ambient temperature
float to, stemp;                                                        // object temperature

int loop_interval = 100;                                                // check temp every 100 msec
int readcount = 0;

void setup() {
  pinMode(trigPin, OUTPUT);
                                             // Use LED indicator (if required)
  Serial.begin(115200);                                                 // Select appropriate Baud for Se

  if (!mlx.begin()) {
    Serial.println("Error connecting to MLX sensor. Check wiring.");
    while (1);
  }
}

void loop() {
  pinMode(echoPin, INPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  
  /******************************Based on the speed of sound, Distance Calculated in cm*********************/
  distance = duration / 58.2;
  /*****************************Reading object (to) and ambient (ta) temperature, and rounding value********/
  to = mlx.readObjectTempC();                   // Discrepancy in MLX90614 incremented
  ta = round(mlx.readAmbientTempC() * 10) * .1;

  Serial.println("Dist:" + String(distance) + "cm");

  if (distance > maximumRange) {
    Serial.println("GET CLOSER");
  }
  if (distance < minimumRange) {
    Serial.println("TOO CLOSE!");
  }
  if ((distance >= minimumRange) && (distance <= maximumRange)) {
    if (readcount == 5) {                                              // Take Reading for 5 consecutive time
      to = stemp / 5;                                                  // Average reading for a more accurate reading
      Serial.println("YOUR TEMP:");
     
      
     Temperature = String(to).substring(0, 4);
     Serial.print(Temperature  + "C");
      readcount = 0;
      stemp = 0;
      if (to >= 37.5) {
        play_alert();
      } else {
        play_ok();
      }
      loop_interval = 5000;                                            // Hold for 5 seconds
    } else {
      Serial.println("HOLD ON");                                        // when in range, ask user to hold position
      stemp = stemp + to;
      readcount++;
      loop_interval = 200;                                             // until approx. 5 x 200 ms = 1 sec
    }
  } else {                                                             // if user is out of range, reset calculation
    loop_interval = 100;
    readcount = 0;
    stemp = 0;
  }
  delay(loop_interval);
}

/*********************Three Sequential notes are played when object temperature is below 37.5C*************************/
void play_ok() {  
  tone(BUZZER, 600, 200);
  delay(100);
  tone(BUZZER, 750, 200);
  delay(100);
  tone(BUZZER, 1000, 200);
  delay(100);
  noTone(BUZZER);
}

/**********************Beep three times when object Temperature is greater than 37.5C************************************/
void play_alert() { 
  tone(BUZZER, 1000, 500);
  delay(1000);
  tone(BUZZER, 1000, 500);
  delay(1000);
  tone(BUZZER, 1000, 500);
  delay(1000);
  noTone(BUZZER);
}

how can i make it stay inside tempRequest function until user temperature is taken??
can someone help me with this??

You need to post all your code, or if you think it is too long write a small code that shows your problem.

i have uploaded all codes needed for this issue..

 if (Check_timeIn == "null" && Type == "Lecturer") {

      Serial.println("temperature needed for card IN");
      tempRequest();
      Serial.println("New Status = IN");
      NewStatus = "IN";

    } else if (Check_timeOut == "null" && Type == "Lecturer") {

      Serial.println("No temperature needed");
      Serial.println("New Status = OUT");
      NewStatus = "OUT";

    } else if (Check_timeOut != "null" && Type == "Lecturer") {

      Serial.println("temperature needed for card IN");
      tempRequest();
      Serial.println("New Status = IN");
      NewStatus = "IN";

    } else {

      Serial.println("temperature needed for card IN");
      tempRequest();
      Serial.println("New Status = IN");
      NewStatus = "IN";
    }

in here when tempRequest function is called it should run that tempRequest function but not exit tempRequest function until temperature is taken. problem is it goes inside the tempRequest function and exits before taking temperature

Please yourself, good luck in getting this to work.

there is no loop inside your function waiting for some event to be matched... so I don't see why you would expect this function to be "blocking"

if you put the code in the loop() function, then it loops...

Hi,

Can I suugest you look at select.. case.

https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/

And forget nested "else if".

Tom.. :smiley: :+1: :coffee: :australia:

I use goto to function to stay inside the tempRequest. Now it works perfectly.

goto... :cold_face:

gee... never heard about iteration statements (while, do/while, for) ?

There’s a an unexpected plot twist.

Please post your function with a goto fixing you problem.

Someone here will rewrite it without goto, and that will be a better thing for other noobs to find if they are trying to learn how to program by browsing these fora.

TIA

a7

...for a limited subset of definitions of "perfectly"

So the issue is solved, isn't it? Could you show the solution you came up with?

here is the code that works for me. if anyone has a better one please tell me.

void tempRequest() {
temploop:
  
  pinMode(echoPin, INPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);

  /******************************Based on the speed of sound, Distance Calculated in cm*********************/
  distance = duration / 58.2;
  /*****************************Reading object (to) and ambient (ta) temperature, and rounding value********/
  to = mlx.readObjectTempC();                   // Discrepancy in MLX90614 incremented
  ta = round(mlx.readAmbientTempC() * 10) * .1;

  Serial.println("Dist:" + String(distance) + "cm");

  if ((distance >= minimumRange) && (distance <= maximumRange)) {

    Serial.print("HOLD ON");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("HOLD ON");
    delay(2000);                                           // Hold for 5 seconds
    Serial.println("YOUR TEMP:");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("YOUR TEMPERATURE");
    lcd.setCursor(4, 1);
    lcd.print("IS OK!!!");
    Temperature = String(to).substring(0, 4);
    Serial.print(Temperature  + "C");
    if (to >= 37.5) {
      play_alert();
    } else {
      play_ok();
    }

  } else {
    goto temploop;
    delay(500);
  }
}

/*********************Three Sequential notes are played when object temperature is below 37.5C*************************/
/**********************Beep three times when object Temperature is greater than 37.5C************************************/

void play_ok() {
  tone(BUZZER, 1000, 200);
  delay(100);
  noTone(BUZZER);
}
void play_alert() {
  tone(BUZZER, 1000, 500);
  delay(1000);
  tone(BUZZER, 1000, 500);
  delay(1000);
  tone(BUZZER, 1000, 500);
  delay(1000);
  noTone(BUZZER);
}
    goto temploop;
    delay(500);

Seriously?

delay(2000);                                           // Hold for 5 seconds

Nope,

That use of delay() is 100 percent OK! :expressionless:

a7

I am not a pro.. :expressionless:

The brute-force-and-ignorance version could look like this

void tempRequest() 
{
  while (1) {
  
    // pinMode(echoPin, INPUT);  // should be in setup ()
    // digitalWrite(trigPin, LOW); // should already be LOW
    // delayMicroseconds(2);       // not needed
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);

    /******************************Based on the speed of sound, Distance Calculated in cm*********************/
    distance = duration / 58.2;
    /*****************************Reading object (to) and ambient (ta) temperature, and rounding value********/
    to = mlx.readObjectTempC();                   // Discrepancy in MLX90614 incremented
    ta = round(mlx.readAmbientTempC() * 10) * .1;

    Serial.println("Dist:" + String(distance) + "cm ");

    if ((distance >= minimumRange) && (distance <= maximumRange)) {

      Serial.print("HOLD ON");
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("HOLD ON");
      delay(2000);                                           // Hold for 2 seconds
      Serial.println("YOUR TEMP:");
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("YOUR TEMPERATURE");
      lcd.setCursor(4, 1);
      lcd.print("IS OK!!!");
      Temperature = String(to).substring(0, 4);
      Serial.print(Temperature  + "C");
      if (to >= 37.5) {
        play_alert();
      } else {
        play_ok();
      }
      break;
    }
  }  
}

(uncompiled, untested (obviously)).
But I wouldn't write it like that either.

thank you. this works :slightly_smiling_face: