Problem using an SSD1306 I2C display

Dear All,

New as Arduino user, I have a problem using a SSD1306 I2C display.

I wrote a sketch nicely working for a test bench. The source code is below.

I need now to add a display showing the number of cycles executed by the sketch.

If the “Display();” statement in line N° 62 is put under comments, everything works fine. If I uncomment

it, the sketch didn’t loop. Could someone help me ? Thanks.

Sketch :

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#if (SSD1306_LCDHEIGHT != 64)
#error(“Height incorrect, please fix Adafruit_SSD1306.h!”);
#endif

int durationZDown = 75;
int durationZUp = 45;
int durationXForward = 45; // 60
int durationXBackward = 45; // 60
int durationPause = 10;
int courseX = 10000; // max 5000
int courseZ = 4000; // max 6000
int seq = 0;
int Xposition = 0;
int Cycles = 0;

void setup() // Initialization
{
Serial.begin(19200);
display.begin(SSD1306_SWITCHCAPVCC, 0x3c); // initialize with the I2C addr 0x3D (for the 128x64)
// Clear the buffer.
display.clearDisplay();
display.display();

pinMode(2, OUTPUT); // Z axis direction
pinMode(3, OUTPUT); // Z axis steps

pinMode(4, OUTPUT); // X axis direction
pinMode(5, OUTPUT); // X axis steps

pinMode(6, INPUT); // X axis detection sample pipetting position
pinMode(7, INPUT); // X axis detection preparation rotor pipetting position
pinMode(8, INPUT); // Z axis detection top position

digitalWrite(2, HIGH); // Z axis direction
digitalWrite(3, HIGH); // Z axis steps

digitalWrite(4, LOW); // X axis direction
digitalWrite(5, LOW); // X axis steps

BeginOfTest();

RazV(); // Set Z arm on his top position
RazH(); // Set X arm on his backward position

delay(durationPause*50);

Cycles = 0;
}

void loop()
{
Testing();
Cycles=Cycles + 1;
//Display();
}

void BeginOfTest()
{
// text display tests
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("- ***** -");
display.setCursor(0,32);
display.setTextSize(1);
display.println(“The test will start”);
display.setCursor(0,40);
display.println(" soon");
display.setTextColor(BLACK, WHITE); // ‘inverted’ text
display.setTextSize(2);
display.setTextColor(WHITE);
display.display();
delay(2000);
display.clearDisplay();
display.display();
}

void Display()
{
display.setCursor(0,0);
display.setCursor(30, 30);
display.println(Cycles);
display.display();
display.clearDisplay();
display.display();
}

void EndOfTest()
{
Cycles=0;
do
{
display.clearDisplay();
display.setCursor(0,0);
display.println(" Test");
display.setCursor(0,30);
display.println(“achieved !”);
display.display();
delay(500);
display.clearDisplay();
display.display();
delay(500);
}
while(Cycles=1);
}

void Testing()
{
//===================================================================================================================
// action : move X axis forward --------------------------------------------------------------------------

digitalWrite(5, LOW);
delayMicroseconds(durationXForward/4);
digitalWrite(5, HIGH);
delayMicroseconds(durationXForward/4);
if (digitalRead(4)== LOW)
{
Xposition = Xposition +1;

if (Xposition == 5800)
{
//delay(durationPause*100);
courseZ = 4400;
Pipetting();
courseZ = 4000;
}
}
else
{
Xposition = Xposition -1;
}

if (digitalRead(7)== 0)
{
digitalWrite(4, HIGH);
delay(durationPause);
//=========================================================================================================================
//Emplacement pour la routine de pipettage
//=========================================================================================================================
if (seq==0)
{
courseZ = 9200;
Pipetting();
seq=1;
courseZ = 4000;
}
}
if (digitalRead(8)== 0)
{
digitalWrite(4, LOW);
delay(durationPause);
//=========================================================================================================================
//Emplacement pour la routine de pipettage
//=========================================================================================================================
if (seq==1)
{

courseZ = 2000;
Pipetting();
seq=0;
courseZ = 4000;
}
}
}

void RazH() //
{
//===================================================================================================================
// action : move X axis backward ------------------------------------------------------------------------------------
digitalWrite(4, HIGH);
do
{
digitalWrite(5, LOW);
delayMicroseconds(durationXBackward/4);
digitalWrite(5, HIGH);
delayMicroseconds(durationXBackward/4);
}
while (digitalRead(8) != 0);
digitalWrite(4, LOW);
delay(durationPause);
}

void RazV()
{
//===================================================================================================================
// action : move Z axis upward --------------------------------------------------------------------------------------
digitalWrite(2, HIGH);
delay(durationPause);
do
{
digitalWrite(3, LOW);
delayMicroseconds(durationZUp/4);
digitalWrite(3, HIGH);
delayMicroseconds(durationZUp/4);
}
while (digitalRead(6) != 0);
digitalWrite(2, LOW);
delay(durationPause);
}

void Pipetting()
{ //===================================================================================================================
// action : move Z down, pump in or pump out, move Z up -------------------------------------------------------------
do
{
digitalWrite(3, LOW);
delayMicroseconds(durationZUp);
digitalWrite(3, HIGH);
delayMicroseconds(durationZUp/2.5);
}
while (digitalRead(6) != 0);
digitalWrite(2, LOW);
for (int i=0; i <= courseZ; i++)
{
digitalWrite(3, LOW);
delayMicroseconds(durationZDown);
digitalWrite(3, HIGH);
delayMicroseconds(durationZDown/2.5);
}
digitalWrite(2, HIGH);
delay(durationPause*100);
do
{
digitalWrite(3, LOW);
delayMicroseconds(durationZUp/4);
digitalWrite(3, HIGH);
delayMicroseconds(durationZUp/4);
}
while (digitalRead(6) != 0);
digitalWrite(2, LOW);
//exit(0);
}

I think the problem is here:

void Display()
{
  display.setCursor(0,0);
  display.setCursor(30, 30);
  display.println(Cycles);
  display.display();
    ????????
  display.clearDisplay();
  display.display();
}

You write the content to the display, you clear the buffer and IMMEDIATELY write to the display. So you basically clear the display.

If you wait 2 sec, it works:

void BeginOfTest()
{
// text display tests
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("- ***** -");
  display.setCursor(0,32);
  display.setTextSize(1);
  display.println("The test will start");
  display.setCursor(0,40);
  display.println("       soon");
  display.setTextColor(BLACK, WHITE); // 'inverted' text
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.display();
      delay(2000);
  display.clearDisplay();
  display.display();  
}

Dear mischnic,
Thank you for your reply. I was travelling so I could not make some test until today. I have tried, but I still have the same problem, the loop starts, display "1" and stops.
It seems that the problem could be linked with interrupt. Any other idea ?

There could be a active infinite while loop and the sketch would not continue running. But that would be the case not only of Display() is called. Please insert some Serial.println() calls between the instructions and determine where the code is interrupted!

Please ignore the following text if EndOfTest() is not used:

I don't think   while(Cycles=1); is intended as it is because it would always return 0 and not stop the sketch. You should use ==. Also you create a local "Cycles" variable which has the same name as the global variable and your not changing it's value inside the do-while loop.