2 spi displays with arduino

Dear friends,

I wonder if is it possible to use 2 or more tft spi displays on at the same time displaying different pieces of information.

following the topic of another user

http://forum.arduino.cc/index.php?topic=282967.0

As long as you have enough free pins for the individual SS lines, yes.

free pins doesn't matter.

i use the TFT library and wrote a code

but when i comment in setup first screen initialization second dispay works and vice versa.

together they do not work

#include <SPI.h>
#include <TFT.h>      

#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(6000);
  tft_draw1(screen2);
  tft_draw2(screen1);
  delay(6000);
}

humapoc:
TFT screen1 = TFT(cs1, dc, rst);
TFT screen2 = TFT(cs2, dc, rst);

Here is your problem - but you want to double check this with the C++ gurus. If I'm not mistaken you have only one TFT instance, which you set up twice, so the second one is the configuration you end up with. You need two individual TFT instances, one for each screen.

I think you have to add a "new" keyword or so, not sure how to program it exactly.

wvmarle:
Here is your problem - but you want to double check this with the C++ gurus. If I'm not mistaken you have only one TFT instance, which you set up twice, so the second one is the configuration you end up with. You need two individual TFT instances, one for each screen.

No, these are copy initializations: "TFT(cs1, dc, rst)" creates a temporary TFT object, and then it gets copied to "screen1". I don't see a reason why they should be the same.

wvmarle:
I think you have to add a "new" keyword or so, not sure how to program it exactly.

"new" allocates memory on the heap (dynamic storage), at run time, and returns a pointer to the new object. There's no reason why you'd need that here.

To test this:

class A {
  public:
    A(int x) : x(x) {};
    void print() {
      Serial.println(x);
    }
  private:
    int x = 0;
};

void setup() {
  Serial.begin(115200);
  while (!Serial);

  A a1 = A(1); // copy initialization
  A a2 = A(2);

  A a3(3); // direct initialization
  A a4(4);

  A *a5 = new A(5); // dynamic allocation + initialization
  A *a6 = new A(6); // note that a5 and a6 are pointers to an object

  a1.print();  // 1
  a2.print();  // 2
  a3.print();  // 3
  a4.print();  // 4
  a5->print(); // 5
  a6->print(); // 6

  delete a5; // object is not deleted when pointer a5 or a6 goes out of scope, 
  delete a6; // so delete manually
} // a1, a2, a3 and a4 go out of scope, so they are deleted

void loop() { }

Pieter

OK, thanks for the correction. Those things are always tricky to understand (and differ per language).

That leaves me at least with no clue on why only one screen can work at the time.

OP: if you say "together they don't work" you mean neither of them works? Or only one of them works? Because that's how I interpreted it. Your description however is ambiguous.

Is there enough power available to run both screens at the same time?

power is enough. according to code only one of them works. When i comment screen1 in setup () the other works.

Do you have an oscilloscope to see what's going on?
If not, add an LED + resistor to the CS lines, and see what they do. (They should go LOW during transmission.) If it's too fast too see, change the SPI prescaler for a lower SPI clock speed.

Pieter

Why have you started another thread for this? If you didn't understand the help in the other thread, then you should have asked for help there again. Opening new threads just wastes other people's time.

previous thread here

Thanks for the duplicate notification; I for one am not reading that other forum.
But just read that thread and indeed should have picked up on the comments in these two lines of code:

humapoc:

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

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

Is that so?
Can they really be shared?
There may be problems with sharing pins on both hardware and software levels.

OP: did you even try the suggestion in your other thread of NOT sharing these two pins?