Advertisement
Vladimir69

test_vu

Jul 25th, 2021
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. /*
  2. * Arduino Sound Meter
  3. */
  4. //--------------------------------------------------------------------------------------------
  5. // LIBRARIES
  6. //--------------------------------------------------------------------------------------------
  7. #include <Wire.h>
  8. #include <SPI.h> //SPI Library for OLED
  9. #include <Adafruit_GFX.h> //Graphics library for OLED
  10. #include <Adafruit_SH1106.h> //OLED Driver
  11.  
  12. //--------------------------------------------------------------------------------------------
  13. // DEFINES
  14. //--------------------------------------------------------------------------------------------
  15. #define OLED_RESET -1 // reset required for SH1106 4
  16.  
  17.  
  18. /*
  19. #define OLED_MOSI 9 //SoftSPI MOSI on D9
  20. #define OLED_CLK 10 //SoftSPI CLK on D10
  21. #define OLED_DC 11 //SoftSPI DC on D11
  22. #define OLED_CS 12 //SoftSPI CS on D12
  23. #define OLED_RESET 13 //OLED Reset in d13
  24. */
  25. //--------------------------------------------------------------------------------------------
  26. // LIBRARY
  27. //--------------------------------------------------------------------------------------------
  28. Adafruit_SH1106 display(OLED_RESET); // reset required for SH1106
  29. //Create instance of OLED called display
  30. //Adafruit_SH1106 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
  31.  
  32. //--------------------------------------------------------------------------------------------
  33. // GLOBAL VARIABLES
  34. //--------------------------------------------------------------------------------------------
  35.  
  36. const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz) 50
  37. unsigned int sample;
  38. const int sample1Window = 250; // Sample window width in mS (50 mS = 20Hz) 50
  39. unsigned int sample1;
  40.  
  41. //--------------------------------------------------------------------------------------------
  42. // SETUP
  43. //--------------------------------------------------------------------------------------------
  44.  
  45. void setup()
  46. {
  47. Serial.begin(9600); //Serial comms for debugging
  48. display.begin(SH1106_SWITCHCAPVCC); //OLED display start
  49. display.display(); //show buffer
  50. display.clearDisplay(); //clear buffer
  51. display.setRotation(1);
  52. display.setTextSize(1); //Set text size to 1 (1-6)
  53. display.setTextColor(WHITE); //Set text color to WHITE (no choice lol)
  54. display.setCursor(0,0); //cursor to upper left corner
  55. display.println(" VU Meter"); //write title
  56. display.display(); //show title
  57. delay(2000); //wait 2 seconds
  58. }
  59.  
  60. //--------------------------------------------------------------------------------------------
  61. // LOOP
  62. //--------------------------------------------------------------------------------------------
  63.  
  64. void loop()
  65. {
  66. unsigned long startMillis= millis(); // Start of sample window
  67. float peakToPeak = 0; // peak-to-peak level
  68.  
  69. unsigned int signalMax = 0; //minimum value
  70. unsigned int signalMin = 1024; //maximum value
  71.  
  72. // collect data for 50 mS
  73. while (millis() - startMillis < sampleWindow)
  74. {
  75. sample = analogRead(0); //get reading from microphone
  76. if (sample < 1024) // toss out spurious readings 1024
  77. {
  78. if (sample > signalMax)
  79. {
  80. signalMax = sample; // save just the max levels
  81. }
  82. else if (sample < signalMin)
  83. {
  84. signalMin = sample; // save just the min levels
  85. }
  86. }
  87. }
  88. peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
  89. float db = map(peakToPeak,0,124,0,124); //kalibrovat pro deciBels float db = map(peakToPeak,20,900,49.5,90); map(peakToPeak,0,124,0,124);
  90. float db1 = map(peakToPeak,100,900,0,200);
  91. display.setRotation(1);
  92. display.setCursor(10,0); //cursor to upper left
  93. display.setTextSize(1); //set text size to 2
  94. display.print(db1); //write calibrated deciBels
  95. display.print(" dB"); //write units
  96.  
  97. display.setRotation(0);
  98. for(int x =5;x<114;x=x+6){ //nakreslete měřítko
  99. display.drawLine(x, 35, x, 27, WHITE);
  100. }
  101.  
  102. display.drawRoundRect(0, 12, 120, 40, 0, WHITE); //nakreslete obrys sloupcového grafu
  103. int r = map(db,0,120,1,120); //nastavit sloupcový graf pro šířku obrazovky
  104. int r1 = map(db,0,120,1,120);
  105. display.fillRoundRect(1,13,r,38,0,WHITE); //nakreslete sloupcový graf o šířce r (1, 13, r, 38, 0, WHITE) display.fillRoundRect(1,13,r,38,0,WHITE);
  106.  
  107. // if (millis()%4 == 0)
  108. // display.drawRoundRect(1,13,r1,38,0,WHITE);
  109. display.display(); //show all that we just wrote & drew
  110. display.clearDisplay(); //clear the display
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement