Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- const int B = 4275; // B value of the thermistor
- const int R0 = 10000; // R0 = 100k
- const int pinTempSensor = A0; // Grove - Temperature Sensor connect to A5
- const int mic = A3;
- const int buzzer= A2;
- bool check = false;
- void setup() {
- Serial.begin(9600);
- pinMode(buzzer, OUTPUT);
- }
- void loop() {
- while (!check) {
- //Get the current temperature
- float totalTemp = 0;
- int totalSound = 0;
- for (int i = 0; i < 10; i++) {
- //Get the value of the temperature
- float temp1 = getTemp();
- totalTemp += temp1;
- //Get the value from the audio signal
- int num1 = getSound();
- totalSound += num1;
- delay(500);
- }
- float avgTemp = totalTemp/10;
- int avgSound = totalSound/10;
- Serial.print("The temperature is: ");
- Serial.println(avgTemp);
- Serial.print("The sounds level is: ");
- Serial.println(avgSound);
- //If the temperatue and sound is above a threshold, activate the buzzer
- if(avgSound > 15000 && avgTemp > 90)
- {
- check = true;
- }
- delay(1000);
- }
- if (check) {
- Serial.println("good");
- while (true)
- {
- tone(buzzer, 150, 500);
- noTone(buzzer);
- tone(buzzer, 150, 250);
- delay(500);
- }
- }
- }
- //helper functions
- float getTemp()
- {
- int a = analogRead(pinTempSensor);
- float R = (float)1023.0/a-1.0;
- R = R0*R;
- float temp = 1.0/(log(R/R0)/B+1/298.15)-273.15; // convert to temperature via datasheet
- float temp1 = temp*1.8 +32;
- return temp1;
- }
- int getSound()
- {
- int soundVal1 = analogRead(mic);
- int val = soundVal1;
- float num =(float)val/1023;
- float num1= num/0.00002;
- return num1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement