I am trying to get a spectrum analyser up and running. I found the following code on Galileo GEN2 Project - Real-Time Audio Spectrum Analyzer1:
/*
*
* Galileo GEN2 Project - Real-Time Audio Spectrum Analyzer
* https://communities.intel.com/docs/DOC-23406
*
*
* ERROR: analogReadResolution() ???
*
*/
#include <stdint.h> //For New Arduino
#include "PCD8544.h"
#define pin_adc 0
#include "fix_fft.h"
char im[128];
char data[128];
int analog_value[128];
int i=0;
PCD8544 nokia = PCD8544(3, 4, 5, 7, 6);
// pin 7 - Serial clock out (SCLK) connects Dig3
// pin 6 - Serial data out (DIN) connects Dig4
// pin 5 - Data/Cmd. selct. (D/C) connects Dig5
// pin 4 - LCD reset (RST) connects Dig6
// pin 3 - LCD chip select (CS) connects Dig7
void setup() {
nokia.init();
nokia.command(PCD8544_DISPLAYCONTROL | PCD8544_DISPLAYNORMAL);
nokia.clear();
analogReadResolution(8);
}
void show_big_bars(char *spektrum) {
int spek_for_draw;
nokia.drawline(10, 15, 10, 39, BLACK);
for (byte i = 1; i < 64; i++){ // Skip 0 Channel
// Serial.print(spektrum[i],BYTE);
spek_for_draw = 1.5*(spektrum[i]); // 0/2.5 ~ 255/2.5
if (spek_for_draw > 39) spek_for_draw = 39;
nokia.drawline(i+10, 39 - spek_for_draw, i+10, 39, BLACK);
}
//nokia.drawstring(5, 5, "0 09 22 36 49");
nokia.drawstring(5, 5, "0");
nokia.drawstring(21, 5, "4");
nokia.drawstring(37, 5, "8");
nokia.drawstring(48, 5, "12");
nokia.drawstring(67, 5, "18");
nokia.drawstring(5, 0, "Spectrum");
nokia.drawstring(5, 1,"* 0.1KHz");
nokia.display(); // Update Screen
nokia.clear(); // Clear Screen for the animation
}
void loop(){
static long tt;
int val;
if (i < 128){
analog_value[i] = analogRead(pin_adc);
i++;
}
else{
for (i=0; i< 128;i++){ //Prepare data for FFT
data[i] = analog_value[i] - 128;
im[i] = 0;
}
//this could be done with the fix_fftr function without the im array.
fix_fft(data,im,7,0);
// I am only interessted in the absolute value of the transformation
for (i=0; i< 64;i++)
data[i] = sqrt(data[i] * data[i] + im[i] * im[i]);
//do something with the data values 1..64 and ignore im
show_big_bars(data);
i=0;
}
}
When I attempt to compile the above code, I get the error
Arduino: 1.6.5 (Mac OS X), Board: "Arduino Micro"
fft_galileo.ino: In function 'void setup()':
fft_galileo:31: error: 'analogReadResolution' was not declared in this scope
'analogReadResolution' was not declared in this scope
When I search for the location of analogReadResolution()
on Google, I repeatedly get the information that it is only available for the Due and Zero. From Arduino - AnalogReadResolution:
analogReadResolution() is an extension of the Analog API for the Arduino Due and Zero.
I was wondering if I needed to add an #include
statement but all of the code examples that I found do not have any specific #include
for the Analogue library.
Also, thinking maybe it is board specific (as the video Arduino Due error: 'analogReadResolution' was not declared in this scope suggests), I selected the Duemilanove board (as the Due is not on offer in my "Board" menu), but I still get the same error. I have even tried the Arduino NG or older Board menu item, but with the same results.
So, how do I get rid of this compile time error? Or, more poignantly, how can I compile this code for my Uno or Leonardo board. Should I just comment it out? If I do comment the line out then the code does compile, so does this mean that analogReadResolution()
is not required any longer?
If it isn't required, then why does this Uno specific example, How to Use ADC in Arduino Uno?, state:
As default we get the maximum board ADC resolution which is 10bits, this resolution can be changed by using instruction (“analogReadResolution(bits);”). This resolution change can come in handy for some cases.
How can the resolution be changed if the method has, indeed, been deprecated?
Also, why do I not have a "Due" in my Board menu?
1 For completeness fix_fft.h
and fix_fft.cpp
can be found on TJC's github and here is the PCD8544 Arduino library for Nokia 5510 LCD library. The password for decompressing Intel_Nokia5110.rar
file is 406.
Zero Uno Due Tre
- that isZero One Two Three
.