The following code is working ....
#include <Wire.h> // requried to run I2C SH1106
#include <SPI.h> // requried to run I2C SH1106
#include <Adafruit_GFX.h> // GitHub - adafruit/Adafruit-GFX-Library: Adafruit GFX graphics core Arduino library, this is the 'core' class that all our other graphics libraries derive from
#include <Adafruit_SSD1306.h> // GitHub - wonho-maker/Adafruit_SH1106: Adafruit graphic library for SH1106 dirver lcds.
#define OLED_RESET 4 // reset required for SH1106
Adafruit_SSD1306 display(OLED_RESET); // reset required for SH1106
int analogInput = A0; // analog input for outside audio source
int hMeter = 65; // horizontal center for needle animation
int vMeter = 65; // vertical center for needle animation (outside of dislay limits)
int rMeter = 80; // length of needle animation or arch of needle travel
const int sampleWindow = 50; // sample window width in mS (50 mS = 20Hz)
unsigned int sample;
// VU meter background mask image:
static const unsigned char PROGMEM VUMeter[] = {
// Avoiding the graphic here
};
void setup(){
pinMode(analogInput, INPUT); // analog input for outside audio source
display.begin(SSD1306_SWITCHCAPVCC, 0x3c); // needed for SH1106 display
display.clearDisplay(); // clears display from any library info displayed
}
void loop(){
/***********************************************************************
Start of code taken from Adafruit Example Sound Level Sketch for the
Adafruit Microphone Amplifier
************************************************************************/
unsigned long startMillis = millis(); // start of sample window
unsigned int PeaktoPeak = 0; // peak-to-peak level
unsigned int SignalMax = 0;
unsigned int SignalMin = 1024;
while ( millis() - startMillis < sampleWindow ){
sample = analogRead(analogInput);
if (sample < 1024) {
if (sample > SignalMax){
SignalMax = sample; // saves just the max levels
}
else if (sample < SignalMin){
SignalMin = sample; // saves just the min levels
}
}
}
PeaktoPeak = SignalMax - SignalMin; // max - min = peak-peak amplitude
float MeterValue = PeaktoPeak * 330 / 1024; // convert volts to arrow information
/****************************************************
End of code taken from Adafruit Sound Level Sketch
*****************************************************/
MeterValue = MeterValue - 40; // shifts needle to zero position
display.clearDisplay(); // refresh display for next step
display.drawBitmap(0, 0, VUMeter, 128, 32, WHITE); // draws background
int a1 = (hMeter + (sin(MeterValue / 57.296) * rMeter)); // meter needle horizontal coordinate
int a2 = (vMeter - (cos(MeterValue / 57.296) * rMeter)); // meter needle vertical coordinate
display.drawLine(a1, a2, hMeter, vMeter, WHITE); // draws needle
display.display();
}
Next following sketch only shows the background graphics, No needle graph coming ???
(Last line - u8g.drawLine(a1, a2, hMeter, vMeter);
#include "U8glib.h"
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE); // I2C / TWI
int analogInput = A0; // analog input for outside audio source
int hMeter = 65; // horizontal center for needle animation
int vMeter = 65; // vertical center for needle animation (outside of dislay limits)
int rMeter = 80; // length of needle animation or arch of needle travel
const int sampleWindow = 50; // sample window width in mS (50 mS = 20Hz)
unsigned int sample;
const uint8_t rook_bitmap[] PROGMEM = {
// avoided for making the lines less
};
void draw(void) {
// graphic commands to redraw the complete screen should be placed here
u8g.drawBitmapP(0,0,16,128, rook_bitmap);
}
void setup(void) {
pinMode(analogInput, INPUT);
}
void loop(void) {
// picture loop
u8g.firstPage();
do {
draw();
} while( u8g.nextPage() );
// rebuild the picture after some delay
delay(1000);
unsigned long startMillis = millis(); // start of sample window
unsigned int PeaktoPeak = 0; // peak-to-peak level
unsigned int SignalMax = 0;
unsigned int SignalMin = 1024;
while ( millis() - startMillis < sampleWindow ){
sample = analogRead(analogInput);
if (sample < 1024) {
if (sample > SignalMax){
SignalMax = sample; // saves just the max levels
}
else if (sample < SignalMin){
SignalMin = sample; // saves just the min levels
}
}
}
PeaktoPeak = SignalMax - SignalMin; // max - min = peak-peak amplitude
float MeterValue = PeaktoPeak * 330 / 1024; // convert volts to arrow information
int a1 = (hMeter + (sin(MeterValue / 57.296) * rMeter)); // meter needle horizontal coordinate
int a2 = (vMeter - (cos(MeterValue / 57.296) * rMeter)); // meter needle vertical coordinate
u8g.drawLine(a1, a2, hMeter, vMeter); // draws needle
}