sliders bouncing without being touched on TFT screen

So I have a McuFriend copy screen from Jay car.

I wrote a program to change an RGB LED based on the positions of the sliders as I couldn’t use the Touch and UTFT libraries from Rinky Dink Electronics.

Now For some reason the red slider is stable and I can easily move it up and down and it stays where it belongs.

I cut and paste the code for the red and altered it for the Green and blue, but… neither of the green or blue sliders stay where i put them. They keep defaulting to about 20 pixels from the bottom of the sliders and bouncing around even though no one is touching them.

whats going on why are the green and blue sliders jumping when the red is fine and based on same code?

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <SPI.h>
#include <Wire.h>      // this is needed even tho we aren't using it
#include <TouchScreen.h>


#define MINPRESSURE 10  //Min prssure to activate touch screen
#define MAXPRESSURE 1000  //Max prssure to activate touch screen

#define BLACK   0x0000    //translates colour codes to english text
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

// ALL Touch panels and wiring is DIFFERENT
// copy-paste results from TouchScreen_Calibr_native.ino
const int XP = 8, XM = A2, YP = A3, YM = 9; //ID=0x8230 sets pin for communication
const int TS_LEFT = 942, TS_RT = 139, TS_TOP = 136, TS_BOT = 907; // setp up touch for portrait

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); 

Adafruit_GFX_Button on_btn;

int pixel_x, pixel_y;     //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
    TSPoint p = ts.getPoint();
    pinMode(YP, OUTPUT);      //restore shared pins
    pinMode(XM, OUTPUT);
    digitalWrite(YP, HIGH);   //because TFT control pins are digital
    digitalWrite(XM, HIGH);
    bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);  //looks for detected pressure on screen
    if (pressed) {
        pixel_x = map(p.x, TS_LEFT, TS_RT, 0, 240); //return pressure point data
        pixel_y = map(p.y, TS_TOP, TS_BOT, 0, 320);
    }
    return pressed;
}



// Colour and position variables
int red_value = 255;
int green_value = 255;
int blue_value = 255;
int red_position = 50;
int green_position = 50;
int blue_position = 50;
int r_old_position = 0;
int g_old_position = 0;
int b_old_position = 0;
int fill_color = tft.color565(red_value, green_value, blue_value);

// LED Variables
boolean led_state = true;
int led_r_pin = 44;
int led_g_pin = 46;
int led_b_pin = 45;





void setup() {

  Serial.begin(9600);
     uint16_t ID = tft.readID();
    Serial.print("TFT ID = 0x");
    Serial.println(ID, HEX);
    Serial.println("Calibrate for your Touch Panel");
    if (ID == 0xD3D3) ID = 0x8230; // write-only shield
    tft.begin(ID);
    tft.setRotation(0);            //PORTRAIT
    tft.fillScreen(BLACK);
 
 // Start our touchscreen
//  if (!ts.begin()) {     //commented out
//  while (1);             //comnetted out 
//  }

  // Initialize our RGB LED
 
  pinMode(led_r_pin, OUTPUT);
  pinMode(led_g_pin, OUTPUT);
  pinMode(led_b_pin, OUTPUT);
  digitalWrite(led_r_pin, red_value);
  digitalWrite(led_g_pin, green_value);
  digitalWrite(led_b_pin, blue_value);
  
  draw_screen();
  
}

void loop() {
  digitalWrite(13, HIGH);
  TSPoint p = ts.getPoint();
  digitalWrite(13, LOW);

  // if sharing pins, you'll need to fix the directions of the touchscreen pins
  pinMode(XP, OUTPUT);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  pinMode(YM, OUTPUT);

  // we have some minimum pressure we consider 'valid'
  // pressure of 0 means no pressing!

  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
  
    Serial.print("X = "); Serial.print(p.x);
    Serial.print("\tY = "); Serial.print(p.y);
    Serial.print("\tPressure = "); Serial.println(p.z);
   
  }

  // Retrieve a point  
//  TSPoint p = ts.getPoint();

