Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 11 of 11

Thread: ili9341 sleep after 5 min and wake on touch?

  1. #1
    Junior Member
    Join Date
    Aug 2020
    Posts
    9

    ili9341 sleep after 5 min and wake on touch?

    Hi, this seems like something that would have been already answered, but I can't find it. Is there any code to put my ili9341 TFT screen (but not the touch sensor) to sleep after a certain amount of time and then wake by touching the screen? I'm using a Teensy 4.0 with it.

    Thanks!

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,922
    I am not sure what you mean by turn off... If you mean to completely shutoff all power to the display, you could probably use something like a transistor or two circuit with an IO pin that controls if the power is turned on or not... But this would probably also turn off the power the touch controller needs as well...

    Or do you simply want it to go dark and use less power...

    What I did with a Well Monitor setup I work on occasionally (usually only right after something fails with my water system) was to connect the backlight pin of the display to a PWM pin on the Teensy. Now depending on your display you are using, you may want to do this through a transistor or the like. Some displays, like the one from Adafruit has their display setup with a transistor on the display. Others like the one PJRC sells does not...

    I then have my code the PWM duty cycle depending on how long it has been since I last touched the display. After so much time I may set it to 0... In may case I did go all the way to zero. I wanted to have just enough so you could see it was still on.

    Not sure if this helps or not...

  3. #3
    Junior Member
    Join Date
    Aug 2020
    Posts
    9
    Hi KurtE,

    Thanks! This does help at lot! I am just trying to save some power by making the backlight go dark. Would you be willing to share the relevant pieces of code for your Well Monitor setup? It sounds like exactly what I need, including the part that changes the brightness based on how long since I last touched the display.

    Thanks Kurt!

    Bob

  4. #4
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,922
    The WIP code is up at: https://github.com/KurtE/WellMonitor
    It is in the display.cpp file.

    But again, nothing special, just a few calls to analogWrite(...)

    When the program starts it cycles through a couple settings to let me know it is hooked up...

    And it can probably be cleaned up some...
    Code:
    //====================================================================================
    // SetFullTFTBacklight - Turn to max brightness... May change later...
    //====================================================================================
    void SetFullTFTBacklight() 
    {
      if (g_backlight_value != TFT_DIM_MIN) {
        g_backlight_value = TFT_DIM_MIN;
        analogWrite(TFT_BACKLIGHT, g_backlight_value);
      }
      g_tft_backlight_timer = millis(); // remember last time touched.
    }
    
    
    //====================================================================================
    // ProcessTouchScreen - Process input from Touch Screen.
    //====================================================================================
    bool ProcessTouchScreen()
    {
      if (!ts.touched()) { // only process when touched.  empty is just a timer...
        if ((g_backlight_value < TFT_DIM_MAX) && (millis() - g_tft_backlight_timer) >= TFT_DIM_INTERVAL_MILLIS) {
          g_backlight_value += TFT_DIM_AMOUNT;
          if ( g_backlight_value > TFT_DIM_MAX) g_backlight_value = TFT_DIM_MAX;
          analogWrite(TFT_BACKLIGHT, g_backlight_value);
          g_tft_backlight_timer = millis();
        }
        return false;
      }
    
      SetFullTFTBacklight();  // Make sure it is on...
      
      int16_t x, y, x_prev = 0xffff, y_prev = 0xffff;
      while (GetTouchPoint(&x, &y)) {
        if ((x != x_prev) || (y != y_prev)) {
          x_prev = x;
          y_prev = y;
          if (g_debug_output)Serial.printf("PTS Raw: %d, %d Out: %d, %d\n", g_pt.x, g_pt.y, x, y);
        }
      }
      g_tft_backlight_timer = millis(); // remember last time touched.
      return true;
    }
    And from one of the headers:
    Code:
    //====================================================================================
    // Touch screen calibrations.  
    //====================================================================================
    // This is calibration data for the raw touch data to the screen coordinates
    // Warning, These are WIP
    #define SCREEN_ORIENTATION_1
    #define TS_MINX 260
    #define TS_MINY 350
    #define TS_MAXX 3825
    #define TS_MAXY 3825
    
    // Note: Dim 0-255 where 0 is full on and 255 full off
    #define TFT_DIM_INTERVAL_MILLIS 10000 // How fast to dim
    #define TFT_DIM_AMOUNT          16    // How much to dim
    #define TFT_DIM_MIN             0     // Full On....
    #define TFT_DIM_MAX             (256-16)  // MAX value

  5. #5
    Junior Member
    Join Date
    Aug 2020
    Posts
    9
    Thanks so much Kurt! I'll give it a go!

    Bob

  6. #6
    Junior Member
    Join Date
    Aug 2020
    Posts
    9
    Hi Kurt. I'm having trouble making that code work in my sketch. With me trying to simultaneously build this project and learn coding at the same time, I'm betting how I added it to my sketch is the culprit. Would you be willing to take a look?

    I'm also wondering were you indicate in the code which pin you're connecting the backlight pin of the display to the PWM pin.

    I'm embarrassed to ask you such dumb questions. Thanks for your help!

    Code:
    /* DISPLAYING ACTIVE MEASUREMENT CHANGES OF VACUUM PRESSURE
    // file: TFT_Display_ongoing_vacuum_changes_20200828.ino
    // FOR TEENSY 4.0 (NOT AUDIO BOARD VERSION) See "connections" chart at https://www.pjrc.com/store/display_ili9341_touch.html for differences in pins
    // e: steve.bull@gmail.com
    // last edit: 20200819
    // Goal: Touch TFT display on Teensy 4.0 to active changes of vacuum pressure
    // Assumptions / needs:
    // 0) use Teensy 4.0 in final deployment (currently working with Teensy 3.2)
    // 1) take pressure readings at 1/second with SparkFunMPL3115A2
    // 2) display #1 pressure as a vacuum readings with negative float number in Pascals (Pa)
    // 3) display #2 provide a pulsing progress bar as the pressure changes 10 times/minute. 
    // 3a) Example subject to change: (2 sec vacuum motor on + 4 sec no motor) * 10 cycles = 1 minute
    // 3b) Touch TFT display+sensor is remote from the vacuum motor micro-controller
    // 4) display #3 average a baseline negative number (the closest to 0) that were read with a 1 minute sampling
    // 5) display #4 average greatest vacuum achieved within the same 1 minute sampling as 4)
    // 6) put screen asleep after 20 seconds
    // 7) wake up screen by simple touch anywhere
    // 8) battery power recommendation to keep the display working with intermittent user touches (~15/day) for minimum of 15 days
    // 9) need low power warning to support 8) to notify user to change the batteries
    // On my own I'll discover how to rotate screen for horizontal display
    */
    
    // about the sensor
    #include <Wire.h>
    #include "SparkFunMPL3115A2.h"
    #include <ILI9341_t3.h>
    #include <font_Arial.h> // from ILI9341_t3
    //Create an instance of the object
    MPL3115A2 myPressure;
    //
    const int SDApin=18;  // identify SDA pin (BLUE wire)
    const int SCLpin=19;  // identify SCL pin (YELLOW wire) 
    
    // about the screen
    // VIN = Vin (3.6 tto 5.5 volts)
    #define TFT_DC       9
    #define TFT_CS      10
    #define TFT_RST    255  // 255 = unused, connect to 3.3V
    #define TFT_MOSI    11
    #define TFT_SCLK    13
    #define TFT_MISO    12
    ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO);
    
    //T_CLK             13
    //T_CS              8
    //T_DIN             11
    //T_DO              12
    //T_IRQ              2
    
    //====================================================================================
    // Touch screen calibrations.  
    //====================================================================================
    // This is calibration data for the raw touch data to the screen coordinates
    // Warning, These are WIP
    #define SCREEN_ORIENTATION_1
    #define TS_MINX 260
    #define TS_MINY 350
    #define TS_MAXX 3825
    #define TS_MAXY 3825
    
    // Note: Dim 0-255 where 0 is full on and 255 full off
    #define TFT_DIM_INTERVAL_MILLIS 10000 // How fast to dim
    #define TFT_DIM_AMOUNT          16    // How much to dim
    #define TFT_DIM_MIN             0     // Full On....
    #define TFT_DIM_MAX             (256-16)  // MAX value
    
    
    
    
    
    
    
    void setup() {
      // set up for pressure sensor
      pinMode(SDApin, SDA); // set pin to SDA mode
      pinMode(SCLpin, SCL);// set pin to SCL mode
      //
      Wire.begin(); // Join i2c bus for pressure sensor
      Serial.begin(9600);
      // Get sensor online
      myPressure.begin(); 
      //Configure sensor
      //myPressure.setModeAltimeter(); // Measure altitude above sea level in meters
      myPressure.setModeBarometer(); // Measure pressure in Pascals from 20 to 110 kPa
      myPressure.setOversampleRate(7); // Set Oversample to the recommended 128
      myPressure.enableEventFlags(); // Enable all three pressure and temp event flags
     
      // setup for touch sensor screen
      tft.begin();
      tft.fillScreen(ILI9341_BLACK);
      tft.setTextColor(ILI9341_WHITE);
      tft.setFont(Arial_14);
      tft.setCursor(36, 8);
      tft.println("breathe.global"); // branding
      tft.setCursor(36, 30);
      tft.println("CUIRASS VACUUM");  // identify purpose of screen
      tft.setCursor(36, 52);
      tft.setFont(Arial_12);
      tft.println("Touch screen to wakeup");  // user interface info
    
    
    //====================================================================================
    // SetFullTFTBacklight - Turn to max brightness... May change later...
    //====================================================================================
    
    {
      if (g_backlight_value != TFT_DIM_MIN) {
        g_backlight_value = TFT_DIM_MIN;
        analogWrite(TFT_BACKLIGHT, g_backlight_value);
      }
      g_tft_backlight_timer = millis(); // remember last time touched.
    }
    
    
    //====================================================================================
    // ProcessTouchScreen - Process input from Touch Screen.
    //====================================================================================
    bool ProcessTouchScreen()
    {
      if (!ts.touched()) { // only process when touched.  empty is just a timer...
        if ((g_backlight_value < TFT_DIM_MAX) && (millis() - g_tft_backlight_timer) >= TFT_DIM_INTERVAL_MILLIS) {
          g_backlight_value += TFT_DIM_AMOUNT;
          if ( g_backlight_value > TFT_DIM_MAX) g_backlight_value = TFT_DIM_MAX;
          analogWrite(TFT_BACKLIGHT, g_backlight_value);
          g_tft_backlight_timer = millis();
        }
        return false;
      }
    
      SetFullTFTBacklight();  // Make sure it is on...
      
      int16_t x, y, x_prev = 0xffff, y_prev = 0xffff;
      while (GetTouchPoint(&x, &y)) {
        if ((x != x_prev) || (y != y_prev)) {
          x_prev = x;
          y_prev = y;
          if (g_debug_output)Serial.printf("PTS Raw: %d, %d Out: %d, %d\n", g_pt.x, g_pt.y, x, y);
        }
      }
      g_tft_backlight_timer = millis(); // remember last time touched.
      return true;
    }
    
    
    
    
    }
    
    // elapsedMillis msecs; // I've a feeling this might be useful later
    
    void loop() {
      /**** I GOT THIS WORKING ****/
      /** Q: what is the most efficient way to set the pressure delivery update 1/sec?" **/
      float pressure = myPressure.readPressure();
      Serial.print("Pressure(Pa): ");
      Serial.print(pressure, 2);
      Serial.println();
    
      /**** GOT THIS WORKING ****/
          tft.setFont(Arial_14);
          tft.fillRect(10, 80, 240, 16, ILI9341_BLACK);
          tft.setCursor(10, 80);
          tft.print("Pressure(Pa):");
          tft.setCursor(140, 80);
          tft.print(pressure);
    
          delay(1000);  // my newbie attempt to take readings every second
          
     /*** BELOW: portion of code lifted from tutorial associated  Part_3_03_TFT_Display.ino 
      *  that I was noodling - thank you Paul
      ***/ 
     /* if (msecs > 145) {
        if (peak1.available() && peak2.available()) {
          msecs = 0;
          float leftNumber = peak1.read();
          float rightNumber = peak2.read();
          Serial.print(leftNumber);
          Serial.print(", ");
          Serial.print(rightNumber);
          Serial.println();
    
          // draw the verticle bars
          // int height = leftNumber * 240;
          int height = leftNumber * 40;
          tft.fillRect(60, 280 - height, 40, height, ILI9341_GREEN);
          tft.fillRect(60, 280 - 240, 40, 240 - height, ILI9341_BLACK);
          height = rightNumber * 240;
          tft.fillRect(140, 280 - height, 40, height, ILI9341_GREEN);
          tft.fillRect(140, 280 - 240, 40, 240 - height, ILI9341_BLACK);
          // a smarter approach would redraw only the changed portion...
    
          // draw numbers underneath each bar
          tft.setFont(Arial_14);
          tft.fillRect(60, 284, 40, 16, ILI9341_BLACK);
          tft.setCursor(60, 284);
          //tft.print(leftNumber);
          tft.print(leftNote);
          tft.fillRect(140, 284, 40, 16, ILI9341_BLACK);
          tft.setCursor(140, 284);
          //tft.print(rightNumber);
          tft.print(rightNote);
        }
      }
          // draw the verticle bars
          // int height = leftNumber * 240;
          int height = leftNumber * 40;
          tft.fillRect(60, 280 - height, 40, height, ILI9341_GREEN);
          tft.fillRect(60, 280 - 240, 40, 240 - height, ILI9341_BLACK);
          height = rightNumber * 240;
          tft.fillRect(140, 280 - height, 40, height, ILI9341_GREEN);
          tft.fillRect(140, 280 - 240, 40, 240 - height, ILI9341_BLACK);
          // a smarter approach would redraw only the changed portion...
    */
          
    }

  7. #7
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,922
    All of my pin definitions are in the globals.h file: https://github.com/KurtE/WellMonitor.../globals.h#L23

    You should be able to define it to any pin on your processor which is marked PWM on the card associated with your board.

    But again depending on which display you are using, you may require some extra circuitry to make it work properly. As I mentioned the Ada fruit boards have a built in transistor to handle it.

    For example I am using the one sold by PJRC which does not have a transistor built in, so I saw how I think it was @FrankB did it on one of his boards and did something similar:

    Click image for larger version. 

