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??