Serial.println("XY point retrived"); //diagnosis notes

  

  // Scale from ~0->4000 to tft.width using the calibration #'s
   pixel_x = map(p.x, TS_LEFT, TS_RT, 0, 240); //.kbv makes sense to me
   pixel_y = map(p.y, TS_TOP, TS_BOT, 0, 320);
  
    Serial.print("("); Serial.print(pixel_x);
    Serial.print(", "); Serial.print(pixel_y);
    Serial.println(")");
    

  // Check sliders
  if (pixel_y > 50 && pixel_y < 230) {
    // Red Slider
    if (pixel_x > 40 && pixel_x < 80) {
      // Map slider y value to LED intensity
      red_value = map(p.y, 230, 50, 0, 255);

Serial.println("check red slider"); //diagnosis notes
    
  
      // Grab new slider position after storing the old one
      r_old_position = red_position;
      red_position = pixel_y;

      // Black out the old slider, re-draw the verticle bar, then draw the new slider
      tft.drawFastHLine(40, r_old_position, 40, BLACK);
      tft.drawPixel(60, r_old_position, WHITE);
      tft.drawFastHLine(40, red_position, 40, WHITE);
    }

Serial.println("red slider updated");; //diagnosis notes
    
   // Check sliders
  if (pixel_y > 50 && pixel_y < 230) {
    // Green Slider
    if (pixel_x > 100 && pixel_x < 140) {
      // Map slider y value to LED intensity
      green_value = map(p.y, 230, 50, 0, 255);

Serial.println("check green slider"); //diagnosis notes
    

      // Grab new slider position after storing the old one
      g_old_position = green_position;
      green_position = pixel_y;

      // Black out the old slider, re-draw the verticle bar, then draw the new slider
      tft.drawFastHLine(100, g_old_position, 40, BLACK);
      tft.drawPixel(120, g_old_position, WHITE);
      tft.drawFastHLine(100, green_position, 40, WHITE);
    }

Serial.println("green slider updated"); //diagnosis notes
     
   
     // Check sliders
  if (pixel_y > 50 && pixel_y < 230) {
    // Blue Slider
    if (pixel_x > 160 && pixel_x < 200) {
      // Map slider y value to LED intensity
      blue_value = map(p.y, 230, 50, 0, 255);

Serial.println("check blue slider"); //diagnosis notes

      // Grab new slider position after storing the old one
      b_old_position = blue_position;
      blue_position = pixel_y;

      // Black out the old slider, re-draw the verticle bar, then draw the new slider
      tft.drawFastHLine(160, b_old_position, 40, BLACK);
      tft.drawPixel(180, b_old_position, WHITE);
      tft.drawFastHLine(160, blue_position, 40, WHITE);
      
Serial.println("blue slider updated"); //diagnosis notes    
    
    }
  }

  // Toggle the LED state (on/off)
  if ((pixel_x > 60 && pixel_x < 180) && (pixel_y > 270 && pixel_y < 310)) {
    led_state = ! led_state;
    delay(250);

  Serial.println("green slider updated"); //diagnosis notes
  
  }

  // Set the LED colour and fill colour based on the slider positions
  if (led_state) {
    fill_color = tft.color565(red_value, green_value, blue_value);
    digitalWrite(led_r_pin, red_value);
    digitalWrite(led_g_pin, green_value);
    digitalWrite(led_b_pin, blue_value);
  } else {
    fill_color = BLACK;
    digitalWrite(led_r_pin, 0);
    digitalWrite(led_g_pin, 0);
    digitalWrite(led_b_pin, 0);
  }
  
  // Redraw LED Status Button
  tft.fillRect(60, 270, 120, 40, fill_color);
  tft.drawRect(60, 270, 120, 40, WHITE);
}
  }

}  
void draw_screen() {
  // Create a blank, black screen
  tft.fillScreen(BLACK);

  // Vertical slider lines and labels
  tft.fillCircle(60, 20, 10, RED);
  tft.fillCircle(120, 20, 10, GREEN);
  tft.fillCircle(180, 20, 10, BLUE);
  tft.drawFastVLine(60, 50, 180, WHITE);
  tft.drawFastVLine(120, 50, 180, WHITE);
  tft.drawFastVLine(180, 50, 180, WHITE);
  tft.drawFastHLine(40, red_position, 40, WHITE);
  tft.drawFastHLine(100, green_position, 40, WHITE);
  tft.drawFastHLine(160, blue_position, 40, WHITE);

  // LED Status Button
  tft.fillRect(60, 270, 120, 40, fill_color);
  tft.drawRect(60, 270, 120, 40, WHITE);



}

First off, I changed your MINPRESSURE value and replaced your calibration lines with mine e.g.

...
#define MINPRESSURE 200  //Min prssure to activate touch screen
...
//const int XP = 8, XM = A2, YP = A3, YM = 9; //ID=0x8230 sets pin for communication
//const int TS_LEFT = 942, TS_RT = 139, TS_TOP = 136, TS_BOT = 907; // setp up touch for portrait
const int XP = 6, XM = A2, YP = A1, YM = 7; //240x320 ID=0x9341
const int TS_LEFT = 896, TS_RT = 126, TS_TOP = 944, TS_BOT = 148;
...

Yes, your program was crazy. It reported good presses but acted on good or bad presses.

You should only process valid presses.

I have changed your logic:

    //if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
    if (p.z < MINPRESSURE || p.z > MAXPRESSURE) return; //INVALID

        Serial.print("X = "); Serial.print(p.x);
        Serial.print("\tY = "); Serial.print(p.y);
        Serial.print("\tPressure = "); Serial.println(p.z);

    //}

Please let me know how you get on.

David.

It worked with a minor adjustment,

it works beautifully now thank you for that. I was not aware of the coding difference. Will know for future reference now.

Yours

  //if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
    if (p.z < MINPRESSURE || p.z > MAXPRESSURE) return; //INVALID

        Serial.print("X = "); Serial.print(p.x);
        Serial.print("\tY = "); Serial.print(p.y);
        Serial.print("\tPressure = "); Serial.println(p.z);

    //}

Mine

if (p.z < MINPRESSURE || p.z > MAXPRESSURE) {
  return; //INVALID
    Serial.print("X = "); Serial.print(p.x);
    Serial.print("\tY = "); Serial.print(p.y);
    Serial.print("\tPressure = "); Serial.println(p.z);

Look carefully at your logic.

I suggest that you try my edit. Then format with ctrl-T.
You will see which lines get executed.

Mind you, you only need the Serial.print() statements if debugging.

David.

OK I see it makes a big difference when I look at the serial output thank you for that.