0.96 OLED DISplay with LOOP Issue,,,Need your HELP

I have one Project with OLED and Keypad.If I Press any Number in Keypad it show Correspondin
7displayprob.ino (9.2 KB)
g Value to the all 6 OLED Screen.It s All fine till 5 th Display.
6 th only Continuous Showing All Correspond Value assign With Keypad.\

in previous i Assigned Predefine Value for EachUse code tags to format code for the forum

`

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



const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad

char keymap[numRows][numCols]=
{
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
static const unsigned char PROGMEM logo16_glcd_bmp[] = { B00000000, B11000000, B00000001, B11000000, B00000001, B11000000, B00000011, B11100000, B11110011, B11100000, B11111110, B11111000, B01111110, B11111111, B00110011, B10011111, B00011111, B11111100, B00001101, B01110000, B00011011, B10100000, B00111111, B11100000, B00111111, B11110000, B01111100, B11110000, B01110000, B01110000, B00000000, B00110000 };
#if (SSD1306_LCDHEIGHT != 64)
//#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
char keypressed;

byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3
byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3

//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
int val = 0;

int val1;
int val2;
int val3;
int val4;
int val5;
int val6;


#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Adafruit_SSD1306 display2(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Adafruit_SSD1306 display3(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Adafruit_SSD1306 display4(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Adafruit_SSD1306 display5(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Adafruit_SSD1306 display6(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);


// for each TCA9548A, add an entry with its address
const uint8_t TCA_ADDRESSES[] = {
  0x70,
  0x71,
  0x73
};

const uint8_t TCA_COUNT = sizeof(TCA_ADDRESSES) / sizeof(TCA_ADDRESSES[0]);


void tcaselect(uint8_t tca, uint8_t channel) {
  if (tca >= TCA_COUNT) return;
  if (channel > 7) return;

  // loop over all TCA's
  for (uint8_t i = 0; i < TCA_COUNT; i++) {
    Wire.beginTransmission(TCA_ADDRESSES[i]);
    if (i == tca) {
      // set output channel for selected TCA
      Wire.write(1 << channel);
    } else {
      // for others, turn off all channels
      Wire.write(0);
    }
    Wire.endTransmission();
  }
}
void setup() {
Serial.begin(9600);
 Wire.begin();
 


  Serial.begin(9600);
  while (!Serial);
  Serial.println("Multiple TCA9548A Example.");

  Serial.print("Total number of TCA's = ");
  Serial.println(TCA_COUNT);
  for (uint8_t i = 0; i < TCA_COUNT; i++) {
    Serial.print(i);
    Serial.print(" : 0x");
    Serial.println(TCA_ADDRESSES[i], 16);
     }
    
   

    // Init OLED display on bus number 1 (display 1)
    tcaselect(0, 0);
    if (!display1.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
      Serial.println(F("SSD1306 allocation failed"));
      for (;;)
        ;
    }
    // Clear the buffer
    display1.clearDisplay();

    // Init OLED display on bus number 2
    tcaselect(0, 1);
    if (!display2.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
      Serial.println(F("SSD1306 allocation failed"));
      for (;;)
        ;
    }
    // Clear the buffer
    display2.clearDisplay();

    // Init OLED display on bus number 3
    tcaselect(0, 2);
    if (!display3.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
      Serial.println(F("SSD1306 allocation failed"));
      for (;;)
        ;
    }
    // Clear the buffer
    display3.clearDisplay();

    // Init OLED display on bus number 4
    tcaselect(0, 3);
    if (!display4.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
      Serial.println(F("SSD1306 allocation failed"));
      for (;;)
        ;
    }
    // Clear the buffer
    display4.clearDisplay();

    // Init OLED display on bus number 4
    tcaselect(0, 4);
    if (!display5.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
      Serial.println(F("SSD1306 allocation failed"));
      for (;;)
        ;
    }
    // Clear the buffer
    display5.clearDisplay();


// Init OLED display on bus number 4
    tcaselect(0, 5);
    if (!display6.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
      Serial.println(F("SSD1306 allocation failed"));
      for (;;)
        ;
    }
    // Clear the buffer
    display6.clearDisplay();

    

    tcaselect(0, 0);
    display1.setTextSize(8);
    display1.setTextColor(WHITE);
    display1.setCursor(28, 8);
    // Display static text
    display1.println("vj");
    Serial.print(val1);
    display1.display();
    display1.println("\n");

    // Write to OLED on bus number 3
    tcaselect(0, 1);
    display2.setTextSize(8);
    display2.setTextColor(WHITE);
    display2.setCursor(28, 8);
    // Display static text
    display2.println("Vj");
    Serial.print(val2);
    display2.display();
    display2.println("\n");

    // Write to OLED on bus number 4
    tcaselect(0, 2);
    display3.setTextSize(8);
    display3.setTextColor(WHITE);
    display3.setCursor(28, 8);
    // Display static text
    display3.println("vj");
    Serial.print(val3);
    display3.display();
    display3.println("\n");


    // Write to OLED on bus number 5
    tcaselect(0, 3);
    display4.setTextSize(8);
    display4.setTextColor(WHITE);
    display4.setCursor(28, 8);
    // Display static text
    display4.println("vj");
    Serial.print(val4);
    display4.display();
    display4.println("\n");

    // Write to OLED on bus number 5
    tcaselect(0, 4);
    display5.setTextSize(8);
    display5.setTextColor(WHITE);
    display5.setCursor(28, 8);
    // Display static text
    display5.println("vj");
    Serial.print(val5);
    display5.display();
    display5.println("\n");

    // Write to OLED on bus number 5
    tcaselect(0, 5);
    display6.setTextSize(8);
    display6.setTextColor(WHITE);
    display6.setCursor(28, 8);
    // Display static text
    display6.println("vj");
    Serial.print(val6);
    display6.display();
    display6.println("\n");


    
}

void loop() 
{
while (1) 
 {
String s;
Serial.println("vijay");
char keypressed = myKeypad.getKey();
Serial.println(keypressed);
if (keypressed != NO_KEY)
  {

if (keypressed == '0')
          {
    Serial.println(keypressed);
               val1 = 1;
               val2 = 2;
               val3 = 3;
               val4 = 23;
               val5 = 4;
               val6 = 5;
               
           }
    else if (keypressed == '1') 
           {
        Serial.println(keypressed);     
        val1 = 6;
        val2 = 7;
        val3 = 8;
        val4 = 45;
        val5 = 9;
        val6 = 10;
        
           }
    else if (keypressed == '2') 
           {
        Serial.println(keypressed);     
        val1 = 10;
        val2 = 12;
        val3 = 13;
        val4 = 23;
        val5 = 14;
        val6 = 15; 
              
           }
          
          
  }
break; 

}

display1.setTextSize(1);
display1.setTextColor(WHITE);
display1.setCursor(10, 0);
display1.clearDisplay();
display1.println("");
printText();
display1.display();

display2.setTextSize(1);
display2.setTextColor(WHITE);
display2.setCursor(10, 0);
display2.clearDisplay();
display2.println("");
printText();
display2.display();

display3.setTextSize(1);
display3.setTextColor(WHITE);
display3.setCursor(10, 0);
dis`Use code tags to format code for the forum`play3.clearDisplay();
display3.println("");
printText();
display3.display();

display4.setTextSize(1);
display4.setTextColor(WHITE);
display4.setCursor(10, 0);
display4.clearDisplay();
display4.println("");
printText();
display4.display();

display5.setTextSize(1);
display5.setTextColor(WHITE);
display5.setCursor(10, 0);
display5.clearDisplay();
display5.println("");
printText();
display5.display();

display6.setTextSize(1);
display6.setTextColor(WHITE);
display6.setCursor(10, 0);
display6.clearDisplay();
display6.println("");
printText();
display6.display();



}




void printText(void)
{
    tcaselect(0, 0);
    display1.setTextSize(8);
    display1.setTextColor(WHITE);
    display1.setCursor(28, 8);
    // Display static text
    display1.println(val1);
        Serial.print(val1);
    display1.display();
    display1.println("\n");
    

    // Write to OLED on bus number 3
    tcaselect(0, 1);
    display2.setTextSize(8);
    display2.setTextColor(WHITE);
    display2.setCursor(28, 8);
    // Display static text
    display2.println(val2);
    Serial.print(val2);
    display2.display();
    display2.println("\n");

    // Write to OLED on bus number 2
    tcaselect(0, 2);
    display3.setTextSize(8);
    display3.setTextColor(WHITE);
    display3.setCursor(28, 8);
    // Display static text
    display3.println(val3);
    Serial.print(val3);
    display3.display();
    display3.println("\n");

    // Write to OLED on bus number 2
    tcaselect(0, 3);
    display4.setTextSize(8);
    display4.setTextColor(WHITE);
    display4.setCursor(28, 8);
    // Display static text
    display4.println(val4);
    Serial.print(val4);
    display4.display();
    display4.println("\n");


    // Write to OLED on bus number 2
    tcaselect(0, 4);
    display5.setTextSize(8);
    display5.setTextColor(WHITE);
    display5.setCursor(28, 8);
    // Display static text
    display5.println(val5);
    Serial.print(val5);
    display5.display();
    display5.println("\n");
    
    tcaselect(0, 5);
    display6.setTextSize(8);
    display6.setTextColor(WHITE);
    display6.setCursor(28, 8);
    // Display static text
    display6.println(val6);
    Serial.print(val6);
    display6.display();
    display6.println("\n");
    

     }




`

