I have an NRF24L01 and an SPI ILI9341 TFT connected to my arduino nano. They work well separately, but once they are together, the TFT just can’t be refreshed, after I do anything with the NRF module.
I have tried setting LOW/HIGH the Chip select pins of the two devices in any possible combinations, but the NRF module ALWAYS keeps working (I can see on the serial monitor I receive data), but the TFT only does the thing that I have written in the setup(). For example in I put my “displayStaticElements()” after the radio.begin(), the TFT would do nothing.
So I am sure the problem is switching between the two SPI devices. But how?
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "Fonts/FreeSans18pt7b.h"
#include "Fonts/FreeSans12pt7b.h"
#define NRF_CSN 8
#define NRF_CE 7
RF24 radio(NRF_CE, NRF_CSN); // CE, CSN
const byte address[6] = "00001";
#define TFT_DC 9
#define TFT_CS 3
#define TFT_RST 6
#define TFT_MISO 12
#define TFT_MOSI 11
#define TFT_CLK 13
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
struct packageOfWeatherData{
float temperature;
float humidity;
float heatindex;
};
typedef struct packageOfWeatherData PackageOfWeatherData;
PackageOfWeatherData weatherDataPackage;
float inTemp;
float inHum;
float outTemp;
float outHum;
float prevOutTemp;
float prevOutHum;
void setup() {
pinMode(TFT_CS, OUTPUT);
pinMode(NRF_CSN, OUTPUT);
tft.begin();
tft.setRotation(0);
displayStaticElements(); //displaying square frame, line, celsius/humidity symbols, only need once
radio.begin();
radio.openReadingPipe(1, address);
radio.setPALevel(RF24_PA_MAX);
radio.setChannel(100);
radio.setDataRate(RF24_250KBPS);
radio.startListening();
// SPI.begin();
Serial.begin(9600);
}
void loop() {
if (radio.available()) {
radio.read(&weatherDataPackage, sizeof(weatherDataPackage));
Serial.println("new data");
if(weatherDataPackage.temperature != outTemp || weatherDataPackage.humidity != outHum){
prevOutTemp = outTemp;
prevOutHum = outHum;
outTemp = weatherDataPackage.temperature;
outHum = weatherDataPackage.humidity;
Serial.println(outTemp);
digitalWrite(NRF_CSN, HIGH);
}
}
digitalWrite(TFT_CS, LOW); // DOES NOT WORK
tft.setTextSize(2);
tft.setCursor(75,88); tft.print(outTemp,1); //display outside temp
delay(1000);
}
void displayStaticElements(){
tft.fillScreen(ILI9341_BLACK); // fill entire screen with black
tft.drawRoundRect(0,0,240,320,13,ILI9341_WHITE); // draw a rounder rectangular frame
tft.drawFastHLine(15,159,210,0xC618); // two horizontal light grey lines in the half of the display
tft.drawFastHLine(15,160,210,0xC618);
tft.setFont(&FreeSans18pt7b); tft.setTextColor(ILI9341_WHITE); tft.setTextSize(0);
tft.setCursor(15,40); tft.print("Kint:");
tft.setCursor(15,200); tft.print("Bent:");
tft.setFont(&FreeSans12pt7b); tft.setTextSize(2);
tft.setCursor(186,88); tft.print("C");
tft.setCursor(180,141); tft.print("%");
tft.setCursor(186,240); tft.print("C");
tft.setCursor(180,300); tft.print("%");
tft.setFont(&FreeSans12pt7b); tft.setTextSize(0);
tft.setCursor(173,64); tft.print("o");
tft.setCursor(173,216); tft.print("o");
}