Name:	screenshot.jpg 
Views:	16 
Size:	91.3 KB 
ID:	21540

    Note: the area in the Red pin marking is the stuff to do the backlight support.

  8. #8
    Junior Member
    Join Date
    Aug 2020
    Posts
    9
    Thanks Kurt! This is a big help. I do have a PJRC so I'll add a transistor. As for the code I uploaded, do you see anything in the way I added it that might be causing the errors?

    Thanks!

    Bob

  9. #9
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,922
    There were pieces missing, defines missing, ... This builds for me now... Probably things probably not fully there or...

    Code:
    /* DISPLAYING ACTIVE MEASUREMENT CHANGES OF VACUUM PRESSURE
      // file: TFT_Display_ongoing_vacuum_changes_20200828.ino
      // FOR TEENSY 4.0 (NOT AUDIO BOARD VERSION) See "connections" chart at https://www.pjrc.com/store/display_ili9341_touch.html for differences in pins
      // e: steve.bull@gmail.com
      // last edit: 20200819
      // Goal: Touch TFT display on Teensy 4.0 to active changes of vacuum pressure
      // Assumptions / needs:
      // 0) use Teensy 4.0 in final deployment (currently working with Teensy 3.2)
      // 1) take pressure readings at 1/second with SparkFunMPL3115A2
      // 2) display #1 pressure as a vacuum readings with negative float number in Pascals (Pa)
      // 3) display #2 provide a pulsing progress bar as the pressure changes 10 times/minute.
      // 3a) Example subject to change: (2 sec vacuum motor on + 4 sec no motor) * 10 cycles = 1 minute
      // 3b) Touch TFT display+sensor is remote from the vacuum motor micro-controller
      // 4) display #3 average a baseline negative number (the closest to 0) that were read with a 1 minute sampling
      // 5) display #4 average greatest vacuum achieved within the same 1 minute sampling as 4)
      // 6) put screen asleep after 20 seconds
      // 7) wake up screen by simple touch anywhere
      // 8) battery power recommendation to keep the display working with intermittent user touches (~15/day) for minimum of 15 days
      // 9) need low power warning to support 8) to notify user to change the batteries
      // On my own I'll discover how to rotate screen for horizontal display
    */
    
    // about the sensor
    #include <Wire.h>
    #include "SparkFunMPL3115A2.h"
    #include <ILI9341_t3.h>
    #include <font_Arial.h> // from ILI9341_t3
    #include <XPT2046_Touchscreen.h>
    //Create an instance of the object
    MPL3115A2 myPressure;
    //
    const int SDApin = 18; // identify SDA pin (BLUE wire)
    const int SCLpin = 19; // identify SCL pin (YELLOW wire)
    
    // about the screen
    // VIN = Vin (3.6 tto 5.5 volts)
    #define TFT_DC       9
    #define TFT_CS      10
    #define TFT_RST    255  // 255 = unused, connect to 3.3V
    #define TFT_MOSI    11
    #define TFT_SCLK    13
    #define TFT_MISO    12
    ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO);
    
    //T_CLK             13
    //T_CS              8
    //T_DIN             11
    //T_DO              12
    //T_IRQ              2
    
    //////////////////////MISSING DEFINES //////////////////////////////////////////
    uint32_t  g_tft_backlight_timer; // When was the last time the touch screen was touched.
    uint16_t  g_backlight_value = 0; // what is the current backlight 0-255
    // Note: Dim 0-255 where 0 is full on and 255 full off
    #define TFT_DIM_INTERVAL_MILLIS 10000 // How fast to dim
    #define TFT_DIM_AMOUNT          16    // How much to dim
    #define TFT_DIM_MIN             0     // Full On....
    #define TFT_DIM_MAX             (256-16)  // MAX value             
    #define TFT_BACKLIGHT           6
    
    #define TFT_TCS       8     // SPI Touch CS
    #define TFT_TIRQ      3     // Touch screen IRQ pin
    XPT2046_Touchscreen ts(TFT_TCS, TFT_TIRQ);
    
    //====================================================================================
    // Touch screen calibrations.
    //====================================================================================
    // This is calibration data for the raw touch data to the screen coordinates
    // Warning, These are WIP
    #define SCREEN_ORIENTATION_1
    #define TS_MINX 260
    #define TS_MINY 350
    #define TS_MAXX 3825
    #define TS_MAXY 3825
    
    // Note: Dim 0-255 where 0 is full on and 255 full off
    #define TFT_DIM_INTERVAL_MILLIS 10000 // How fast to dim
    #define TFT_DIM_AMOUNT          16    // How much to dim
    #define TFT_DIM_MIN             0     // Full On....
    #define TFT_DIM_MAX             (256-16)  // MAX value
    
    
    
    
    
    
    
    void setup() {
      // set up for pressure sensor
      pinMode(SDApin, SDA); // set pin to SDA mode
      pinMode(SCLpin, SCL);// set pin to SCL mode
      //
      Wire.begin(); // Join i2c bus for pressure sensor
      Serial.begin(9600);
      // Get sensor online
      myPressure.begin();
      //Configure sensor
      //myPressure.setModeAltimeter(); // Measure altitude above sea level in meters
      myPressure.setModeBarometer(); // Measure pressure in Pascals from 20 to 110 kPa
      myPressure.setOversampleRate(7); // Set Oversample to the recommended 128
      myPressure.enableEventFlags(); // Enable all three pressure and temp event flags
    
      // setup for touch sensor screen
      tft.begin();
      tft.fillScreen(ILI9341_BLACK);
      tft.setTextColor(ILI9341_WHITE);
      tft.setFont(Arial_14);
      tft.setCursor(36, 8);
      tft.println("breathe.global"); // branding
      tft.setCursor(36, 30);
      tft.println("CUIRASS VACUUM");  // identify purpose of screen
      tft.setCursor(36, 52);
      tft.setFont(Arial_12);
      tft.println("Touch screen to wakeup");  // user interface info
    
      if (!ts.begin()) {
        Serial.println("Couldn't start touchscreen controller");
      }
    
    
    }
    
    //====================================================================================
    // SetFullTFTBacklight - Turn to max brightness... May change later...
    //====================================================================================
    void SetFullTFTBacklight()
    {
      if (g_backlight_value != TFT_DIM_MIN) {
        g_backlight_value = TFT_DIM_MIN;
        analogWrite(TFT_BACKLIGHT, g_backlight_value);
      }
      g_tft_backlight_timer = millis(); // remember last time touched.
    }
    
    //====================================================================================
    // GetTouchPoint - Return true if touched and convert touch point to X and Y values
    //====================================================================================
    TS_Point g_pt;  // currently global for debug stuff
    
    bool GetTouchPoint(int16_t *px, int16_t *py)
    {
      if (!ts.touched()) // only process when touched.  empty is just a timer...
        return false;
    
      g_pt = ts.getPoint();
    
      // Scale from ~0->4000 to tft.width using the calibration #'s
    #ifdef SCREEN_ORIENTATION_1
      *px = map(g_pt.x, TS_MINX, TS_MAXX, 0, tft.width());
      *py = map(g_pt.y, TS_MINY, TS_MAXY, 0, tft.height());
    #else
      *px = map(g_pt.x, TS_MINX, TS_MAXX, tft.width(), 0);
      *py = map(g_pt.y, TS_MAXY, TS_MINY, 0, tft.height());
    #endif
      return true;
    
    }
    
    
    //====================================================================================
    // ProcessTouchScreen - Process input from Touch Screen.
    //====================================================================================
    bool ProcessTouchScreen()
    {
      if (!ts.touched()) { // only process when touched.  empty is just a timer...
        if ((g_backlight_value < TFT_DIM_MAX) && (millis() - g_tft_backlight_timer) >= TFT_DIM_INTERVAL_MILLIS) {
          g_backlight_value += TFT_DIM_AMOUNT;
          if ( g_backlight_value > TFT_DIM_MAX) g_backlight_value = TFT_DIM_MAX;
          analogWrite(TFT_BACKLIGHT, g_backlight_value);
          g_tft_backlight_timer = millis();
        }
        return false;
      }
    
      SetFullTFTBacklight();  // Make sure it is on...
    
      int16_t x, y, x_prev = 0xffff, y_prev = 0xffff;
      while (GetTouchPoint(&x, &y)) {
        if ((x != x_prev) || (y != y_prev)) {
          x_prev = x;
          y_prev = y;
        }
      }
      g_tft_backlight_timer = millis(); // remember last time touched.
      return true;
    }
    
    // elapsedMillis msecs; // I've a feeling this might be useful later
    
    void loop() {
      ProcessTouchScreen();
      /**** I GOT THIS WORKING ****/
      /** Q: what is the most efficient way to set the pressure delivery update 1/sec?" **/
      float pressure = myPressure.readPressure();
      Serial.print("Pressure(Pa): ");
      Serial.print(pressure, 2);
      Serial.println();
    
      /**** GOT THIS WORKING ****/
      tft.setFont(Arial_14);
      tft.fillRect(10, 80, 240, 16, ILI9341_BLACK);
      tft.setCursor(10, 80);
      tft.print("Pressure(Pa):");
      tft.setCursor(140, 80);
      tft.print(pressure);
    
      delay(1000);  // my newbie attempt to take readings every second
    
      /*** BELOW: portion of code lifted from tutorial associated  Part_3_03_TFT_Display.ino
          that I was noodling - thank you Paul
       ***/
      /* if (msecs > 145) {
         if (peak1.available() && peak2.available()) {
           msecs = 0;
           float leftNumber = peak1.read();
           float rightNumber = peak2.read();
           Serial.print(leftNumber);
           Serial.print(", ");
           Serial.print(rightNumber);
           Serial.println();
    
           // draw the verticle bars
           // int height = leftNumber * 240;
           int height = leftNumber * 40;
           tft.fillRect(60, 280 - height, 40, height, ILI9341_GREEN);
           tft.fillRect(60, 280 - 240, 40, 240 - height, ILI9341_BLACK);
           height = rightNumber * 240;
           tft.fillRect(140, 280 - height, 40, height, ILI9341_GREEN);
           tft.fillRect(140, 280 - 240, 40, 240 - height, ILI9341_BLACK);
           // a smarter approach would redraw only the changed portion...
    
           // draw numbers underneath each bar
           tft.setFont(Arial_14);
           tft.fillRect(60, 284, 40, 16, ILI9341_BLACK);
           tft.setCursor(60, 284);
           //tft.print(leftNumber);
           tft.print(leftNote);
           tft.fillRect(140, 284, 40, 16, ILI9341_BLACK);
           tft.setCursor(140, 284);
           //tft.print(rightNumber);
           tft.print(rightNote);
         }
        }
           // draw the verticle bars
           // int height = leftNumber * 240;
           int height = leftNumber * 40;
           tft.fillRect(60, 280 - height, 40, height, ILI9341_GREEN);
           tft.fillRect(60, 280 - 240, 40, 240 - height, ILI9341_BLACK);
           height = rightNumber * 240;
           tft.fillRect(140, 280 - height, 40, height, ILI9341_GREEN);
           tft.fillRect(140, 280 - 240, 40, 240 - height, ILI9341_BLACK);
           // a smarter approach would redraw only the changed portion...
      */
    
    }

  10. #10
    Junior Member
    Join Date
    Aug 2020
    Posts
    9
    Thanks so much Kurt! I believe I got it working! Is there a variable in there that's the time it stays "full on" after it's touched before dimming again?

  11. #11
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,922
    Right now the code is pretty simplistic. It worked well enough for me: It has to do with the code fragment:

    Code:
    bool ProcessTouchScreen()
    {
      if (!ts.touched()) { // only process when touched.  empty is just a timer...
        if ((g_backlight_value < TFT_DIM_MAX) && (millis() - g_tft_backlight_timer) >= TFT_DIM_INTERVAL_MILLIS) {
          g_backlight_value += TFT_DIM_AMOUNT;
          if ( g_backlight_value > TFT_DIM_MAX) g_backlight_value = TFT_DIM_MAX;
          analogWrite(TFT_BACKLIGHT, g_backlight_value);
          g_tft_backlight_timer = millis();
        }
        return false;
      }
    So every TFT_DIM_INTERVAL_MILLIS milliseconds it will increase the backlight value by TFT_DIM_AMOUNT until it gets to the max value.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •