I'm making a bee hive monitor, at the hive I have a nano with a BMP280, soon to be a BME280. the data is being sent to a Mega via NRF24L01+ modules. That part works. I am now trying to add an Adafruit 3.5 TFT display to the mega. That works as well.
What doesn't work is when I try to read the data while having the display and the NRF2401 both connected to the SPI pins. From what I've read I need to select and de-select the slave select pins of the receiving NRF24L01 and the display. I've played around with different pins and methods but obviously it's not working. I don't know enough about getting into the libraries to see what variables I should be using.
My sketch is in 3 tabs, the main tab, display control, and read the hive. Here are all 3.
(I couldn't post all the logic as it exceeded the max length so I remove the logic to draw the screens, you shouldn't need that for my questions, I don't think any ways)
the main
// Bee Hive base Controller with TFT display
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include "Adafruit_GFX.h"
#include "Adafruit_HX8357.h"
#include "TouchScreen.h"
#include <Wire.h>
#include "RTClib.h"
// for NRF24L01
#define radio_CS 4
#define radio_CSN 5
RF24 radio(radio_CS, radio_CSN); // 7, 8 CE, CSN
const byte addresses[][6] = {"1Node", "2Node"};
// for TFT display
// These are the four touchscreen analog pins
#define YP A2 // must be an analog pin, use "An" notation!
#define XM A3 // must be an analog pin, use "An" notation!
#define YM 29 // can be a digital pin
#define XP 28 // can be a digital pin
// This is calibration data for the raw touch data to the screen coordinates
#define TS_MINX 110
#define TS_MINY 80
#define TS_MAXX 900
#define TS_MAXY 940
#define MINPRESSURE 10
#define MAXPRESSURE 1000
// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// These are 'flexible' lines that can be changed
#define TFT_CS 10
#define TFT_DC 9
int TFTCS_Status;
// #define SD_CS 44
#define TFT_RST 8 // dont use a reset pin, tie to arduino RST if you like
//#define TFT_RST 8 // RST can be set to -1 if you tie it to Arduino's reset
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, TFT_RST);
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
// RTC_DS1307 RTC; // define the Real Time Clock object
RTC_PCF8523 RTC; // define the Real Time Clock object
//==== Defining Variables
char text[6] = "";
String inTemp, inHum, outTemp = "", outHum;
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];
int x, y;
int currentPage = 0; //, selectedUnit;
int selectedUnit = 0;
char hive1Temp[20] = " "; // Hive 1 Temperature number
char hive1Hum[20] = " "; // Hive 1 Humidity number
long int start_time = millis();
long int read_time = 5000; // time between reading hive data
int hiveRead = 0; // bit that system is reading hive status
//String rtcTime, rtcDate;
//int draw_state = 0;
//unsigned long previousMillis = 0;
//long interval = 3000;
void setup() {
Serial.begin(9600);
Serial.println(F("Hive Base"));
pinMode(TFT_CS, OUTPUT);
pinMode(TFT_DC, OUTPUT);
pinMode(radio_CS, OUTPUT);
pinMode(radio_CSN, OUTPUT);
// NRF24L01 setup
radio.begin(); // Initiate the radio object
// radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN); // Set the transmit power to lowest available to prevent power supply related issues
radio.setDataRate(RF24_2MBPS); // Set the speed of the transmission to the quickest available
radio.setChannel(124); // Use a channel unlikely to be used by Wifi, Microwave ovens etc
// Open a writing and reading pipe on each radio, with opposite addresses
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1, addresses[1]);
// Stop the radio listening for data
radio.stopListening();
// TFT setup
tft.begin();
tft.fillScreen(BLACK); //clears screen, sets to Black
tft.setRotation(1); // rotates screen 90' for landscape mode
currentPage = 0; // Indicates that we are at Home Screen
selectedUnit = 0; // Indicates the selected unit for the first example, cms or inches
drawHomeScreen(); // Draws the Home Screen
digitalWrite(TFT_CS, HIGH); // enable display
digitalWrite(TFT_DC, LOW); // Select display
digitalWrite(radio_CS, LOW); // Disable NRF24L01
digitalWrite(radio_CSN, HIGH); // de-select NRF24L01
// connect to RTC
/* Wire.begin();
if (!RTC.begin()) {
logfile.println("RTC failed");
}
*/
} // end void setup
void loop() {
// read hive every 5 seconds disble display while reading hive
if(millis() > (start_time + read_time)){
start_time = millis();
hiveRead = 1;
readHive(); // get hive status
} // end if(millis() > (start_time + read_time))
else if(hiveRead == 0) {
display_cntrl(); // update display
} // end else
} // end void loop
display control
// TFT display control
void display_cntrl()
{
// Retrieve a point on touch screen
TSPoint p = ts.getPoint();
/*
// for touch screen testing gives, x, y and pressure values
// we have some minimum pressure we consider 'valid'
// pressure of 0 means no pressing!
if (p.z < MINPRESSURE || p.z > MAXPRESSURE) {
return;
}
Serial.print("X = "); Serial.print(p.x);
Serial.print("\tY = "); Serial.print(p.y);
Serial.print("\tPressure = "); Serial.println(p.z);
// Scale from ~0->1000 to tft.width using the calibration #'s
p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
*/
// Home Screen
if (currentPage == 0) {
// If we press the Report Setup Button
if ((p.x>=440) && (p.x<=540) && (p.y>=350) && (p.y<=670)) {
// drawFrame(35, 90, 285, 130); // Custom Function -Highlighs the buttons when it's pressed
currentPage = 1; // Indicates that we are the first example
tft.fillScreen(BLACK); //clears screen, sets to Black
drawHiveStatus(); // It is called only once, because in the next iteration of the loop, this above if statement will be false so this funtion won't be called. This function will draw the graphics of the first example.
} // end if for report setup
// If we press the Test Control Button
if ((p.x>=240) && (p.x<=320) && (p.y>=320) && (p.y<=700)) {
// drawFrame(35, 140, 285, 180);
currentPage = 2;
tft.fillScreen(BLACK); //clears screen, sets to Black
// drawTestControl();
} // end if for Test Control
} // end if currentPage ==0
// Logic for Hive page
if (currentPage == 1) {
// If we press the Back to Main Menu Button
if ((p.x>=810) && (p.x<=860) && (p.y>=140) && (p.y<=210)) {
// drawFrame(35, 140, 285, 180);
currentPage = 0;
tft.fillScreen(BLACK); //clears screen, sets to Black
drawHomeScreen();
} // end if for back to home
} // end if curentPage ==1
} // end void display_cntrl
Hive read
void readHive()
{
digitalWrite(TFT_CS, LOW); // disable display
digitalWrite(TFT_DC, HIGH); // de-select display
digitalWrite(radio_CS, HIGH); // enable NRF24L01
digitalWrite(radio_CSN, LOW); // select NRF24L01
// listen for a response
radio.startListening();
if (radio.available()) {
Serial.println("radio.available");
radio.read(&text, sizeof(text)); // Read incoming data
outTemp = String(text[0]) + String(text[1]) + " 'F"; // Outdoor Temperature
outHum = String(text[2]) + String(text[3]) + " inHg"; // Outdoor Humidity
} // end if (radio.available()
float hive1_temp = (outTemp.toFloat()); // convert string to float
float hive1_hum = (outHum.toFloat()); // convert string to float
Serial.print("Hive 1 temp ");
Serial.print(outTemp);
Serial.print("\t");
Serial.print("Hive 1 Pressure ");
Serial.println(outHum);
// Stop listening for a response
radio.stopListening();
if(millis() > (start_time + 1000)){
} // if(millis() > (start)time + 1000)
digitalWrite(TFT_CS, HIGH); // enable display
digitalWrite(TFT_DC, LOW); // Select display
digitalWrite(radio_CS, LOW); // Disable NRF24L01
digitalWrite(radio_CSN, HIGH); // de-select NRF24L01
hiveRead = 0;
} // end void readHive
Any and all help will be greatly appreciated
Thanks
John