Character Glitches when printing from an analogRead function?

Hey everyone,

New to the Arduino world here, but I have been fumbling my way through some examples and tweaking codes here and there to get a DIYMall SSD1306 128x64 OLED working (yellow/blue version).

I am using the u8g.print function to display data from an i2c-based 3-axis accelerometer (MMA8452), and from an infrared distance sensor via analogRead and some math.

The trouble I have is that I can’t understand why the display of the acceleration values look OK, but the display of the distance sensor has graphical glitches. I am linking to a short youtube video of the display, along with copying my code below. Am I doing something wrong here? I am guessing it is just too “fast” for the i2c bus, but this is all pretty new to me…

OLED: http://www.amazon.com/gp/product/B00O2LLT30?psc=1&redirect=true&ref_=oh_aui_detailpage_o01_s03

IR Sensor: http://www.amazon.com/gp/product/B00IMOSEJA?psc=1&redirect=true&ref_=oh_aui_detailpage_o01_s02

Accelerometer: http://www.amazon.com/gp/product/B00L8WG26A?psc=1&redirect=true&ref_=oh_aui_detailpage_o01_s01

CODE:

#include <U8glib.h>
#include <Wire.h>
#include <SparkFun_MMA8452Q.h> // Includes the SFE_MMA8452Q library


const int dpin = A0;                  // Distance sense pin

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);  // I2C / TWI 

// Begin using the library by creating an instance of the MMA8452Q
//  class. We'll call it "accel". That's what we'll reference from
//  here on out.
MMA8452Q accel;

// The setup function simply starts serial and initializes the
//  accelerometer.

void setup(void) {
  // initialize the serial communications:
  Serial.begin(9600);  

  // u8g.setFont(u8g_font_fub30);
     u8g.setFont(u8g_font_fur14);

  Serial.println("MMA8452Q Test Code!");
  
  accel.init();

  
}

void loop(void) {
  // Use the accel.available() function to wait for new data
  //  from the accelerometer.
  if (accel.available())
  {
    // First, use accel.read() to read the new variables:
    accel.read();

  }


//Picture Loop
  u8g.firstPage();  
  do {
    draw();
   
  } while( u8g.nextPage() );
// rebuild the picture after some delay
// delay(200);
}
void draw(void) {
  u8g.setFont(u8g_font_fur14);
   u8g.drawStr( 10, 16, "X");
   u8g.drawStr( 60, 16, "Y");
   u8g.drawStr( 110, 16, "Z");
    u8g.setFont(u8g_font_fur11);
    u8g.setPrintPos( 0, 38);
    u8g.print(accel.cx,2);u8g.print("g");
    // u8g.print("\xb0");
    u8g.setPrintPos( 45, 38);
    u8g.print(accel.cy,2);u8g.print("g");
    u8g.setPrintPos( 90, 38);
    u8g.print(accel.cz,2);u8g.print("g");
    u8g.setFont(u8g_font_fur11);
    int sensorvalue = analogRead(dpin);
    int dist = (6762/(sensorvalue-9))-4;
    u8g.setPrintPos( 0, 60);
    u8g.print(dist);u8g.print(" cm");

}

Hi and welcome.

This kind of question pops up regularly. Most of the times this is caused by too fast updates to the screen. You are reading values, and immediately after that the value is (re)drawn to the screen. Perhaps you should evaluate the values before you draw them to the screen. If the value is already on the screen, then you do not need to redraw it. Do you realise how fast the values come in ?

You can test this by adding a delay (just for this testing purpose) somewhere in "loop".

Thanks for the feedback! Your emphasis on the I was able to fix it by moving the analog read function outside the picture loop and playing with a delay.