Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1.  
  2. #include <math.h>
  3.  
  4. const int B = 4275; // B value of the thermistor
  5. const int R0 = 10000; // R0 = 100k
  6. const int pinTempSensor = A0; // Grove - Temperature Sensor connect to A5
  7. const int mic = A3;
  8. const int buzzer= A2;
  9. bool check = false;
  10.  
  11. void setup() {
  12. Serial.begin(9600);
  13. pinMode(buzzer, OUTPUT);
  14.  
  15. }
  16.  
  17. void loop() {
  18. while (!check) {
  19. //Get the current temperature
  20. float totalTemp = 0;
  21. int totalSound = 0;
  22. for (int i = 0; i < 10; i++) {
  23. //Get the value of the temperature
  24. float temp1 = getTemp();
  25. totalTemp += temp1;
  26.  
  27. //Get the value from the audio signal
  28. int num1 = getSound();
  29. totalSound += num1;
  30.  
  31. delay(500);
  32. }
  33. float avgTemp = totalTemp/10;
  34. int avgSound = totalSound/10;
  35. Serial.print("The temperature is: ");
  36. Serial.println(avgTemp);
  37. Serial.print("The sounds level is: ");
  38. Serial.println(avgSound);
  39.  
  40. //If the temperatue and sound is above a threshold, activate the buzzer
  41. if(avgSound > 15000 && avgTemp > 90)
  42. {
  43. check = true;
  44. }
  45. delay(1000);
  46. }
  47.  
  48. if (check) {
  49. Serial.println("good");
  50. while (true)
  51. {
  52. tone(buzzer, 150, 500);
  53. noTone(buzzer);
  54. tone(buzzer, 150, 250);
  55. delay(500);
  56. }
  57. }
  58.  
  59. }
  60.  
  61. //helper functions
  62.  
  63. float getTemp()
  64. {
  65. int a = analogRead(pinTempSensor);
  66. float R = (float)1023.0/a-1.0;
  67. R = R0*R;
  68. float temp = 1.0/(log(R/R0)/B+1/298.15)-273.15; // convert to temperature via datasheet
  69. float temp1 = temp*1.8 +32;
  70. return temp1;
  71. }
  72.  
  73. int getSound()
  74. {
  75. int soundVal1 = analogRead(mic);
  76. int val = soundVal1;
  77. float num =(float)val/1023;
  78. float num1= num/0.00002;
  79. return num1;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement