OLED + serial port dont work together

Hi,
I have problem with my OLED display connected to Arduino Nano (328P)
I use OLED with SH1106 over SPI. When I try to display static chars everithing is OK, but when I send data to arduino over USB from PC, picture get corrupted.
If I use smaller font everithing is OK.

Same code - different constructor works fine with I2C OLED.

#include "U8glib.h"
U8GLIB_SH1106_128X64 ffDisp(10, 9);	// SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);	// I2C / TWI 
//U8GLIB_SH1106_128X64 ffDisp(U8G_I2C_OPT_NO_ACK);	// Display which does not send ACK

char FuelFlow[5];
int FF_CHAR_W = 25;
int FF_CHAR_H = 45;
int FF_V_CONST = 7;
int FF_H_CONST = 0;

 
void readFF() {
  Serial.print('F');
  Serial.flush();
  Serial.readBytes(FuelFlow, 5) ;
}

void drawFF() {
  ffDisp.setFont(u8g_font_fur35n);
  char FFT[2];
  FFT[0] =  FuelFlow[0];
  FFT[1] =  FuelFlow[1];
  char FFh = FuelFlow[2];
  char FFhPriv;
  if (FuelFlow[2] == 48) {
    FFhPriv = 57;
  } else {
    FFhPriv = FuelFlow[2] - 1;
  }
 
  char FFhNext;
  if (FuelFlow[2] == 57) { //a
    FFhNext = 48;
  } else {
    FFhNext = FuelFlow[2] + 1;
  }
 
  char FFhTwoOver;
  if (FFhNext == 57) {
    FFhTwoOver = 48;
  } else {
    FFhTwoOver = FFhNext + 1;
  }
 
  short offset = short((FuelFlow[3] - 48) * FF_CHAR_H / 10); //it's a char - Ascii value of '0'
  //short offset = short((FuelFlow[3] - 48) * 60 / 10); //it's a char - Ascii value of '0'
  ffDisp.firstPage();
  do {
    //ffDisp.drawStr((FF_CHAR_W * 1) + FF_H_CONST, short(FF_CHAR_H * 1) + FF_V_CONST, FFT);
    ffDisp.setPrintPos((FF_CHAR_W * 0) + FF_H_CONST, short((FF_CHAR_H * 1)) + FF_V_CONST);
    ffDisp.print(FFT[0]);
    ffDisp.setPrintPos((FF_CHAR_W * 1) + FF_H_CONST, short((FF_CHAR_H * 1)) + FF_V_CONST);
    ffDisp.print(FFT[1]);
    //ffDisp.setPrintPos((FF_CHAR_W * 2) + FF_H_CONST, short((FF_CHAR_H * -1)) + offset + FF_V_CONST);
    //ffDisp.print(FFhTwoOver);
    ffDisp.setPrintPos((FF_CHAR_W * 2) + FF_H_CONST, short((FF_CHAR_H * 0)) + offset + FF_V_CONST);
    ffDisp.print(FFhNext);
    ffDisp.setPrintPos((FF_CHAR_W * 2) + FF_H_CONST, short((FF_CHAR_H * 1)) + offset + FF_V_CONST);
    ffDisp.print(FFh);
    ffDisp.setPrintPos((FF_CHAR_W * 2) + FF_H_CONST, short((FF_CHAR_H * 2)) + offset + FF_V_CONST);
    ffDisp.print(FFhPriv);
 
    ffDisp.drawStr((FF_CHAR_W * 3) + FF_H_CONST, short(FF_CHAR_H * 1) + FF_V_CONST, "00");
  } while ( ffDisp.nextPage() );
}


void setup(void) {
  Serial.begin(9600);
}

void loop(void) {

  ffDisp.firstPage();  
  do {
    readFF();
    drawFF();
  } while( ffDisp.nextPage() );
  delay(1);
}

Hi bulka

Serial.readBytes(FuelFlow, 5) ;

This statement will return 5 bytes of received data, but fewer bytes if 5 have not been received within the (default 1 second) serial timeout from when you call readBytes().

Your code uses bytes 0 to 3 of FuelFlow. Maybe the problems are happening because not enough bytes were received in time, so of array elements are undefined?

You might be better to use Serial.available() to check if a byte has been received and Serial.read() to read each byte and store it in the character array.

Check out these:

http://forum.arduino.cc/index.php?topic=236162.0 http://www.gammon.com.au/forum/?id=11425

Regards

Ray

Problem SOLVED. Seller on ebay sent us display with wrong driver - SSD1306 instead of SH1106 written in their description