Please read first
https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/1111649

In loop(), each time you call the printText() function, afterwards you have a call to display.display(), which sends each of the display buffers to the 6th display (because that display is the one selected through the I2C multiplexer at the end of the printText() function).

1 Like

Thanks a lot..... after Correct it remove all

display5.display();
`

PrintText()

`

function it ok....

now Loop function corrected .but when i connect the 7th Display it showing Allocation Failed Alarm.Could you solve this with your Valuable Reply......

now Loop function corrected .but when i connect the 7th Display it showing Allocation Failed Alarm.Could you solve this with your Valuable Reply......

What type of arduino are you using? Each display needs 1024 bytes of ram for the display buffer. The Adafruit library will give you an "allocation failed" warning when there is not enough memory for the buffer.

It is possible to use a single buffer with multiple displays, but that does have some limitations.
How many displays are you intending to use?

Thanks for your Reply.......
Now I am Currently Using Arudino Mega 2560 Board..
And also I want Connect more than 20 Display.

if any other method to use all this Display ???..

Create an IIC bus.

1 Like

You can share a single buffer between all the displays. This has the advantage of needing much less ram, but the disadvantage that you have to clear the buffer between displays.

< edit > Sketch has been edited, I neglected to call Wire.begin() at the beginning of setup()

Something like this:

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


