Printing to LCD but only when touchscreen is pressed

I have used a Graphing to LCD program and have been modifying it to suit my application (which is very simple)

I need to read a value from an analog pin every 200mSec and display the point on a graph.
Set an Alarm level line on the graph and trigger a relay if it exceeds the Alarm level.
Print the weight at the bottom of the screen that the sensor is reading.

The weigh at the bottom of the screen is where I am having a problem. It only prints the value if I push on the screen somewhere and each time I touch the screen the value will update. I am trying to print the weight continuously.

I have moved that part of the code to several different places expecting it would be outside a conditional loop somewhere, but always the same result. I don’t understand enough where the program is sitting to wait for some input. I thought the IF statements would just check if the condition was TRUE, and if the program continues to execute.

Any help as to what I am misunderstanding would be greatly appreciated.

void loop () {

  //Read Value from Strain Gauge and send
  //it to Tension Function then return calculated weight.
  sensor1Val = analogRead(sensor1Pin);
  Kg = Tension(sensor1Val);

  TSPoint p = ts.getPoint();

  if (p.z > ts.pressureThreshhold) {
    //get point on touchscreen that has been touched

    pinMode(YP, OUTPUT);     //.kbv these pins are shared with TFT
    pinMode(XM, OUTPUT);     //.kbv these pins are shared with TFT

    tft.fillRect(100, Kg, 1, 2, YELLOW);
    tft.setCursor(220, 210);

    // we have some minimum pressure we consider 'valid'
    // pressure of 0 means no pressing!
    if (p.z > ts.pressureThreshhold) {
      //Determine if the "INC" alarm button is pressed and draw
      //new Alarm line
      if (p.x > 794 && p.x < 872 && p.y > 138 && p.y < 265) {

        tft.drawLine(50, Alarm, 315, Alarm, BLACK);
        Alarm = Alarm + 10 ;
        if (Alarm > 200) Alarm = 200;
        tft.drawLine(50, Alarm, 315, Alarm, RED);
      //Determine if the "DEC" alarm level is pressed and draw
      //new alarm line
      if (p.x > 811 && p.x < 879 && p.y > 295 && p.y < 414) {
        tft.drawLine(50, Alarm, 315, Alarm, BLACK);
        Alarm = Alarm - 10 ;
        if (Alarm < 0) Alarm = 0;
        tft.drawLine(50, Alarm, 315, Alarm, RED);
        //Serial.println (Alarm);
        //Serial.println(p.x); Serial.println(p.y);

//Print the Alarm Level Value on the side that corresponds with the
//Alarm Level line.
      AlarmLevel = 400 - (Alarm * 2);
      tft.fillRect(1, 0, 43, 203, BLACK);
      tft.setCursor(5, Alarm);



  // Print the Weight Value and Kg at the bottom left of screen.
  tft.setCursor(280, 210);
  tft.setCursor(220, 210);
  tft.fillRect(220, 210, 54, 22, BLACK);

int Tension(int Weight) {
  Weight = sensor1Val / 3;

  return Weight;

This should be in setup( ):

pinMode( YP, OUTPUT );     //.kbv these pins are shared with TFT
pinMode( XM, OUTPUT );     //.kbv these pins are shared with TFT

You have two identical lines:

if( p.z > ts.pressureThreshhold ) {

Not familiar with the tft lib. but this looks funny to me:

tft.fillRect( 100, Kg, 1, 2, YELLOW );

When Kg prints, does is show for just 300mS?

Thanks Boolrules,

From my understanding the pinModes are required in the loop because of the dual use of the pins for the LCD and the Touchscreen. I did try with them in the setup, but the screen goes to white when I press the screen.

I think you have hit the nail on the head with 2 identical lines. The statement only prints when there is a screen touch.

Getting rid of the extra one now causes me another issue. I am just starting to try Functions. The Function in this code gives the error "Tension" was not declared in this scope. Does this need to be declared in the Loop function?

tft.fillRect( 100, Kg, 1, 2, YELLOW );

Format is (X position of top left hand corner, Y position of top left hand corner, width in pixels, height in pixels, color,) and this seems to work OK in other places I have used it.

I am using this for plotting the point on the graph. My intention is to use a loop of the graph screen in pixels and set the Y value by using the weight returned from Tension()

The Kg when it prints stays there and if I change the analog input value, next time I touch the screen it updates to the new value.


Well I really don’t know. You can try this:

<whatever_type_Kg_is> oldKg = 0;     // make a new variable (you don't show your Kg declaration so I don't know the type)

if( Kg != oldKg ) {
    oldKg = Kg;
    // now put your Kg printing block here

Thanks boolrules,

that has got me on the right track :slight_smile: