2 spi tft displays with arduino at the same time

Dear friends,

I use 2 spi tft displays with arduino and i would like to display different images on each at the same time.

I know that to multiple spi communication i need to add CS PINS but how to correct the code for the second display to display something different?

Thanks

#include <SPI.h>
#include <TFT.h>            // Arduino TFT library

#define cs  10 
#define dc 8 
#define rst 9 
TFT screen = TFT(cs, dc, rst);

byte mode_tft;
uint32_t time_tft;

void tft_draw1(){
  // 1 режим
  screen.background(255,255,255);
  screen.stroke(0,0,70);
  screen.fill(0,51,102);
  screen.circle(screen.width()/2.7, screen.height()/1.2, 26);

}

void tft_draw2(){
  // 2 режим
  screen.background(255,255,255);
  screen.stroke(0,0,70);
  screen.fill(0,51,102);
  screen.circle(screen.width()/2.7, screen.height()/1.9, 26);

}

void tft_draw3(){
  // 3 режим
  screen.background(255,255,255);
  screen.stroke(0,0,70);
  screen.fill(0,51,102);
  screen.circle(screen.width()/2.7, screen.height()/3.0, 26);

}

void tft_circle()
{
  switch (mode_tft) {
    case 0:
      mode_tft = 1;
      time_tft = millis() + 2000;
      tft_draw1();
      break;
    case 1:
      mode_tft = 2;
      time_tft = millis() + 2000;
      tft_draw2();
      break;
    case 2:
      mode_tft = 0;
      time_tft = millis() + 6000;
      tft_draw3();
      break;
    default: ;
  }  
}

 void setup(){

  screen.begin();
  mode_tft=0;
  time_tft=millis()+4000;

 }

void loop()
{
  if ((time_tft>0) & (time_tft<millis()) ) {
    tft_circle();  
    
  }
  
}

I haven't personally tried it, but I think it should be as simple as creating a second TFT instance and using it instead. You could then pass a reference to the chosen screen into your draw function. Something like this:

...
#define cs1  10
#define cs2  11 // whatever pin you have chosen
#define dc 8 // this pin can probably be shared...
#define rst 9 // ...as well as this
TFT screen1 = TFT(cs1, dc, rst);
TFT screen2 = TFT(cs2, dc, rst);

...

void tft_draw1(TFT& t){
  t.background(255,255,255);
...
}

void tft_draw2(TFT& t){
  t.background(255,255,255);
...
}

 void setup(){
  screen1.begin();
  screen2.begin();
...
 }

void loop()
{
  tft_draw1(screen1);
  tft_draw2(screen2);
  delay(10000);
  tft_draw1(screen2);
  tft_draw2(screen1);
  delay(10000);
}

Thanks a lot for your advice.

I tried the sample and one display updates the other is blank. Intresting...

Well, post your updated code and post a diagram of how you have wired your hardware up.

Hi

The problem with second display in setup().

screen1.begin();
screen2.begin();

second display works. when comment second line first display works. What would you do to make them work together separately? THANKS.

What would you do to make them work together separately?

If it was me, I'd start by posting a schematic. I'd follow that up by posting the real code I was using. YMMV.

#include <SPI.h>
#include <TFT.h>            // Arduino TFT library

#define cs1  10
#define cs2  6 // whatever pin you have chosen
#define dc 8 // this pin can probably be shared...
#define rst 9 // ...as well as this
TFT screen1 = TFT(cs1, dc, rst);
TFT screen2 = TFT(cs2, dc, rst);



void tft_draw1(TFT& t){
  t.background(255,255,255);

}

void tft_draw2(TFT& t){
  t.background(0,0,0);

}

 void setup(){
  screen1.begin();
  screen2.begin();

 }

void loop()
{
  tft_draw1(screen1);
  tft_draw2(screen2);
  delay(10000);
  tft_draw1(screen2);
  tft_draw2(screen1);
  delay(10000);
}

If that code only works for one TFT, then I'd question these assumptions:

#define dc 8 // this pin can probably be shared...
#define rst 9 // ...as well as this

thanks. i will try.

humapoc:
thanks. i will try.

Judging by your weird response and the fact that you have opened a new thread in another sub-forum with my unchanged code sample, I can only assume that you have not understood what PaulS has suggested.

PaulS:
If that code only works for one TFT, then I'd question these assumptions:

#define dc 8 // this pin can probably be shared...

#define rst 9 // ...as well as this

He means that I may be wrong to have assumed that those pins could have been shared. You need to allocated two more pins so that each screen has its own dc and rst, and then update the code so that it uses them.
Perhaps that will fix the problem.

#define dc1 8
#define rst1 9
#define dc2 ...
#define rst2 ...
TFT screen1 = TFT(cs1, dc1, rst1);
TFT screen2 = TFT(cs2, dc2, rst2);