int val[6] = {0, 1, 2, 3, 4, 5};

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

const uint8_t displayCount = 6;

// for each TCA9548A, add an entry with its address
const uint8_t TCA_ADDRESSES[] = {
  0x70,
  0x71,
  0x73
};

const uint8_t TCA_COUNT = sizeof(TCA_ADDRESSES) / sizeof(TCA_ADDRESSES[0]);

void tcaselect(uint8_t tca, uint8_t channel) {
  if (tca >= TCA_COUNT) return;
  if (channel > 7) return;

  // loop over all TCA's
  for (uint8_t i = 0; i < TCA_COUNT; i++) {
    Wire.beginTransmission(TCA_ADDRESSES[i]);
    if (i == tca) {
      // set output channel for selected TCA
      Wire.write(1 << channel);
    } else {
      // for others, turn off all channels
      Wire.write(0);
    }
    Wire.endTransmission();
  }
}


void setup() {
  Wire.begin(); // <<<<<< edited sketch to add this line
  Serial.begin(9600);
  while (!Serial);

  Serial.println(F("Multiple TCA9548A Example."));

  Serial.print(F("Total number of TCA's = "));
  Serial.println(TCA_COUNT);
  for (uint8_t i = 0; i < TCA_COUNT; i++) {
    Serial.print(i);
    Serial.print(" : 0x");
    Serial.println(TCA_ADDRESSES[i], 16);
  }


  // Init OLED displays
  for (uint8_t i = 0; i < displayCount; i++) {
    tcaselect(0, i);
    if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
      Serial.println(F("SSD1306 allocation failed"));
      for (;;) ;
    }
  }

  for (uint8_t i = 0; i < displayCount; i++) {
    tcaselect(0, i);
    display.clearDisplay();
    display.setTextSize(8);
    display.setTextColor(WHITE);
    display.setCursor(28, 8);
    // Display static text
    //display.println(F("vj"));
    display.print(val[i]);
    Serial.println(val[i]);
    display.display();
  }
}

void loop() {
}
1 Like

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