Screen graphics clearing

Hi,
below is some code that reads a straingauge input via a HX711 then displaying the weight on a graph.

I wish to have a hidden button that clears the screen and then I can add the Scale factor in to calibrate the output of the HX711 to read Kg.

All working so far, problem is when I hit the hidden button the screen clears to Black, but the graph, alarm button and Alarm line redraw on the screen. I understand why they do it, because the loop is still running, what I want help with is the best method (read easiest) to solve the issue so these dont show. I could put in an If in the calibrate screen, but am sure there is a more elegant solution, I just cant think of it.
Thanks
Sutto55

#include <pin_magic.h>
#include <registers.h>

#include <HX711.h>

#include <TouchScreen.h>

#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
#define DOUT A14
#define CLK A15

#include <SPI.h>          // f.k. for Arduino-1.5.2
#include "Adafruit_GFX.h"// Hardware-specific library
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;

// Assign human-readable names to some common 16-bit color values:
#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define GREY      0xCE79
#define LIGHTGREY 0xDEDB


// These are the pins for the shield!
#define YP A3  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 9   // can be a digital pin
#define XP 8   // can be a digital pin
#define MINPRESSURE 60   //touch screen pressure Minimum
#define MAXPRESSURE 1000  // Touchscreen pressure Maximum
#define relayPin 49 //will turn on to power the relay when the Actual weight is < the alarm setpoint


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



long int sensor1Val = 0; // used to hold sensor data. This is read via I2C on pins A14 and A15. This is a data stream, not an analog value. The HX711 sends out 24 bits of data
int sensor1ValOld = 0; // holds previous value for line graph
int Alarm = 110; // Just an initial value
int AlarmPoint; // setpoint that is the trip point for the weight
int oldAlarmPoint; //used to check if the Alarm point has been changed
int oldAlarm;
int Xposn = 51, Yposn;
int Weight;
int Kg;
int oldKg = 0;
int plotVal ; //the weight read from the strain gauge
int oldPlotVal; //used as the start point of the line on the graph. The program draws a line between the oldPlotVal to plotVal so it is a continuous line and not a row of dots
bool AlarmTripped = false; // used to test when the alarm has been exceeded and waits for a reset touch on the screen to latch out the speed control signal
float ScaleValue;
HX711 scale(DOUT, CLK); //Send scale factor to the HX711
float calibration_factor = -7050;


/////////////////////////////////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(9600);

  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, HIGH); // turn sensor power on
  scale.set_gain(128);
  tft.drawRect(50, 0, 319, 200, BLUE); // draw the graph outline
  scale.tare();                    // reset the scale to 0

  int i = 0;

  // Draw crosshairs {

  tft.reset();
  delay(500);

  uint16_t identifier = 0x7783;

  tft.begin(identifier);

  delay(500);

  tft.fillRect(0, 0, 240, 320, BLACK);
  tft.setRotation(3);
  tft.drawRect(50, 1, 270, 200, BLUE);

  // for (int i = 0; i < 200; i += 9) {
  //   tft.drawLine(45, i, 50, i, BLUE);
  //all the following just setup the screen display that does not change
  // }
  tft.fillRoundRect(10, 205, 50, 30, 8, GREEN);
  tft.fillRoundRect(80, 205, 50, 30, 8, GREEN);
  tft.setCursor(17, 215);
  tft.setTextColor(BLACK);
  tft.setTextSize(2);
  tft.print("INC");
  tft.setCursor(87, 215);
  tft.setTextColor(BLACK);
  tft.setTextSize(2);
  tft.print("DEC");
  tft.setTextColor(YELLOW);
  tft.setTextSize(3);
  tft.setCursor(280, 210);
  tft.print("Kg");
  tft.fillCircle(160, 220, 15, GREEN);


  scale.tare();               // reset the scale to 0
  scale.set_scale(2427);


}





//////////////////////////////////////////////////////////////////////////////////


