Advertisement
Attilator

OLED Spektr analyzer

Jan 31st, 2020
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Arduino Spectrum Analyzer
  3.  *
  4.  * learnelectronics
  5.  * 27 April 2017
  6.  * black magic stolen from CBM80Amiga
  7.  *
  8.  * www.youtube.com/c/learnelectronics
  9.  * arduino1069@gmail.com
  10.  *
  11.  * Fix_FFT library available @ https://github.com/kosme/arduinoFFT
  12.  */
  13.  
  14.  
  15. #include "fix_fft.h"                                  //library to perfom the Fast Fourier Transform
  16.  
  17. #include <SPI.h>                                      //SPI library
  18. //#include <Wire.h>                                     //I2C library for OLED
  19. #include <Adafruit_GFX.h>                             //graphics library for OLED
  20. #include <Adafruit_SH1106.h>                          //OLED driver
  21.  
  22. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  23. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  24.  
  25. // Comment out above, uncomment this block to use hardware SPI, MOSI-11, CLK-13
  26. #define OLED_DC     6
  27. #define OLED_CS     7
  28. #define OLED_RESET  8
  29. Adafruit_SH1106 display(OLED_DC, OLED_RESET, OLED_CS);
  30.  
  31.  
  32. //#define OLED_RESET 4                                  //OLED reset, not hooked up but required by library
  33.  
  34. char im[128], data[128];                              //variables for the FFT
  35. char x = 0, ylim = 60;                                //variables for drawing the graphics
  36. int i = 0, val;                                       //counters
  37.  
  38. void setup()
  39. {
  40.   Serial.begin(9600);                                 //serial comms for debuging
  41. //  display.begin(SSD1306_SWITCHCAPVCC,0x3C);           //begin OLED @ hex addy 0x3C
  42.   display.begin(SH1106_SWITCHCAPVCC);
  43.   display.setTextSize(1);                             //set OLED text size to 1 (1-6)
  44.   display.setTextColor(WHITE);                        //set text color to white
  45.   display.clearDisplay();                             //clear display
  46.   analogReference(DEFAULT);                           // Use default (5v) aref voltage.
  47. };
  48.  
  49. void loop()
  50. {
  51.   int min=1024, max=0;                                //set minumum & maximum ADC values
  52.   for (i = 0; i < 128; i++) {                         //take 128 samples
  53.     val = analogRead(A0);                             //get audio from Analog 0
  54.     data[i] = val / 4 - 128;                          //each element of array is val/4-128
  55.     im[i] = 0;                                        //
  56.     if(val>max) max=val;                              //capture maximum level
  57.     if(val<min) min=val;                              //capture minimum level
  58.   };
  59.    
  60.   fix_fft(data, im, 7, 0);                            //perform the FFT on data
  61.  
  62.   display.clearDisplay();                             //clear display
  63.   for (i = 1; i < 64; i++) {                          // In the current design, 60Hz and noise
  64.     int dat = sqrt(data[i] * data[i] + im[i] * im[i]);//filter out noise and hum
  65.     display.drawLine(i*2 + x, ylim, i*2 + x, ylim - dat, WHITE); // draw bar graphics for freqs above 500Hz to buffer
  66.   };                                                
  67.   display.setCursor(0,0);                             //set cursor to top of screen
  68.   display.print("->Spectrum Analyzer<-");             //print title to buffer
  69.   display.display();                                  //show the buffer
  70. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement