3 Sensors to IPS LCD Screen

Hey There, first thing I'd like to say is thank you to everyone who is willing to read/Help me out with this.

Anyways, I've been trying to make a "Smart garden" Cause i personally thought it would be really interesting doing this as my first project with arduino(Nano).
All the wiring is properly connected, and all sensors work as intended to do so. I've tested each sensors individually and thankfully , they all give accurate data results.

Now to get to the issue(s), i've been attempting to display all 3 sensor Data to my 2" IPS LCD Screen, but for the life of me, i cant figure out how to do it. I will post the code that im currently using to display(Test) Each sensor individually. i have fiddle farted with the code multiple times, and tried to come up with something that will make all 3 sensors show the data on the LCD Screen (Instead of One at a time), but i have had no luck. So now i am coming here to ask for guidance! :slight_smile:

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

// pin definition for the Uno
#define cs   10
#define dc   9
#define rst  -1
#define LR A0 //Photo-Resistor Sensor
#define TE A1 //Temperature Sensor
#define MO A2 //Moisture Sensor

// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);

// char array to print to the screen
char sensorPrintout[4];

void setup() {

  // Put this line at the beginning of every sketch that uses the GLCD:
  TFTscreen.begin();

  // clear the screen with a black background
  TFTscreen.background(0, 0, 0);

  // write the static text to the screen
  // set the font color to white
  TFTscreen.stroke(255, 255, 255);
  // set the font size
  TFTscreen.setTextSize(2);
  // write the text to the top left corner of the screen
  TFTscreen.text("Sensor Value :\n ", 0, 0);
  // ste the font size very large for the loop
  TFTscreen.setTextSize(5);
}

void loop() {

  // Read the value of the sensor on A0
  String sensorVal = String(analogRead(A0));

  // convert the reading to a char array
  sensorVal.toCharArray(sensorPrintout, 4);

  // set the font color
  TFTscreen.stroke(255, 255, 255);
  // print the sensor value
  TFTscreen.text(sensorPrintout, 0, 20);
  // wait for a moment
  delay(250);
  // erase the text you just wrote
  TFTscreen.stroke(0, 0, 0);
  TFTscreen.text(sensorPrintout, 0, 20);
}

Hello ascendingc
Keep it simple and stupid.
Run some tutorials to the hardware selected.
If you are happy with the results of the tutorials you can merge these to the project by using control structures.
Have a nice day and enjoy coding in C++.

1 Like

i'm not familiar with the tft functions. but in general, things like stroke only need to be done once in setup, which you do, but do again in loop()

don't understant why setTextSize is called twice.

if "Sensor Value" appears correctly, then i think you have it working

like other devices (i'm familiar with OLED displays), text is displayed at an x,y position (never sure of y=0 is top or bottom). but once you figure that out, i'd suggest creating a function for displaying the # of lines possible on the display and simply passing char strings to that function. what i use which also displays text on the serial monitor for debugging

void dispOled(
    const char  *s0,
    const char  *s1,
    const char  *s2,
    const char  *s3,
    bool         clr )
{
    char  s [40];

    if (clr)
        display.clear();

    display.setTextAlignment(TEXT_ALIGN_LEFT);

    if (s0)  {
        display.drawString(0, DISP_Y0,  s0);
        if (debug && NUL != *s0)  {
            sprintf (s, "... %s", s0);
            if (ST_ECHO & state)
                Serial.println (s);
        }
    }
    if (s1)  {
        display.drawString(0, DISP_Y1, s1);
        if (debug && NUL != *s1)  {
            sprintf (s, "    %s", s1);
            if (ST_ECHO & state)
                Serial.println (s);
        }
    }
    if (s2)  {
        display.drawString(0, DISP_Y2, s2);
        if (debug && NUL != *s2)  {
            sprintf (s, "    %s", s2);
            if (ST_ECHO & state)
                Serial.println (s);
        }
    }
    if (s3)  {
        display.drawString(0, DISP_Y3, s3);
        if (debug && NUL != *s3)  {
            sprintf (s, "    %s", s3);
            if (ST_ECHO & state)
                Serial.println (s);
        }
    }
    display.display();
}

and here's an example of how i use it, using sprintf() to format multiple pieces of information on each line

        sprintf (s, "%2d:%02d   %d", timeSec / 60, timeSec % 60, dccAdr);
        sprintf (s0, " %3d Thr  %s%02d Rev",
            throttle, DIR_REV == dir ? "-" : " ", cutoff);

        sprintf (s1, "  %5s %5s", airBrkStr [brakeAir], indBrkStr [brakeInd]);
        sprintf (s2, "  %3d MPH  %3d DCC-Spd", int(mph), dccSpd);

        dispOled (   s, s0, s1, s2, CLR);
1 Like

Hello can anyone give basic instructions in these topic

TFT - Arduino Reference, Examples

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.