Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
34 views

Ear Project Final Code Arduino

This document contains code for an Arduino sound level meter that displays decibel readings on an OLED display. It initializes the OLED display and sets the font size. It then enters a main loop where it takes samples from the microphone over a 50ms window, calculates the peak-to-peak amplitude, maps this to decibels, and displays the reading and a bar graph on the OLED. It draws a scale and outline for the bar graph before filling it proportionately based on the decibel level.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Ear Project Final Code Arduino

This document contains code for an Arduino sound level meter that displays decibel readings on an OLED display. It initializes the OLED display and sets the font size. It then enters a main loop where it takes samples from the microphone over a 50ms window, calculates the peak-to-peak amplitude, maps this to decibels, and displays the reading and a bar graph on the OLED. It draws a scale and outline for the bar graph before filling it proportionately based on the decibel level.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <SPI.

h>

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#define OLED_RESET 4

Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);

//--------------------------------------------------------------------------------------------

// GLOBAL VARIABLES

//--------------------------------------------------------------------------------------------

const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)

unsigned int sample;

//--------------------------------------------------------------------------------------------

// SETUP

//--------------------------------------------------------------------------------------------

void setup()

Serial.begin(9600); //Serial comms for debugging

display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //OLED display start

display.display(); //show buffer

display.clearDisplay(); //clear buffer

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(" Arduino Sound Meter"); //write title


display.display(); //show title

delay(2000); //wait 2 seconds

//--------------------------------------------------------------------------------------------

// MAIN 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

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,20,900,49.5,90); //calibrate for deciBels

display.setCursor(0,0); //cursor to upper left

display.setTextSize(2); //set text size to 2

display.print((db-45)*5); //write calibrated deciBels

display.print(" dB"); //write units

for(int x =5;x<114;x=x+6){ //draw scale

display.drawLine(x, 32, x, 27, WHITE);

display.drawRoundRect(0, 32, 120, 20, 6, WHITE); //draw outline of bar graph

int r = map(((db-45)*5),0,120,1,120); //set bar graph for width of screen

display.fillRoundRect(1, 33, r, 18, 6, WHITE); //draw bar graph with a width of r

display.display(); //show all that we just wrote & drew

display.clearDisplay(); //clear the display

You might also like