ImI'm using an arduino nanoArduino Nano for programing an automatic hand sanitizer that has a thermometer. But after running for some time the board gets freezefreezes and only restarting can solve the issue. While checking the serial monitor the workingit suddenly stops at suddenworking.
The program includes reading distance from an ultrasonic sensor and temperature from an IR thermometer (distance and ambient temperature are read continuoslycontinuously and the serial monitor will constantly display values).At At random when the temperature is read and is displayed on the OLED screen,the the board freezes (board freezes only at this time) and restarting can make it work 1 more time and then freezes again.
This is my code:
#include <Wire.h>
#include <Adafruit_MLX90614.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_MLX90614 mlx =
Adafruit_MLX90614();
Adafruit_SSD1306 display(-1);
//Adafruit_SSD1306 display(128,64, &Wire, -1);
#define trigPin 12 // Trigger pin
#define echoPin 11 // echo pin
float roomTemp; // ambient temperature
float objectTemp, stemp; // object temperature
int readcount = 0;
float threshold= 3.5 ;
int maximumRange = 15; // Maximum range needed
int minimumRange = 3; // Minimum range needed
long duration, distance; // Duration used to calculate distance
int dtime;
unsigned long rememTime;
// motor coding
int set_time;
float distance_cm;
unsigned long ultra_time;
int set_cm = 20;
int motor = 2; // Output for Motor drive
int flag=0;
void setup() {
Serial.begin(9600);// initialize serial communication at 9600 bits per second:
pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);
pinMode(motor, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC,0x3C) ;
delay(200);
display.clearDisplay();
display.setTextColor(WHITE);
}
void loop() {
// temperature sensor coding
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance= duration*0.034/2;
// reading object and ambient temperature
objectTemp = threshold + mlx.readObjectTempC() ;
roomTemp = mlx.readAmbientTempC() ;
// print to Serial port
Serial.println("Object:" + String(objectTemp) + ", Ambient:" + String(roomTemp));
Serial.println(distance);
// display on OLED
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,25);
display.print("Dis:" + String(distance) + "cm");
display.setCursor(65,25);
display.print("Room:" + String(roomTemp).substring(0, 4) + "C");
display.display();
display.setTextSize(2);
display.setCursor(0, 0);
if (distance < minimumRange) {
display.print("TOO CLOSE!");
}
if ((distance >= minimumRange) && (distance <= maximumRange))
{
if (readcount == 5) { // after reading 5 consecutive time
disptemp();
} else {
display.print("HOLD ON"); // when in range, ask user to hold position
stemp = stemp + objectTemp;
readcount++;
// until approx. 5 x 200 ms = 1 sec
}
} else { // if user is out of range, reset calculation
dtime = 100;
readcount = 0;
stemp = 0;
}
display.display();
delay(dtime);
Serial.println("count :"+String(readcount));
}
void disptemp()
{
objectTemp = stemp / 5; // get the average reading of temp
display.setTextSize(1);
display.print("YOUR TEMP:");
display.setTextSize(2);
display.setCursor(60,5);
display.print(String(objectTemp).substring(0, 4) + "C");
display.display();
readcount = 0;
stemp = 0;
if (objectTemp >= 38) {
play_alert();
} else {
play_ok();
}
ultrasonicRead();
dtime = 5000; // wait for 5 seconds
}
void play_ok() { // play three sequential notes when object temperature is below 37.5C
tone(3, 600, 1000); // pin,frequency,duration
delay(200);
tone(3, 750, 500);
delay(100);
tone(3, 1000, 500);
delay(200);
noTone(3);
}
void play_alert() { // beep 3x when object temperature is >= 37.5C
tone(3, 2000, 1000);
delay(1000);
tone(3, 3000, 1000);
delay(1000);
tone(3, 4000, 1000);
delay(1000);
noTone(3);
}
void ultrasonicRead()
{
if(distance<set_cm)
{
digitalWrite(motor,HIGH);
Serial.println("Motor On");
delay(500);
digitalWrite(motor,LOW);
Serial.println("motor off");
}
}
#include <Wire.h>
#include <Adafruit_MLX90614.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
Adafruit_SSD1306 display(-1);
//Adafruit_SSD1306 display(128, 64, &Wire, -1);
#define trigPin 12 // Trigger pin
#define echoPin 11 // echo pin
float roomTemp; // ambient temperature
float objectTemp, stemp; // object temperature
int readcount = 0;
float threshold= 3.5 ;
int maximumRange = 15; // Maximum range needed
int minimumRange = 3; // Minimum range needed
long duration, distance; // Duration used to calculate distance
int dtime;
unsigned long rememTime;
// motor coding
int set_time;
float distance_cm;
unsigned long ultra_time;
int set_cm = 20;
int motor = 2; // Output for Motor drive
int flag = 0;
void setup() {
Serial.begin(9600);// initialize serial communication at 9600 bits per second:
pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);
pinMode(motor, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C) ;
delay(200);
display.clearDisplay();
display.setTextColor(WHITE);
}
void loop() {
// temperature sensor coding
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance= duration * 0.034/2;
// reading object and ambient temperature
objectTemp = threshold + mlx.readObjectTempC() ;
roomTemp = mlx.readAmbientTempC() ;
// print to Serial port
Serial.println("Object:" + String(objectTemp) + ", Ambient:" + String(roomTemp));
Serial.println(distance);
// display on OLED
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 25);
display.print("Dis:" + String(distance) + "cm");
display.setCursor(65, 25);
display.print("Room:" + String(roomTemp).substring(0, 4) + "C");
display.display();
display.setTextSize(2);
display.setCursor(0, 0);
if (distance < minimumRange) {
display.print("TOO CLOSE!");
}
if ((distance >= minimumRange) && (distance <= maximumRange)) {
if (readcount == 5) { // after reading 5 consecutive time
disptemp();
} else {
display.print("HOLD ON"); // when in range, ask user to hold position
stemp = stemp + objectTemp;
readcount++; // until approx. 5 x 200 ms = 1 sec
}
} else { // if user is out of range, reset calculation
dtime = 100;
readcount = 0;
stemp = 0;
}
display.display();
delay(dtime);
Serial.println("count :"+String(readcount));
}
void disptemp() {
objectTemp = stemp / 5; // get the average reading of temp
display.setTextSize(1);
display.print("YOUR TEMP:");
display.setTextSize(2);
display.setCursor(60,5);
display.print(String(objectTemp).substring(0, 4) + "C");
display.display();
readcount = 0;
stemp = 0;
if (objectTemp >= 38) {
play_alert();
} else {
play_ok();
}
ultrasonicRead();
dtime = 5000; // wait for 5 seconds
}
void play_ok() { // play three sequential notes when object temperature is below 37.5C
tone(3, 600, 1000); // pin,frequency,duration
delay(200);
tone(3, 750, 500);
delay(100);
tone(3, 1000, 500);
delay(200);
noTone(3);
}
void play_alert() { // beep 3x when object temperature is >= 37.5C
tone(3, 2000, 1000);
delay(1000);
tone(3, 3000, 1000);
delay(1000);
tone(3, 4000, 1000);
delay(1000);
noTone(3);
}
void ultrasonicRead() {
if (distance < set_cm) {
digitalWrite(motor, HIGH);
Serial.println("Motor On");
delay(500);
digitalWrite(motor, LOW);
Serial.println("motor off");
}
}