/* A program that measures the temperature using a temperature sensor every 5
seconds while simultaneously generating a tune with varying pitches every 2 seconds from
the speaker. You are required to display values of temperature at each time of measurement
and the text “Speaker is on!” right before a tune is generated and the text “Speaker if off!”
right after the tune is turned off. */
/* Notes:
The TMP sensor returns an analogue signal which we need to calibrate.
The sensor is sensitive to temperature and its impedance, hence voltage
across will vary in relation to temperature changes in its immediate
surrounding. We thus need to first convert the raw reading into a voltage,
and then adding the intercept and timing it by the gradient that can be
found on the relevant datasheet to get the temperature reading.
In code, the temperature of the NTC thermistor is calculated by using the Steinhart–Hart equation.
It is considered the best mathematical expression to obtain the resistance vs temperature characteristic
of an NTC thermistor. It can obtain the temperature values that are approximately equal to the actual values
from the instantaneous resistance of the thermistor.
References: https://mechatrofice.com/arduino/measure-temperature-arduino-ntc-thermistor
https://www.youtube.com/watch?v=hD3cR25MbW8
*/
#include "pitches.h"
const int BUZZER_PIN = 7;
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
const float TEMP_NOMINAL = 298.15; // nominal temperature
// Timed events constants in ms
const unsigned long TONE_EVENT_TIME_1 = 2000;
const unsigned long TEMP_EVENT_TIME_2 = 5000;
unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;
void setup() {
// initialize serial communication
Serial.begin(9600);
// sets the pin as output
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
// get the updated time - updates frequently
unsigned long currentTime = millis();
// Event 1 - buzzer/tone
// if the difference between the current and previous time is more than the
// event time, then it is time to play the tone.
if(currentTime - previousTime_1 >= TONE_EVENT_TIME_1) {
//Serial.println(currentTime);
playTone();
// Update timing for the next event
previousTime_1 = currentTime;
}
// Event 2 - read and display temp
// if the difference between the current and previous time is more than the
// event time, then it is time to read/display temp.
if(currentTime - previousTime_2 >= TEMP_EVENT_TIME_2) {
//Serial.println(currentTime);
displayTemp();
// Update timing for the next event
previousTime_2 = currentTime;
}
}
/* Plays a music tone with varying pitches */
void playTone() {
Serial.println("Speaker is on!");
//tone(pin, frequency, duration)
tone(BUZZER_PIN, NOTE_E4);
delay(150);
tone(BUZZER_PIN, NOTE_G4);
delay(150);
tone(BUZZER_PIN, NOTE_E5);
delay(150);
tone(BUZZER_PIN, NOTE_C5);
delay(150);
tone(BUZZER_PIN, NOTE_D5);
delay(150);
tone(BUZZER_PIN, NOTE_G5);
delay(150);
noTone(BUZZER_PIN);
Serial.println("Speaker is off!");
}
void displayTemp() {
// read the analog output of the sensor
int analogValue = analogRead(A0);
// calculate temp value in kelvin
float kelvin = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / TEMP_NOMINAL);
// convert to celsius
float celsius = kelvin - 273.15;
// print results
Serial.println((String)"Temperature in celsius :" + celsius + "°C");
}
mega:SCL
mega:SDA
mega:AREF
mega:GND.1
mega:13
mega:12
mega:11
mega:10
mega:9
mega:8
mega:7
mega:6
mega:5
mega:4
mega:3
mega:2
mega:1
mega:0
mega:14
mega:15
mega:16
mega:17
mega:18
mega:19
mega:20
mega:21
mega:5V.1
mega:5V.2
mega:22
mega:23
mega:24
mega:25
mega:26
mega:27
mega:28
mega:29
mega:30
mega:31
mega:32
mega:33
mega:34
mega:35
mega:36
mega:37
mega:38
mega:39
mega:40
mega:41
mega:42
mega:43
mega:44
mega:45
mega:46
mega:47
mega:48
mega:49
mega:50
mega:51
mega:52
mega:53
mega:GND.4
mega:GND.5
mega:IOREF
mega:RESET
mega:3.3V
mega:5V
mega:GND.2
mega:GND.3
mega:VIN
mega:A0
mega:A1
mega:A2
mega:A3
mega:A4
mega:A5
mega:A6
mega:A7
mega:A8
mega:A9
mega:A10
mega:A11
mega:A12
mega:A13
mega:A14
mega:A15
ntc1:GND
ntc1:VCC
ntc1:OUT
bz1:1
bz1:2
r1:1
r1:2