void loop () {

  int AlarmLine;
  //Read Value from Strain Gauge and send
  //it to Tension Function then return calculated weight.
  Kg = scale.get_units(4); //Raw Value in KG after scaling in the HX711
  Serial.println(Kg);// the calculated Kg is sent to the Serial port and can be monitored with the Serial Monitor. It can also be graphed on the serial plotter
  if (Kg < 0)Kg = 0;
  //Kg=0;

  plotVal = (-.829 * Kg) + 200;

  //Serial.println(plotVal);
  tft.drawLine(Xposn, oldPlotVal, Xposn + 1, plotVal, YELLOW);// line drawing on the graph from oldPlotVal to PlotVal and incrementing by 1 pixel
  //delay(100);
  if (plotVal != oldPlotVal) {
    oldPlotVal = plotVal;// moving the new plotVal into oldPlotVal as the start of the line for the next value
  }

  if ( Kg != oldKg ) {
    oldKg = Kg;

    tft.fillRect(220, 210, 60, 40, BLACK);// dispaly the Kg value at the bottom RH of the screen
    tft.setTextColor(YELLOW);
    tft.setTextSize(3);
    tft.setCursor(220, 210);
    tft.print(Kg);

  }
  // this increments the pixel for the next point on the graph
  Xposn = Xposn + 1;
  if (Xposn > 317) {
    Xposn = 52;
    tft.fillRect(51, 2, 268, 197, BLACK);// and this clears the plot area and resets the cursor to the LHS of the plot screen


  }
  AlarmLine = AlarmLevel ();// calculation of the Alarm line

  AlarmPoint = (-0.829 * Alarm) + 200 ;
  tft.drawLine (52, AlarmPoint, 318, AlarmPoint, RED);

  tft.setTextColor(RED);// draw the alarm line
  tft.setTextSize(2);
  tft.setCursor(0, (AlarmPoint - 10));

  if (Alarm != oldAlarm) {// overwrite the red line with a black line if the Alarm Level is changed
    tft.fillRect(0, (AlarmPoint - 20), 40, 33, BLACK);

    tft.print(Alarm);
    oldAlarm = Alarm;
  }

  TSPoint p = ts.getPoint();

  pinMode(YP, OUTPUT);     // these lines capture a screen press and resets AlarmTripped value from True to false
  pinMode(XM, OUTPUT);

  if (p.z > ts.pressureThreshhold) {
    Serial.println("Im in here");
    AlarmTripped = false;
  }
  if (Kg > Alarm & ! AlarmTripped ) { // the lines here look for Kg to be > alarm and NOT AlarmTripped. If true this sets the AlarmTripped Var to true
    AlarmTripped = true;

  }
  switch (AlarmTripped) {// this evaluates if the AlarmTripped value and either latches the Relay off or On
    case 0:
      tft.fillCircle(160, 220, 15, GREEN);
      digitalWrite (relayPin, HIGH);
      break;
    case 1:
      tft.fillCircle(160, 220, 15, RED);
      digitalWrite(relayPin, LOW);
      break;
  }

    if (p.z > ts.pressureThreshhold) {
    if (p.x > 784 && p.x < 902 && p.y > 76 && p.y < 180){
        tft.fillScreen(BLACK);
    }
    }
      delay(100);
  
}


// the below function looks at which button is pressed INC or DEC and moves the Alarm level to the required value. It is limited to 10 and 240

int AlarmLevel() {
  TSPoint p = ts.getPoint();
  pinMode(YP, OUTPUT);
  pinMode(XM, OUTPUT);



  if (p.z > ts.pressureThreshhold) {
    if (p.x > 128 && p.x < 200 && p.y > 764 && p.y < 860) {
      tft.drawLine (52, ((-0.829 * Alarm) + 200), 318, ((-0.829 * Alarm) + 200), BLACK);
      Alarm = Alarm + 10;
      if ( Alarm > 240) {
        Alarm = 240;

      }

    }


    if (p.x > 128 && p.x < 200 && p.y > 592 && p.y < 690) {
      tft.drawLine (52, ((-0.829 * Alarm) + 200), 318, ((-0.829 * Alarm) + 200), BLACK);
      Alarm = Alarm - 10 ;
      if (Alarm < 10) {
        Alarm = 10;

      }
    }

    return Alarm;
  }
}

This line

if (Kg > Alarm & ! AlarmTripped )

is not correct. '&' it bitwise and. You want '&&' which is logical and

You have explained what happens and what is wrong with it, but I cannot figure out what you want instead. Those other things must be drawn eventually. What do you want to have happen and when?

When I jump to the calibrate screen, I want to bring up a keypad, enter a scaling value and write the scale value to EEprom so the next time I power up the scaling value can be read from EEprom and sent to the HX77.

As it is now the graph, Alarm line and Alarm indicator will overwrite the graphics I use for the keypad so I need to prevent that from happening. My thought was a function that is called when the hidden button is pressed, but I dont know the Function well enough to know if the program will continue to run in there without jumping back to the Main loop before I complete the Calibrate part of the program

Thanks
Sutto55

It sounds like you need a global variable that indicates which screen is current active - either the graph screen or the keyboard screen. Then, when you are going through loop() you will react differently based on which screen is active. You could also look into the finite-state-machine (FSM) library since your problem could be solved by different states (e.g. ENTER_GRAPH, IN_GRAPH, ENTER_KEYBOARD, IN_KEYBOARD)

OK, Thanks for the ideas, I will see how I go.

sutto55