ILI9340 TFT + NRF24L01+ variable print issue...

Got a thermostat project on the go using a local and remote temp sensor (DHT11).

Basic layout of the project for background (in case anyone is interested :slight_smile: ):

Nano(328P) with DHT11 and NRF24L01+

Mega2560 with DHT11, NRF24L01+, 2.2" TFT ILI940 driver and relay(substituted with a LED for testing).

Issue that I’m having is when I’m writing the remote temperature to the display the local temp field is also being populated by the remote value, I’ve checked the serial output when setting the value’s and they are correct.

The code that I’m using to write the values to the display is:

    if (localTemp < setTemp) // if the temperature exceeds your chosen setting set font colour to RED and turn LED ON
    {
      digitalWrite (relayPin, 1); // turn on the led
      tft.setTextColor(ILI9340_RED);
    }
    else if (localTemp > setTemp) // if not then set the font colour to WHITE and turn LED OFF
    {
      digitalWrite (relayPin,0);
      tft.setTextColor(ILI9340_WHITE);
    }
    //display local temp.
    tft.setTextSize(4);
    tft.setCursor(46,23);
    tft.print(localTemp);
    tft.print("C");

    if (remoteTemp < setTemp) // if the temperature exceeds your chosen setting set font colour to RED
    {
      tft.setTextColor(ILI9340_RED);
    }
    else if (remoteTemp > setTemp) // if not then set the font colour to WHITE
    {
      tft.setTextColor(ILI9340_WHITE);
    }
    //display remote temp.
    tft.setTextSize(4);
    tft.setCursor(200,23);
    tft.print(remoteTemp);
    tft.print("C");

I’m not currently doing anything other than changing the font colour when the temperature has exceeded the setting temp.

I’ve checked the code and cant see anywhere I might have set the localTemp to remoteTemp. I’ve attached the full sketch (Please dont shout at me for the bad coding still have ALOT of learning to do :-[ )

Any help is much appreciated :slight_smile:

Thanks

Steve

thermostat.4.ino (5.25 KB)

  EEPROM.read (1); // make the eeprom or atmega328 memory address 1

Read the value at that address, but throw the returned value away. So, why bother reading?

Being a newbie is no excuse for piss-poor indenting. Put every { on a new line. Run Tools + Auto Format.

    radio.read(&remoteTemp, sizeof(uint16_t)+1);

The second argument is the number of bytes that the array (the first argument) can hold. There is NO way that you are going to fit 3 bytes in an int.

Serial.print() the local and remote temps, WITH IDENTIFIERS, each time you write to the tft. Are the values actually different?

PaulS:

  EEPROM.read (1); // make the eeprom or atmega328 memory address 1

Read the value at that address, but throw the returned value away. So, why bother reading?

Oppss my bad, not quite sure why that was there :slight_smile:

PaulS:
Being a newbie is no excuse for piss-poor indenting. Put every { on a new line. Run Tools + Auto Format.

I've always programmed with the opening { after the () and yup my indenting deff sucks lol am working on it lol

PaulS:

    radio.read(&remoteTemp, sizeof(uint16_t)+1);

The second argument is the number of bytes that the array (the first argument) can hold. There is NO way that you are going to fit 3 bytes in an int.

unit16_t will hold from 0 to 65535 so well outside the range the remote arduino will send, but I did take the from an example code and didn't really have a clue till I just googled what it mean :wink: lol

PaulS:
Serial.print() the local and remote temps, WITH IDENTIFIERS, each time you write to the tft. Are the values actually different?

Output from the serial when adding in a Serial.print at the time of writing to the LCD below, first 2 lines are from the initial setting of the variables, the second 2 lines are written at the time of the writing to the LCD.

local temp: 25
remote temp: 22
print local: 22
print remote: 22

I'm slowly loosing the will to live as to why this is changing the localTemp value to the remoteTemp value

Thanks

Steve

You should print the local and remote temperatures again after the seeming alteration on the LCD. Are the values in the variables actually changing?

PaulS:
You should print the local and remote temperatures again after the seeming alteration on the LCD. Are the values in the variables actually changing?

Yeah added them in after they get written to the display.

  if (localTemp < setTemp) // if the temperature exceeds your chosen setting set font colour to RED and turn LED ON
  {
    digitalWrite (relayPin, 1); // turn on the led
    tft.setTextColor(ILI9340_RED);
  }
  else if (localTemp > setTemp) // if not then set the font colour to WHITE and turn LED OFF
  {
    digitalWrite (relayPin, 0); //turn LED OFF.
    tft.setTextColor(ILI9340_WHITE);
  }
  //display local temp.
  tft.setTextSize(4);
  tft.setCursor(46, 23);
  tft.print(localTemp);
  Serial.print("print local: ");
  Serial.println(localTemp);
  tft.print("C");

  if (remoteTemp < setTemp) // // if the temperature exceeds your chosen setting set font colour to RED
  {
    //digitalWrite (relayPin, 1); // turn on the led
    tft.setTextColor(ILI9340_RED);
  }
  else if (remoteTemp > setTemp) // if not then set the font colour to WHITE and
  {
    //digitalWrite (relayPin,0);
    tft.setTextColor(ILI9340_BLUE);
  }
  //display remote temp.
  tft.setTextSize(4);
  tft.setCursor(200, 23);
  tft.print(remoteTemp);
  Serial.print("print remote: ");
  Serial.println(remoteTemp);
  tft.print("C");

The value of remoteTemp is the value being sent from the nano and if I cause temp to raise on that one it changes as expected.

Thanks

Steve

I would be printing localTemp and remoteTemp AT THE SAME TIME. That way, I’d know what was making them become the same, if indeed they are.

PaulS:
I would be printing localTemp and remoteTemp AT THE SAME TIME. That way, I'd know what was making them become the same, if indeed they are.

Not sure what you mean by printing them at the same time? :-[

They output the correct value at this stage where they are set:

unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= colData) {

    previousMillis = currentMillis;

    localTemp = dht.readTemperature();

    Serial.print("local temp: ");
    Serial.println(localTemp);

    radio.read(&remoteTemp, sizeof(uint16_t) + 1);

    Serial.print("remote temp: ");
    Serial.println(remoteTemp);


  }

I suppose what I could do is a serial.print at each stage to see where they change.

Thanks

Steve

Not sure what you mean by printing them at the same time?

I mean that these 4 lines are always together:

    Serial.print("local temp: ");
    Serial.println(localTemp);
    Serial.print("remote temp: ");
    Serial.println(remoteTemp);

PaulS:
I mean that these 4 lines are always together:

    Serial.print("local temp: ");

Serial.println(localTemp);
    Serial.print("remote temp: ");
    Serial.println(remoteTemp);

Ah ok, only time the values are both correct is within this section:

  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= colData) {

    previousMillis = currentMillis;

    getLocTemp = dht.readTemperature();

    int done = radio.read(&remoteTemp, sizeof(uint16_t) + 1);

  }

However I’ve been playing with the code and replaced localTemp through the loop with getLocTemp and in the setup added localTemp = dht.readTemperature(); but not actually use it anywhere and the getLocTemp is correct right through the sketch.

I did just try setting the initial value in setup but had the same outcome so now the full sketch after taking away all the serial.prints is as below which works as expected.

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ILI9340.h>
#include <SPI.h>
#include <buttons.h>
#include <DHT.h>
#include <EEPROM.h>
#include <nRF24L01.h>
#include <RF24.h>

#define _sclk 52
#define _miso 50
#define _mosi 51
#define _cs 48
#define _dc 49
#define _rst 47

RF24 radio(9, 10);

const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

int readTemp;

//define button pins
#define SWTU 3 // switch up is at pin 3
#define SWTD 4 // switch down is at pin 4

//define DHT temp sensor pin and type (DHT11, DHT21 or DHT22).
#define DHTPIN 2
#define DHTTYPE DHT11   // DHT 11

#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif

int relayPin = 5; //define relay pin - initially LED for testing.

int setTemp = 20;
int setTempOld = 0;

//set millis and data collection interval variables.
unsigned long previousMillis = 0;
int colData = 1000;

//declaration
void dht11_wrapper(); // must be declared before the lib initialization

//Initialize DHT
DHT dht(DHTPIN, DHTTYPE);

Adafruit_ILI9340 tft = Adafruit_ILI9340(_cs, _dc, _rst);  // Invoke custom library

int prevTempL;
int prevTempR;

//declare buttons
Button buttonUp;
Button buttonDown;

int getLocTemp = 0;
int localTemp = 0;
int remoteTemp = 0;

void setup() {
  tft.begin();
  dht.begin();
  radio.begin();

  radio.setRetries(15, 15);
  radio.openWritingPipe(pipes[1]);
  radio.openReadingPipe(1, pipes[0]);
  radio.enableDynamicPayloads() ;
  radio.setAutoAck( true );
  radio.startListening();

  tft.setRotation(3);	// 0 - Portrait, 1 - Lanscape
  tft.fillScreen(ILI9340_BLACK);
  tft.setTextWrap(true);

  tft.drawRect (5, 0, 150, 55, ILI9340_WHITE);
  tft.drawRect (165, 0, 150, 55, ILI9340_WHITE);
  tft.drawRect (165, 55, 150, 55, ILI9340_WHITE);
  tft.drawRect (5, 55, 150, 55 , ILI9340_WHITE);

  //temperature text
  tft.setCursor(10, 4);
  tft.setTextSize(2);
  tft.setTextWrap(true);
  tft.setTextColor(ILI9340_WHITE);
  tft.print("Local Temp: ");
  tft.setCursor(170, 4);
  tft.print("Remote Temp: ");
  //setpoint text
  tft.setCursor(10, 59);
  tft.setTextSize(2);
  tft.setTextWrap(true);
  tft.setTextColor(ILI9340_WHITE);
  tft.print("Set Point: ");

  Serial.begin(9600);

  pinMode(relayPin, OUTPUT);  // make led or pin3 an output
  pinMode(SWTU, INPUT_PULLUP);
  pinMode(SWTD, INPUT_PULLUP);
  buttonUp.assign(SWTU);
  buttonDown.assign(SWTD);
  buttonUp.setMode(OneShotTimer);
  buttonUp.setTimer(1500);
  buttonUp.setRefresh(500);
  buttonDown.setMode(OneShotTimer);
  buttonDown.setTimer(1500);
  buttonDown.setRefresh(700);
  
  Serial.println("pause 5");
  delay(1000);
  Serial.println("pause 4");
  delay(1000);
  Serial.println("pause 3");
  delay(1000);
  Serial.println("pause 2");
  delay(1000);
  Serial.println("pause 1");
  delay(1000);
  
  localTemp = dht.readTemperature();
  
  EEPROM.write (1, setTemp);
  
}

void loop() {

  switch (buttonUp.check()) {
    case ON:
      setTemp ++ ;  // add one to the settemp, the settemp is the ideal temperature for you
      EEPROM.write (1, setTemp);
      break;
    case Hold:
      setTemp ++ ;  // add one to the settemp, the settemp is the ideal temperature for you
      EEPROM.write (1, setTemp);
      break;
    default:
      break;
  }

  switch (buttonDown.check()) {
    case ON:
      setTemp -- ;  // add one to the settemp, the settemp is the ideal temperature for you
      EEPROM.write (1, setTemp);
      break;
    case Hold:
      setTemp -- ;  // add one to the settemp, the settemp is the ideal temperature for you
      EEPROM.write (1, setTemp);
      break;
    default:
      break;
  }


  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= colData) {

    previousMillis = currentMillis;

    getLocTemp = dht.readTemperature();

    int done = radio.read(&remoteTemp, sizeof(uint16_t) + 1);

  }

  if (getLocTemp != prevTempL) {
    tft.fillRect (46, 23, 45, 28, ILI9340_BLACK);
    prevTempL = getLocTemp;
  }

  if (remoteTemp != prevTempR) {
    tft.fillRect (200, 23, 45, 28, ILI9340_BLACK);
    prevTempR = remoteTemp;
  }

  setTemp = EEPROM.read(1);

  if (getLocTemp < setTemp) // if the temperature exceeds your chosen setting set font colour to RED and turn LED ON
  {
    digitalWrite (relayPin, 1); // turn on the led
    tft.setTextColor(ILI9340_RED);
  }
  else if (getLocTemp >= setTemp) // if not then set the font colour to WHITE and turn LED OFF
  {
    digitalWrite (relayPin, 0); //turn LED OFF.
    tft.setTextColor(ILI9340_WHITE);
  }
  //display local temp.
  tft.setTextSize(4);
  tft.setCursor(46, 23);
  tft.print(getLocTemp);
  tft.print("C");

  if (remoteTemp < setTemp) // // if the temperature exceeds your chosen setting set font colour to RED
  {
    //digitalWrite (relayPin, 1); // turn on the led
    tft.setTextColor(ILI9340_RED);
  }
  else if (remoteTemp >= setTemp) // if not then set the font colour to WHITE and
  {
    //digitalWrite (relayPin,0);
    tft.setTextColor(ILI9340_BLUE);
  }
  //display remote temp.
  tft.setTextSize(4);
  tft.setCursor(200, 23);
  tft.print(remoteTemp);
  tft.print("C");

  //display set trigger temp.
  if (setTemp != setTempOld) {
    tft.fillRect (46, 77, 45, 28 , ILI9340_BLACK);
    setTempOld = setTemp;
  }

  tft.setCursor(46, 77);
  tft.setTextColor(ILI9340_GREEN);
  tft.print(setTemp);
  tft.print("C");

}

Thanks

Steve

  if (getLocTemp < setTemp) // if the temperature exceeds your chosen setting set font colour to RED and turn LED ON
  {
  }
  else if (getLocTemp >= setTemp) // if not then set the font colour to WHITE and turn LED OFF

Are you planning for the other possibilities? What would they be? 8)