Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Arduino Sound Meter
- */
- //--------------------------------------------------------------------------------------------
- // LIBRARIES
- //--------------------------------------------------------------------------------------------
- #include <Wire.h>
- #include <SPI.h> //SPI Library for OLED
- #include <Adafruit_GFX.h> //Graphics library for OLED
- #include <Adafruit_SH1106.h> //OLED Driver
- //--------------------------------------------------------------------------------------------
- // DEFINES
- //--------------------------------------------------------------------------------------------
- #define OLED_RESET -1 // reset required for SH1106 4
- /*
- #define OLED_MOSI 9 //SoftSPI MOSI on D9
- #define OLED_CLK 10 //SoftSPI CLK on D10
- #define OLED_DC 11 //SoftSPI DC on D11
- #define OLED_CS 12 //SoftSPI CS on D12
- #define OLED_RESET 13 //OLED Reset in d13
- */
- //--------------------------------------------------------------------------------------------
- // LIBRARY
- //--------------------------------------------------------------------------------------------
- Adafruit_SH1106 display(OLED_RESET); // reset required for SH1106
- //Create instance of OLED called display
- //Adafruit_SH1106 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
- //--------------------------------------------------------------------------------------------
- // GLOBAL VARIABLES
- //--------------------------------------------------------------------------------------------
- const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz) 50
- unsigned int sample;
- const int sample1Window = 250; // Sample window width in mS (50 mS = 20Hz) 50
- unsigned int sample1;
- //--------------------------------------------------------------------------------------------
- // SETUP
- //--------------------------------------------------------------------------------------------
- void setup()
- {
- Serial.begin(9600); //Serial comms for debugging
- display.begin(SH1106_SWITCHCAPVCC); //OLED display start
- display.display(); //show buffer
- display.clearDisplay(); //clear buffer
- display.setRotation(1);
- display.setTextSize(1); //Set text size to 1 (1-6)
- display.setTextColor(WHITE); //Set text color to WHITE (no choice lol)
- display.setCursor(0,0); //cursor to upper left corner
- display.println(" VU Meter"); //write title
- display.display(); //show title
- delay(2000); //wait 2 seconds
- }
- //--------------------------------------------------------------------------------------------
- // LOOP
- //--------------------------------------------------------------------------------------------
- void loop()
- {
- unsigned long startMillis= millis(); // Start of sample window
- float peakToPeak = 0; // peak-to-peak level
- unsigned int signalMax = 0; //minimum value
- unsigned int signalMin = 1024; //maximum value
- // collect data for 50 mS
- while (millis() - startMillis < sampleWindow)
- {
- sample = analogRead(0); //get reading from microphone
- if (sample < 1024) // toss out spurious readings 1024
- {
- if (sample > signalMax)
- {
- signalMax = sample; // save just the max levels
- }
- else if (sample < signalMin)
- {
- signalMin = sample; // save just the min levels
- }
- }
- }
- peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
- 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);
- float db1 = map(peakToPeak,100,900,0,200);
- display.setRotation(1);
- display.setCursor(10,0); //cursor to upper left
- display.setTextSize(1); //set text size to 2
- display.print(db1); //write calibrated deciBels
- display.print(" dB"); //write units
- display.setRotation(0);
- for(int x =5;x<114;x=x+6){ //nakreslete měřítko
- display.drawLine(x, 35, x, 27, WHITE);
- }
- display.drawRoundRect(0, 12, 120, 40, 0, WHITE); //nakreslete obrys sloupcového grafu
- int r = map(db,0,120,1,120); //nastavit sloupcový graf pro šířku obrazovky
- int r1 = map(db,0,120,1,120);
- 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);
- // if (millis()%4 == 0)
- // display.drawRoundRect(1,13,r1,38,0,WHITE);
- display.display(); //show all that we just wrote & drew
- display.clearDisplay(); //clear the display
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement