Advertisement
Attilator

OLED Spektr analyzer U8G2_SH1106

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