voidash

Untitled

Apr 5th, 2022
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. // SparkFun Electret Microphone Breakout - BOB-12758 use gareko ho
  2. //https://support.arduino.cc/hc/en-us/articles/360014869820-How-to-install-the-Arduino-Create-Agent
  3. // https://forum.arduino.cc/t/analog-read-value-db/307667/2
  4.  
  5. #include "thingProperties.h"
  6.  
  7.  
  8. int adc;
  9. int adc_ac;
  10. int volts;
  11. float decibelValue;
  12. int seconds;
  13. float bias; // when microphone is quiet how much does it read?
  14. float peak_value;
  15.  
  16.  
  17. void setup() {
  18. // Initialize serial and wait for port to open:
  19. Serial.begin(9600);
  20. // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  21. delay(1500);
  22.  
  23. // Defined in thingProperties.h
  24. initProperties();
  25.  
  26. // Connect to Arduino IoT Cloud
  27. ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  28.  
  29. /*
  30. The following function allows you to obtain more information
  31. related to the state of network and IoT Cloud connection and errors
  32. the higher number the more granular information you’ll get.
  33. The default is 0 (only errors).
  34. Maximum is 4
  35. */
  36. setDebugMessageLevel(2);
  37. ArduinoCloud.printDebugInfo();
  38. }
  39.  
  40. void loop() {
  41.  
  42. bias = 400; //when silent what is the value being read?
  43. peak_value = 1024; // when full noise what is value being read
  44.  
  45.  
  46. ArduinoCloud.update();
  47. adc = analogRead(A0);
  48. adc_ac = adc - bias;
  49. // 5.0 is reference voltage , if you use microphone with different characteristics; check the datasheet
  50. // and replace 5.0 to maybe 3.3 or whatever is present
  51. volts = (5.0 / peak_value ) * adc_ac;
  52. decibelValue = -20.0 * log10(volts) + 6;
  53. seconds = millis() / 1000;
  54. }
  55.  
  56. /*
  57. Since DecibelValue is READ_WRITE variable, onDecibelValueChange() is
  58. executed every time a new value is received from IoT Cloud.
  59. */
  60. void onDecibelValueChange() {
  61. Serial.print("decibel value set to ");
  62. Serial.println(decibelValue);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment