I want to use this working code on Adafruit_SSD1306.h, to U8GLIB_SH1106_128X64 u8g

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

}

Would really help if you posted the sketch properly.
Looks like U8glib is using a page buffer, you need to use the "picture loop" technique to update the display:


u8g.firstPage();
do {
  u8g.drawLine(a1, a2, hMeter, vMeter); // draws needle
} while ( u8g.nextPage() );

Thank You David.

The following sketch is working with my 0.9 oled display, I want to run the same sketch on my new 1.3 inch Oled. I am just a beginner to coding.... can u please help.

thanks again :smiley:

#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[] = {
//----------------------------------------------------------------------------------------

B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00001111,B00000000,
B00000000,B00010111,B10000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00001111,B00000000,B00000000,B00000000,B00000001,B00001000,B00000000,
B00000000,B00110100,B10000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00001001,B00000000,B00000000,B00000000,B00000001,B00001110,B00000000,
B00000111,B10010100,B10000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00001001,B00000000,B00000000,B00000000,B00000111,B11000001,B00000000,
B00000111,B10010100,B10000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00001001,B00000000,B00000000,B00000000,B00000001,B00001001,B00000000,
B00000000,B00010111,B10000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00001111,B00000000,B00000000,B00000000,B00000001,B00000110,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B01111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111110,
B01111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111110,B00000000,B00000000,B00000000,B00000000,B00000000,B00000010,
B01111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111110,B00000000,B00000000,B00000000,B00000000,B00000000,B00000010,
B01111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111110,B00000000,B00000000,B00000000,B00000000,B00000000,B00000010,
B01111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111111,B11111110,
B00000000,B00010010,B01001000,B00000100,B00000010,B00000000,B10000100,B00100001,B00001000,B00000010,B00000000,B10000000,B01001010,B10100100,B00001000,B00000000,
B00000000,B00010010,B01001000,B00000100,B00000010,B00000000,B10000100,B00100001,B00001000,B00000010,B00000000,B10000000,B01001010,B10100100,B00001000,B00000000,
B00000000,B00010010,B01001000,B00000100,B00000010,B00000000,B10000000,B00000000,B00000000,B00000010,B00000000,B10000000,B01000000,B00000100,B00001000,B00000000,
B00000000,B00000000,B00001000,B00000100,B00000010,B00000000,B10000000,B00000000,B00000000,B00000010,B00000000,B10000000,B01000000,B00000100,B00001000,B00000000,
B00000000,B00000000,B00001000,B00000100,B00000010,B00000000,B10000000,B00000000,B00000000,B00000010,B00000000,B10000000,B01000000,B00000100,B00001000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00010000,B10100010,B00001000,B10000100,B00011100,B00100100,B10000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00010000,B10100010,B00001101,B10001010,B00100010,B00100100,B10000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00001001,B00100010,B00001101,B10001010,B00100000,B00100011,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00001001,B00100010,B00001101,B10001010,B00100110,B00100011,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00001001,B00100010,B00001010,B10001110,B00100010,B00100011,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000110,B00100010,B00001010,B10010001,B00100010,B00100100,B10000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000110,B00011100,B00001010,B10010001,B00011100,B00100100,B10000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,
B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000

//------------------------------------------------------------------------------------

};

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();
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.