// include the library code:
#include <Wire.h>
#include <Ultrasonic.h>
#include "U8glib.h"

Ultrasonic ultrasonic(2,3); //Init an Ultrasonic object
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);  

int Distance=0;
int Pre_Distance=0;
char distance_value_temp[5] = {0};
 
void setup() {
    Serial.begin(9600);
}

void loop()
{
     Distance=ultrasonic.Ranging(CM);//get the current result;
     Serial.println(Distance);
     delay(100);
     if(Distance!=Pre_Distance)
     {
         u8g.firstPage();
         do{
          drawDistance(Distance);
          }while(u8g.nextPage());
         Pre_Distance=Distance;
     }
    delay(1000);
}

void drawDistance(float x){
  u8g.setFont(u8g_font_gdr20r);
  u8g.drawStr(8,30,"D:");
  itoa(x,distance_value_temp,10);
  u8g.drawStr(45,30,distance_value_temp);
  }
