// TTA Demo NTC Sensor
#include <LiquidCrystal.h> // Library for LCD
LiquidCrystal lcd (7,6,5,4,3,2); //5,4,3,2 for D4,D5,D6,D7 for string
unsigned long startTime; // define a long variable to store "startTime"
bool timerStarted = false; // timer initial status
bool buttonPressed = false;
const int buttonPin = 8;
const int ledPin = 13; // Constant - define constant to store output pin 13 for the LED
const int buzzerPin = 12; // output pin 12 for the buzzer
const int sensorPin = A0;
float baselineTemp;
// variable foe NTC sensor only
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
void setup() {
Serial.begin(9600); // set booud rate to read time
lcd. begin(16,2); // set screen size
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT); // set pin for LED output
pinMode(buzzerPin, OUTPUT); // set pin for buzz output
}
void loop() {
if (digitalRead(buttonPin) == HIGH && !buttonPressed) {
buttonPressed = true;
// LED flash
for (int i = 0; i < 5; ++i) {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
// LED on
digitalWrite(ledPin, HIGH);
// buzz for 1 second
tone(buzzerPin,2000,1000);
// record baseline temperature
delay(1000);
int sensorVal_base=analogRead(sensorPin); // read voltage on sensor
baselineTemp = 1 / (log(1 / (1023. / sensorVal_base - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.println(baselineTemp);
// start timer
startTime = millis()/1000;
timerStarted = true;
} else if (digitalRead(buttonPin) == LOW) {
buttonPressed = false;
}
// if timer starts, the program starts
if (timerStarted) {
unsigned long elapsedTime = millis()/1000 - startTime;
int sensorVal=analogRead(sensorPin); // read voltage on sensor
// write time in LCD and in Serial
unsigned int minutes = elapsedTime / 60; //convert into minutes
unsigned int seconds = elapsedTime % 60; //convert into seconds
lcd.setCursor(0,0); //set location on screen
lcd.print("Time:"); //print "Time" to LCD
lcd.print(" ");
lcd.print(minutes);
lcd.print(" m ");
lcd.print(seconds);
lcd.print(" s");
lcd.setCursor(0,1); // change line
lcd.print("Temp: ");
//convert the return value for LM35 sensor
//float voltage=(sensorVal*5.00)/1024;
//float temperature=voltage*100;
//lcd.print(temperature); // print current temperature to LCD
// convert the return value for NTC sensor
float celsius = 1 / (log(1 / (1023. / sensorVal - 1)) / BETA + 1.0 / 298.15) - 273.15;
lcd.print(celsius); // print current temperature to LCD
// print information on Arduino IDE since program started
Serial.print("Time: ");
Serial.print(elapsedTime);
Serial.print("sec, Sensor Value: ");
Serial.print(sensorVal);
//Serial.print(", Volts: ");
//Serial.print(voltage);
Serial.print(", degrees C: ");
//Serial.println(temperature);
Serial.println(celsius);
delay(500);
if(celsius <= baselineTemp - 2.00){
timerStarted = false;
tone(buzzerPin,2000,1000);
delay(1000);
}
}
}