Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Copyright (c) 2019 Shajeeb TM
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE. */
- #include <arduinoFFT.h>
- #include <SPI.h>
- //#include <Adafruit_GFX.h> //graphics library for OLED
- #include <Adafruit_SH1106.h> //OLED driver
- #include <Fonts/Picopixel.h> //selecting specific font from Adafruit GFX library
- // SH1106 HW SPI, SDA-MOSI-11, CLK-13
- #define OLED_DC 6
- #define OLED_CS 7
- #define OLED_RESET 8
- Adafruit_SH1106 display(OLED_DC, OLED_RESET, OLED_CS);
- #define SAMPLES 64 //Must be a power of 2
- #define xres 32 // Total number of columns in the display, must be <= SAMPLES/2
- #define yres 60
- int yvalue;
- double vReal[SAMPLES];
- double vImag[SAMPLES];
- const int buttonPin = 5; // the number of the pushbutton pin
- int state = HIGH; // the current reading from the input pin
- int previousState = LOW; // the previous reading from the input pin
- //unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
- //unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
- arduinoFFT FFT = arduinoFFT(); // FFT object
- void setup() {
- //Serial.begin(9600);
- display.begin(SH1106_SWITCHCAPVCC);
- display.setTextSize(1); //set OLED text size to 1 (1-6)
- display.setTextColor(WHITE); //set text color to white
- display.clearDisplay(); //clear display
- ADCSRA = 0b11100101; // Set the Analog Digital Convetor Status Register A to free running mode and set pre-scalar to 32 (0xe5)
- //ADCSRA = 0b11100100; // changed last bit to change division factor from 32 to 16, to spread the display out of the left edge. It might not be as pure as original but visually provides more width.
- ADMUX = 0b00000000; // use pin A0 and external voltage reference
- pinMode(buttonPin, INPUT);
- delay(50); // wait to get reference voltage stabilized
- /*
- for(int i=0; i<xres; i++)
- {peaks[i]=0;
- Serial.print("peaks[i]: "); Serial.print(peaks[i]); Serial.println(" ");
- }
- */
- }
- void loop() {
- char data_avgs[xres];
- int peaks[xres];
- // ++ Sampling
- for(int i=0; i<SAMPLES; i++)
- {
- while(!(ADCSRA & 0x10)); // wait for ADC to complete current conversion ie ADIF bit set
- ADCSRA = 0b11110101 ; // clear ADIF bit so that ADC can do next operation (0xf5)
- int value = ADC - 512 ; // Read from ADC and subtract DC offset caused value
- vReal[i]= value/8; // Copy to bins after compressing. Division by 8 reduces the amplitude of the measured input so that it can be uniformly (vertical) fit in the display used
- vImag[i] = 0;
- //Serial.print("value: "); Serial.print(value); Serial.println(" ");
- }
- // -- Sampling
- // ++ FFT
- FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
- FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
- FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
- // -- FFT
- // ++ re-arrange FFT result to match with no. of columns on display ( xres )
- int step = (SAMPLES/2)/xres;
- int c=0;
- for(int i=0; i<(SAMPLES/2); i+=step)
- {
- data_avgs[c] = 0;
- for (int k=0 ; k< step ; k++) {
- data_avgs[c] = data_avgs[c] + vReal[i+k];
- }
- data_avgs[c] = data_avgs[c]/step;
- c++;
- }
- // -- re-arrange FFT result to match with no. of columns on display ( xres )
- // ++ send to display according measured value
- display.clearDisplay(); //clear display
- for(int i=0; i<xres; i++)
- {
- data_avgs[i] = constrain(data_avgs[i],0,80); // set max & min values for buckets
- data_avgs[i] = map(data_avgs[i], 0, 80, 0, yres+26); // remap averaged values to yres +16 pixels for the title
- yvalue=data_avgs[i];
- // Serial.print("yvalue: "); Serial.print(yvalue); Serial.println(" ");
- if (yvalue > peaks[i]) peaks[i] = yvalue ;
- peaks[i] = peaks[i]-1;
- /*
- peaks[i] = peaks[i]-1; // decay by one light
- if (yvalue > peaks[i])
- peaks[i] = yvalue ;
- yvalue = peaks[i];
- */
- //Drawing columns
- display.drawLine(i*4, yres, i*4, yres-yvalue, WHITE);
- // display.drawLine(i*4+1, yres, i*4+1, yres-yvalue, WHITE); // 2 pixel thick column
- // display.drawLine(i*4+2, yres, i*4+2, yres-yvalue, WHITE); // 3 pixel thick column
- // display.drawLine(i*4+3, yres, i*4+3, yres-yvalue, WHITE); // 4 pixel thick column
- // display.drawPixel(i*4, yres-peaks[i], WHITE); //Drawing dots for peaks
- }
- // -- send to display according measured value
- display.setFont(&Picopixel);
- display.setCursor(0,4); //set cursor to top of screen
- display.print(" 32 Channel Spectrum Indicator"); //print title to buffer
- display.display(); //show the buffer
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement