user73183: In code attached below I am using tftlcd display of 2.4 inch in which i want to show clock as home page, when user click on tftlcd screen then a screen appears which can show three options to user which are about the sensors user want to use. There are three sensors in this code: Pulse oximeter, glucose meter, pedometer. After choosing some option(sensor) the data of sensor will appear on screen.
I have write code for all this procedure which is given below:
//include all libraries
#include <Wire.h>
#include <SPFD5408_Adafruit_GFX.h>
#include <SPFD5408_Adafruit_TFTLCD.h>
#include <SPFD5408_TouchScreen.h>
#include "MAX30105.h"
#include "spo2_algorithm.h"

//Exit buttons
bool backsensed = false;
bool resetsensed = false;

//Include Max30102 lib
MAX30105 particleSensor;

//Define LCD pins
#define YP A1  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 7   // can be a digital pin
#define XP 6   // can be a digital pin

//pedometer
const int xpin = A3;
const int ypin = A4;
const int zpin = A5;
byte p[8] = {0x1F,
  0x1F,
  0x1F,
  0x1F,
  0x1F,
  0x1F,
  0x1F,
  0x1F
};
float threshold = 100;
float xval[100] = {0};
float yval[100] = {0};
float zval[100] = {0};
float xavg, yavg, zavg;
int steps, flag = 0;
//pulseoxi
#define MAX_BRIGHTNESS 255
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
uint16_t irBuffer[100]; //infrared LED sensor data
uint16_t redBuffer[100];  //red LED sensor data
#else
uint32_t irBuffer[100]; //infrared LED sensor data
uint32_t redBuffer[100];  //red LED sensor data
#endif
int32_t bufferLength; //data length
int32_t spo2; //SPO2 value
int8_t validSPO2; //indicator to show if the SPO2 calculation is valid
int32_t heartRate; //heart rate value
int8_t validHeartRate; //indicator to show if the heart rate calculation is valid
byte pulseLED = 11; //Must be on PWM pin
byte readLED = 13; //Blinks with each data read

// LCD Calibrate values
#define TS_MINX 100
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

// For better pressure precision, we need to know the resistance between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

int BLUE = tft.color565(50, 50, 255);
#define TFT_BLACK   0x0000
#define TFT_DARKBLUE 0x0010
#define VIOLET 0x8888
#define TFT_RED     0xF800
#define TFT_GREEN   0x07E0
#define TFT_CYAN    0x07FF
#define TFT_MAGENTA 0xF81F
#define TFT_YELLOW  0xFFE0
#define TFT_WHITE   0xFFFF
#define TFT_GREY    0x5AEB
#define TFT_BLUE    0x001F
#define GOLD 0xFEA0
#define BROWN 0xA145
#define SILVER 0xC618
#define LIME 0x07E0
#define BLUE  0x001F

//button screen
int currentpcolour;
int currentpage;
bool running;

//Minimum and maximum pressure to sense the touch
#define MINPRESSURE 10
#define MAXPRESSURE 1000

//clock
float sx = 0, sy = 1, mx = 1, my = 0, hx = -1, hy = 0;    
float sdeg = 0, mdeg = 0, hdeg = 0;
uint16_t osx = 120, osy = 120, omx = 120, omy = 120, ohx = 120, ohy = 120; 
int16_t x0 = 0, x1 = 0, yy0 = 0, yy1 = 0, x00 = 0, yy00 = 0;
uint32_t targetTime = 0;                    
uint16_t xpos; 
uint8_t conv2d(const char* p) {
  uint8_t v = 0;
  if ('0' <= *p && *p <= '9')
    v = *p - '0';
  return 10 * v + *++p - '0';
}
uint8_t hh = conv2d(__TIME__), mm = conv2d(__TIME__ + 3), ss = conv2d(__TIME__ + 6); 
boolean initial = 1;
char d;

//for buttons
void drawHome()
{
  tft.fillScreen(TFT_BLACK);
  tft.drawRoundRect(0, 0, 319, 240, 8, TFT_YELLOW);     //Page border

  tft.fillRoundRect(60, 180, 200, 40, 8, TFT_DARKBLUE);
  tft.drawRoundRect(60, 180, 200, 40, 8, TFT_YELLOW);  //PEDO

  tft.fillRoundRect(60, 130, 200, 40, 8, TFT_DARKBLUE);   //Glu
  tft.drawRoundRect(60, 130, 200, 40, 8, TFT_YELLOW);

  tft.fillRoundRect(60, 80, 200, 40, 8, TFT_DARKBLUE);
  tft.drawRoundRect(60, 80, 200, 40, 8, TFT_YELLOW); //Pulse oxi and heart rate

  tft.setCursor(25, 20);
  tft.setTextSize(3);
  tft.setTextColor(TFT_WHITE);
  tft.print("Welcome Here..");
  tft.setCursor(25, 50);
  tft.setTextSize(2);
  tft.setTextColor(TFT_YELLOW);
  tft.print("Choose Your Sensor");
  tft.setTextColor(TFT_WHITE);
  tft.setCursor(110, 195);
  tft.print("Pedometer");

  tft.setCursor(80, 145);
  tft.print("Glucose Sensor");

  tft.setCursor(80, 95);
  tft.print("Pulse Oximeter");
  //  delay(500);

  }

  void setup()
{
  tft.reset();  
  tft.begin(0x9341); 
  tft.setRotation(1);
  currentpage = 0; //clock page
  tft.setTextColor(TFT_WHITE);// text color
  tft.fillScreen(TFT_BLACK);// background color

  xpos = tft.width() / 2; 
  tft.drawCircle(xpos, 120, 125, TFT_YELLOW);
  tft.fillCircle(xpos, 120, 118, TFT_BLUE); 
  tft.fillCircle(xpos, 120, 110, TFT_BLACK); 
  for (int a=95; a<104; a++){
  tft.drawCircle(xpos, 120, a, TFT_WHITE);}  

 
  for (int i = 0; i < 360; i += 30) {
    sx = cos((i - 90) * 0.0174532925);
    sy = sin((i - 90) * 0.0174532925);
    x0 = sx * 114 + xpos;
    yy0 = sy * 114 + 120;
    x1 = sx * 100 + xpos;
    yy1 = sy * 100 + 120;
    tft.drawLine(x0, yy0, x1, yy1, TFT_YELLOW);
  }
  for (int i = 0; i < 360; i += 6) {
    sx = cos((i - 90) * 0.0174532925);
    sy = sin((i - 90) * 0.0174532925);
    x0 = sx * 102 + xpos;
    yy0 = sy * 102 + 120;
    x00 = sx * 92 + xpos;
    yy00 = sy * 92 + 120;
    // Draw minute markers
    tft.drawPixel(x0, yy0, TFT_GREEN); 
    tft.drawLine(x0, yy0, x00, yy00, TFT_BLACK);
    tft.drawLine(x0+1, yy0+1, x00+1, yy00+1, TFT_BLACK);
    // Draw main quadrant dots
    if (i == 0 || i == 180) tft.fillCircle(x0, yy0, 2, TFT_WHITE); 
    if (i == 90 || i == 270) tft.fillCircle(x0, yy0, 2, TFT_WHITE); 
  }
  tft.fillCircle(xpos, 121, 3, TFT_WHITE);
  targetTime = millis() + 1000;
  //touch event
  tft.setCursor (80, 250);
  tft.setTextSize (1);
  tft.setTextColor(TFT_BLACK);
  tft.println("Touch to proceed");
  waitOneTouch(); // Wait touch
  drawHome(); //buttons screeen
}

void loop(){
  if (currentpage == 0){
  if (targetTime < millis()) {             //clock code
    targetTime = millis() + 1000;
    ss++;              
    if (ss == 60) {
      ss = 0;
      mm++;            
      if (mm > 59) {
        mm = 0;
        hh++;          
        if (hh > 23) {
          hh = 0;
        }
      }
    }
    sdeg = ss * 6;                     // 0-59 -> 0-354
    mdeg = mm * 6 + sdeg * 0.01666667; // 0-59 -> 0-360 - includes seconds, but these increments are not used
    hdeg = hh * 30 + mdeg * 0.0833333; // 0-11 -> 0-360 - includes minutes and seconds, but these increments are not used
    hx = cos((hdeg - 90) * 0.0174532925);
    hy = sin((hdeg - 90) * 0.0174532925);
    mx = cos((mdeg - 90) * 0.0174532925);
    my = sin((mdeg - 90) * 0.0174532925);
    sx = cos((sdeg - 90) * 0.0174532925);
    sy = sin((sdeg - 90) * 0.0174532925);
    if (ss == 0 || initial) {
      initial = 0;
      // Erase hour and minute hand positions every minute
      tft.drawLine(ohx, ohy, xpos, 121, TFT_BLACK);
      ohx = hx * 62 + xpos + 1;
      ohy = hy * 62 + 121;
      tft.drawLine(omx, omy, xpos, 121, TFT_BLACK);
      omx = mx * 84 + xpos;
      omy = my * 84 + 121;
    }
    // Redraw new hand positions, hour and minute hands not erased here to avoid flicker
    tft.drawLine(osx, osy, xpos, 121, TFT_BLACK);
    osx = sx * 90 + xpos + 1;
    osy = sy * 90 + 121;
    tft.drawLine(osx, osy, xpos, 121, TFT_RED);
    tft.drawLine(ohx, ohy, xpos, 121, TFT_CYAN);
    tft.drawLine(omx, omy, xpos, 121, TFT_WHITE);
    tft.drawLine(osx, osy, xpos, 121, TFT_RED);
    tft.fillCircle(xpos, 121, 3, TFT_RED);
    tft.setCursor(xpos-30, 50);
    tft.setTextSize(2);
    tft.print("Glucoxy");
 
  // Draw MINI clock face "SECOND"
  tft.drawCircle(xpos, 155, 20, TFT_YELLOW);
  tft.drawCircle(xpos, 155, 18, TFT_BLUE);
  tft.drawCircle(xpos, 155, 17, TFT_CYAN);
  tft.drawCircle(xpos, 155, 16, TFT_CYAN);
  tft.fillRect(xpos-10, 149,22,15,TFT_BLACK); //erase
  if(ss<10){tft.setCursor(xpos-10, 149); tft.setTextSize(2);
  tft.print('0'); tft.setCursor(xpos+2, 149);}
  else{
  tft.setCursor(xpos-10, 149);}
  tft.setTextSize(2);
  tft.print(ss);
  // Draw MINI clock face "Minutes"
  tft.drawCircle(xpos+35, 117, 20, TFT_YELLOW);
  tft.drawCircle(xpos+35, 117, 18, TFT_BLUE);
  tft.drawCircle(xpos+35, 117, 17, TFT_CYAN);
  tft.drawCircle(xpos+35, 117, 16, TFT_CYAN);
 tft.fillRect(xpos+25, 111,22,15,TFT_BLACK); //erase
  if(mm<10){tft.setCursor(xpos+25, 111); tft.setTextSize(2);
  tft.print('0'); tft.setCursor(xpos+37, 111);}
  else{
  tft.setCursor(xpos+25, 111);}
  tft.println(mm);
  // Draw MINI clock face "Hour"
  tft.drawCircle(xpos-35, 117, 20, TFT_YELLOW);
  tft.drawCircle(xpos-35, 117, 18, TFT_BLUE);
  tft.drawCircle(xpos-35, 117, 17, TFT_CYAN);
  tft.drawCircle(xpos-35, 117, 16, TFT_CYAN);
  tft.fillRect(xpos-45, 111,22,15,TFT_BLACK); //erase
  if(hh<10){tft.setCursor(xpos-45, 111); tft.setTextSize(2);
  tft.print('0'); tft.setCursor(xpos-33, 111);}
  else{
  tft.setCursor(xpos-45, 111);}
  tft.setTextSize(2);
  tft.print(hh);
  //tft.setCursor(xpos-65, 111);
  //tft.println(':');
  if (hh>=0 && hh<12) d='A'; else {d='P';}
  tft.drawRoundRect(xpos-14,72,29,21,5,TFT_CYAN);
  tft.fillRect(xpos-11, 75,23,15,TFT_BLACK); //erase
  tft.setCursor(xpos-11, 75);
  tft.setTextSize(2);
  tft.print(d);
  tft.println('M');
  }
  }
  //home page
  if (currentpage == 1){
  digitalWrite(13, HIGH);
  TSPoint p = ts.getPoint();     // Read touchscreen
  digitalWrite(13, LOW);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  {if (p.z > 10 && p.z < 1000)
    {
      if (p.x > 736 && p.x < 855 && p.y > 255 && p.y < 725  && p.z > MINPRESSURE && p.z < MAXPRESSURE)
      {
        Serial.println("Pedometer");
        tft.fillRoundRect(60, 180, 200, 40, 8, TFT_WHITE);
        delay(70);
        tft.fillRoundRect(60, 180, 200, 40, 8, TFT_WHITE);
        tft.drawRoundRect(60, 180, 200, 40, 8, TFT_YELLOW);
        tft.setCursor(65, 195);
        tft.println("  Pedometer");
        delay(70);
        currentpage = 2;
        startPedo();
      }
      else if (p.x > 563 && p.x < 683 && p.y > 275 && p.y < 750)
      {
        Serial.println("Glucose Sensor");

        tft.fillRoundRect(60, 130, 200, 40, 8, TFT_WHITE);   //rgb led
        delay(70);
        tft.fillRoundRect(60, 130, 200, 40, 8, TFT_WHITE);   //rgb led
        tft.drawRoundRect(60, 130, 200, 40, 8, TFT_YELLOW);   //rgb led
        tft.setCursor(105, 145);
        tft.print("Glucose Sensor");
        delay(70);
        currentpage = 3;
        startGlu();
      }
      if (p.x > 403 && p.x < 525 && p.y > 271 && p.y < 725)
      {
        Serial.println("Pulse Oximeter");
        currentpage = 4;
        tft.fillRoundRect(60, 80, 200, 40, 8, TFT_WHITE);
        delay(70);
        tft.fillRoundRect(60, 80, 200, 40, 8, TFT_WHITE);
        tft.drawRoundRect(60, 80, 200, 40, 8, TFT_YELLOW);
        tft.setCursor(80, 95);
        tft.print("Pulse Oximeter");
        delay(70);
       // startPulse();

      }
    }
  }
  if (currentpage == 2) //pedo
  {
    senseBack();
    if (backsensed)
    {
      currentpage = 1;
      drawHome();
    }
    sensereset();
    calibrate();
  }
  if (currentpage == 3) // glucose
  {
     senseBack();
     if (backsensed)
     {
      currentpage = 0;
      drawHome();
     }
    sensereset();
  }

  if (currentpage == 4)  //pulse oxi
  {
    senseBack();
     if (backsensed)
     {
      currentpage = 1;
      drawHome();
     }
    sensereset();
    pinMode(pulseLED, OUTPUT);
  pinMode(readLED, OUTPUT);
  if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed  // Initialize sensor
  {
    Serial.println(F("MAX30105 was not found. Please check wiring/power."));
    while (1);
  }
  Serial.println(F("Attach sensor to finger with rubber band. Press any key to start conversion"));
  while (Serial.available() == 0) ; //wait until user presses a key
  Serial.read();
  byte ledBrightness = 60; //Options: 0=Off to 255=50mA
  byte sampleAverage = 4; //Options: 1, 2, 4, 8, 16, 32
  byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
  byte sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
  int pulseWidth = 411; //Options: 69, 118, 215, 411
  int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
  particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
  startPulse();
  }
}

}
// Wait one touch
TSPoint waitOneTouch() { 
  TSPoint p;
  do {
    p= ts.getPoint(); 
    pinMode(XM, OUTPUT); //Pins configures again for TFT control
    pinMode(YP, OUTPUT);
  } while((p.z < MINPRESSURE )|| (p.z > MAXPRESSURE));
  return p;
}
void sensereset()
{
  digitalWrite(13, HIGH);
  TSPoint p = ts.getPoint();     // Read touchscreen
  digitalWrite(13, LOW);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  if (p.x < 199 && p.y < 293 && !running)
    resetsensed = true;
  else
    resetsensed = false;
}
void startPedo()        //-------------------pedometer function--------------------------
{
  int acc = 0;
  float totvect[100] = {0};
  float totave[100] = {0};
  float xaccl[100] = {0};
  float yaccl[100] = {0};
  float zaccl[100] = {0};
  for (int a = 0; a < 100; a++)
  {
    xaccl[a] = float(analogRead(xpin) - 345);
    delay(1);
    yaccl[a] = float(analogRead(ypin) - 346);
    delay(1);
    zaccl[a] = float(analogRead(zpin) - 416);
    delay(1);
    totvect[a] = sqrt(((xaccl[a] - xavg) * (xaccl[a] - xavg)) + ((yaccl[a] - yavg) * (yaccl[a] - yavg)) + ((zval[a] - zavg) * (zval[a] - zavg)));
    totave[a] = (totvect[a] + totvect[a - 1]) / 2 ;
    Serial.println("totave[a]");
    Serial.println(totave[a]);
    delay(100);
    if (totave[a] > threshold && flag == 0)
    {
      steps = steps + 1;
      flag = 1;
    }
    else if (totave[a] > threshold && flag == 1)
    {
      // Don't Count
    }
    if (totave[a] < threshold   && flag == 1)
    {
      flag = 0;
    }
    if (steps < 0) {
      steps = 0;
    }
    Serial.println('\n');
    Serial.print("steps: ");
    Serial.println(steps);
    delay(1000);
  }
  delay(1000);
  //print results on lcd
  tft.setCursor(130, 145);
  tft.setTextSize(3);
  tft.setTextColor(TFT_WHITE);
  tft.print(steps);

  }
void calibrate()            //<----------------------calibrate funtion--------------------->
{
  float sum = 0;
  float sum1 = 0;
  float sum2 = 0;
  for (int i = 0; i < 100; i++) {
    xval[i] = float(analogRead(xpin) - 345);
    sum = xval[i] + sum;
  }
  delay(100);
  xavg = sum / 100.0;
  Serial.println(xavg);
  for (int j = 0; j < 100; j++)
  {
    yval[j] = float(analogRead(ypin) - 346);
    sum1 = yval[j] + sum1;
  }
  yavg = sum1 / 100.0;
  Serial.println(yavg);
  delay(100);
  for (int q = 0; q < 100; q++)
  {
    zval[q] = float(analogRead(zpin) - 416);
    sum2 = zval[q] + sum2;
  }
  zavg = sum2 / 100.0;
  delay(100);
  Serial.println(zavg);
}
void senseBack()
{
  digitalWrite(13, HIGH);
  TSPoint p = ts.getPoint();     // Read touchscreen
  digitalWrite(13, LOW);

  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  if (p.y < 305 && p.x < 285 && p.x > 239 && !running)
  {
    backsensed = true;

  }
  else
    backsensed = false;
}
void startGlu()                 //-------------------------------startGlu-------------------------
{
  }
void startPulse()             //--------------------------------startpulse-----------------------
{
  bufferLength = 100; //buffer length of 100 stores 4 seconds of samples running at 25sps

  //read the first 100 samples, and determine the signal range
  for (byte i = 0 ; i < bufferLength ; i++)
  {
    while (particleSensor.available() == false) //do we have new data?
      particleSensor.check(); //Check the sensor for new data
    redBuffer[i] = particleSensor.getRed();
    irBuffer[i] = particleSensor.getIR();
    particleSensor.nextSample(); //We're finished with this sample so move to next sample
    Serial.print(F("red="));
    Serial.print(redBuffer[i], DEC);
    Serial.print(F(", ir="));
    Serial.println(irBuffer[i], DEC);
  }
  //calculate heart rate and SpO2 after first 100 samples (first 4 seconds of samples)
  maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
  //Continuously taking samples from MAX30102.  Heart rate and SpO2 are calculated every 1 second
  while (1)
  {
    //dumping the first 25 sets of samples in the memory and shift the last 75 sets of samples to the top
    for (byte i = 25; i < 100; i++)
    {
      redBuffer[i - 25] = redBuffer[i];
      irBuffer[i - 25] = irBuffer[i];
    }
    //take 25 sets of samples before calculating the heart rate.
    for (byte i = 75; i < 100; i++)
    {
      while (particleSensor.available() == false) //do we have new data?
        particleSensor.check(); //Check the sensor for new data
      digitalWrite(readLED, !digitalRead(readLED)); //Blink onboard LED with every data read
      redBuffer[i] = particleSensor.getRed();
      irBuffer[i] = particleSensor.getIR();
      particleSensor.nextSample(); //We're finished with this sample so move to next sample
      //send samples and calculation result to terminal program through UART
      Serial.print(F("red="));
      Serial.print(redBuffer[i], DEC);
      Serial.print(F(", ir="));
      Serial.print(irBuffer[i], DEC);
      Serial.print(F(", HR="));
      Serial.print(heartRate, DEC);
      Serial.print(F(", HRvalid="));
      Serial.print(validHeartRate, DEC);
      Serial.print(F(", SPO2="));
      Serial.print(spo2, DEC);
      Serial.print(F(", SPO2Valid="));
      Serial.println(validSPO2, DEC);
    }

    //After gathering 25 new samples recalculate HR and SP02
    maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
    //print results on lcd
    tft.setCursor(130,145);
  tft.setTextSize(3);
  tft.setTextColor(TFT_WHITE);
  tft.print(heartRate);

  }
    
}

Please see above code whether it works perfect as i want. I am getting error when i try to compile this code which is given below

My questions are:

whether this code is right, if not then point out my mistakes.
Solution of error shown in image.


user68788: The Arduino Uno doesn't have enough SRAM, the current Rev3 has just 2 KB. You can circumvent that by using less variables, use #define xpin instead of const int, etc.
Here is an article about how to reduce memory usage, give it a try.
Another option is to use a different Microcontroller with more SRAM, with at least 3KB like a Arduino Mega, ESP32. But first try to optimize your program, if you don't have another unused board lying around anyway.
EDIT:
You might also take a look at the header files if you can optimize anything there.

user31668: Arduino Mega has 8192 bytes of SRAM which will solve your problem.
Reduce the RAM usage (ref)


73183: <p>In code attached below I am using tftlcd display of 2.4 inch in which i want to show clock as home page, when user click on tftlcd screen then a screen appears which can show three options to user which are about the sensors user want to use. There are three sensors in this code: Pulse oximeter, glucose meter, pedometer. After choosing some option(sensor) the data of sensor will appear on screen.
I have write code for all this procedure which is given below:</p>
<pre><code>//include all libraries
#include &lt;Wire.h&gt;
#include &lt;SPFD5408_Adafruit_GFX.h&gt;
#include &lt;SPFD5408_Adafruit_TFTLCD.h&gt;
#include &lt;SPFD5408_TouchScreen.h&gt;
#include &quot;MAX30105.h&quot;
#include &quot;spo2_algorithm.h&quot;

//Exit buttons
bool backsensed = false;
bool resetsensed = false;

//Include Max30102 lib
MAX30105 particleSensor;

//Define LCD pins
#define YP A1  // must be an analog pin, use &quot;An&quot; notation!
#define XM A2  // must be an analog pin, use &quot;An&quot; notation!
#define YM 7   // can be a digital pin
#define XP 6   // can be a digital pin

//pedometer
const int xpin = A3;
const int ypin = A4;
const int zpin = A5;
byte p[8] = {0x1F,
  0x1F,
  0x1F,
  0x1F,
  0x1F,
  0x1F,
  0x1F,
  0x1F
};
float threshold = 100;
float xval[100] = {0};
float yval[100] = {0};
float zval[100] = {0};
float xavg, yavg, zavg;
int steps, flag = 0;
//pulseoxi
#define MAX_BRIGHTNESS 255
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
uint16_t irBuffer[100]; //infrared LED sensor data
uint16_t redBuffer[100];  //red LED sensor data
#else
uint32_t irBuffer[100]; //infrared LED sensor data
uint32_t redBuffer[100];  //red LED sensor data
#endif
int32_t bufferLength; //data length
int32_t spo2; //SPO2 value
int8_t validSPO2; //indicator to show if the SPO2 calculation is valid
int32_t heartRate; //heart rate value
int8_t validHeartRate; //indicator to show if the heart rate calculation is valid
byte pulseLED = 11; //Must be on PWM pin
byte readLED = 13; //Blinks with each data read

// LCD Calibrate values
#define TS_MINX 100
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

// For better pressure precision, we need to know the resistance between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

int BLUE = tft.color565(50, 50, 255);
#define TFT_BLACK   0x0000
#define TFT_DARKBLUE 0x0010
#define VIOLET 0x8888
#define TFT_RED     0xF800
#define TFT_GREEN   0x07E0
#define TFT_CYAN    0x07FF
#define TFT_MAGENTA 0xF81F
#define TFT_YELLOW  0xFFE0
#define TFT_WHITE   0xFFFF
#define TFT_GREY    0x5AEB
#define TFT_BLUE    0x001F
#define GOLD 0xFEA0
#define BROWN 0xA145
#define SILVER 0xC618
#define LIME 0x07E0
#define BLUE  0x001F

//button screen
int currentpcolour;
int currentpage;
bool running;

//Minimum and maximum pressure to sense the touch
#define MINPRESSURE 10
#define MAXPRESSURE 1000

//clock
float sx = 0, sy = 1, mx = 1, my = 0, hx = -1, hy = 0;    
float sdeg = 0, mdeg = 0, hdeg = 0;
uint16_t osx = 120, osy = 120, omx = 120, omy = 120, ohx = 120, ohy = 120; 
int16_t x0 = 0, x1 = 0, yy0 = 0, yy1 = 0, x00 = 0, yy00 = 0;
uint32_t targetTime = 0;                    
uint16_t xpos; 
uint8_t conv2d(const char* p) {
  uint8_t v = 0;
  if ('0' &lt;= *p &amp;&amp; *p &lt;= '9')
    v = *p - '0';
  return 10 * v + *++p - '0';
}
uint8_t hh = conv2d(__TIME__), mm = conv2d(__TIME__ + 3), ss = conv2d(__TIME__ + 6); 
boolean initial = 1;
char d;

//for buttons
void drawHome()
{
  tft.fillScreen(TFT_BLACK);
  tft.drawRoundRect(0, 0, 319, 240, 8, TFT_YELLOW);     //Page border

  tft.fillRoundRect(60, 180, 200, 40, 8, TFT_DARKBLUE);
  tft.drawRoundRect(60, 180, 200, 40, 8, TFT_YELLOW);  //PEDO

  tft.fillRoundRect(60, 130, 200, 40, 8, TFT_DARKBLUE);   //Glu
  tft.drawRoundRect(60, 130, 200, 40, 8, TFT_YELLOW);

  tft.fillRoundRect(60, 80, 200, 40, 8, TFT_DARKBLUE);
  tft.drawRoundRect(60, 80, 200, 40, 8, TFT_YELLOW); //Pulse oxi and heart rate

  tft.setCursor(25, 20);
  tft.setTextSize(3);
  tft.setTextColor(TFT_WHITE);
  tft.print(&quot;Welcome Here..&quot;);
  tft.setCursor(25, 50);
  tft.setTextSize(2);
  tft.setTextColor(TFT_YELLOW);
  tft.print(&quot;Choose Your Sensor&quot;);
  tft.setTextColor(TFT_WHITE);
  tft.setCursor(110, 195);
  tft.print(&quot;Pedometer&quot;);

  tft.setCursor(80, 145);
  tft.print(&quot;Glucose Sensor&quot;);

  tft.setCursor(80, 95);
  tft.print(&quot;Pulse Oximeter&quot;);
  //  delay(500);

  }

  void setup()
{
  tft.reset();  
  tft.begin(0x9341); 
  tft.setRotation(1);
  currentpage = 0; //clock page
  tft.setTextColor(TFT_WHITE);// text color
  tft.fillScreen(TFT_BLACK);// background color

  xpos = tft.width() / 2; 
  tft.drawCircle(xpos, 120, 125, TFT_YELLOW);
  tft.fillCircle(xpos, 120, 118, TFT_BLUE); 
  tft.fillCircle(xpos, 120, 110, TFT_BLACK); 
  for (int a=95; a&lt;104; a++){
  tft.drawCircle(xpos, 120, a, TFT_WHITE);}  

 
  for (int i = 0; i &lt; 360; i += 30) {
    sx = cos((i - 90) * 0.0174532925);
    sy = sin((i - 90) * 0.0174532925);
    x0 = sx * 114 + xpos;
    yy0 = sy * 114 + 120;
    x1 = sx * 100 + xpos;
    yy1 = sy * 100 + 120;
    tft.drawLine(x0, yy0, x1, yy1, TFT_YELLOW);
  }
  for (int i = 0; i &lt; 360; i += 6) {
    sx = cos((i - 90) * 0.0174532925);
    sy = sin((i - 90) * 0.0174532925);
    x0 = sx * 102 + xpos;
    yy0 = sy * 102 + 120;
    x00 = sx * 92 + xpos;
    yy00 = sy * 92 + 120;
    // Draw minute markers
    tft.drawPixel(x0, yy0, TFT_GREEN); 
    tft.drawLine(x0, yy0, x00, yy00, TFT_BLACK);
    tft.drawLine(x0+1, yy0+1, x00+1, yy00+1, TFT_BLACK);
    // Draw main quadrant dots
    if (i == 0 || i == 180) tft.fillCircle(x0, yy0, 2, TFT_WHITE); 
    if (i == 90 || i == 270) tft.fillCircle(x0, yy0, 2, TFT_WHITE); 
  }
  tft.fillCircle(xpos, 121, 3, TFT_WHITE);
  targetTime = millis() + 1000;
  //touch event
  tft.setCursor (80, 250);
  tft.setTextSize (1);
  tft.setTextColor(TFT_BLACK);
  tft.println(&quot;Touch to proceed&quot;);
  waitOneTouch(); // Wait touch
  drawHome(); //buttons screeen
}

void loop(){
  if (currentpage == 0){
  if (targetTime &lt; millis()) {             //clock code
    targetTime = millis() + 1000;
    ss++;              
    if (ss == 60) {
      ss = 0;
      mm++;            
      if (mm &gt; 59) {
        mm = 0;
        hh++;          
        if (hh &gt; 23) {
          hh = 0;
        }
      }
    }
    sdeg = ss * 6;                     // 0-59 -&gt; 0-354
    mdeg = mm * 6 + sdeg * 0.01666667; // 0-59 -&gt; 0-360 - includes seconds, but these increments are not used
    hdeg = hh * 30 + mdeg * 0.0833333; // 0-11 -&gt; 0-360 - includes minutes and seconds, but these increments are not used
    hx = cos((hdeg - 90) * 0.0174532925);
    hy = sin((hdeg - 90) * 0.0174532925);
    mx = cos((mdeg - 90) * 0.0174532925);
    my = sin((mdeg - 90) * 0.0174532925);
    sx = cos((sdeg - 90) * 0.0174532925);
    sy = sin((sdeg - 90) * 0.0174532925);
    if (ss == 0 || initial) {
      initial = 0;
      // Erase hour and minute hand positions every minute
      tft.drawLine(ohx, ohy, xpos, 121, TFT_BLACK);
      ohx = hx * 62 + xpos + 1;
      ohy = hy * 62 + 121;
      tft.drawLine(omx, omy, xpos, 121, TFT_BLACK);
      omx = mx * 84 + xpos;
      omy = my * 84 + 121;
    }
    // Redraw new hand positions, hour and minute hands not erased here to avoid flicker
    tft.drawLine(osx, osy, xpos, 121, TFT_BLACK);
    osx = sx * 90 + xpos + 1;
    osy = sy * 90 + 121;
    tft.drawLine(osx, osy, xpos, 121, TFT_RED);
    tft.drawLine(ohx, ohy, xpos, 121, TFT_CYAN);
    tft.drawLine(omx, omy, xpos, 121, TFT_WHITE);
    tft.drawLine(osx, osy, xpos, 121, TFT_RED);
    tft.fillCircle(xpos, 121, 3, TFT_RED);
    tft.setCursor(xpos-30, 50);
    tft.setTextSize(2);
    tft.print(&quot;Glucoxy&quot;);
 
  // Draw MINI clock face &quot;SECOND&quot;
  tft.drawCircle(xpos, 155, 20, TFT_YELLOW);
  tft.drawCircle(xpos, 155, 18, TFT_BLUE);
  tft.drawCircle(xpos, 155, 17, TFT_CYAN);
  tft.drawCircle(xpos, 155, 16, TFT_CYAN);
  tft.fillRect(xpos-10, 149,22,15,TFT_BLACK); //erase
  if(ss&lt;10){tft.setCursor(xpos-10, 149); tft.setTextSize(2);
  tft.print('0'); tft.setCursor(xpos+2, 149);}
  else{
  tft.setCursor(xpos-10, 149);}
  tft.setTextSize(2);
  tft.print(ss);
  // Draw MINI clock face &quot;Minutes&quot;
  tft.drawCircle(xpos+35, 117, 20, TFT_YELLOW);
  tft.drawCircle(xpos+35, 117, 18, TFT_BLUE);
  tft.drawCircle(xpos+35, 117, 17, TFT_CYAN);
  tft.drawCircle(xpos+35, 117, 16, TFT_CYAN);
 tft.fillRect(xpos+25, 111,22,15,TFT_BLACK); //erase
  if(mm&lt;10){tft.setCursor(xpos+25, 111); tft.setTextSize(2);
  tft.print('0'); tft.setCursor(xpos+37, 111);}
  else{
  tft.setCursor(xpos+25, 111);}
  tft.println(mm);
  // Draw MINI clock face &quot;Hour&quot;
  tft.drawCircle(xpos-35, 117, 20, TFT_YELLOW);
  tft.drawCircle(xpos-35, 117, 18, TFT_BLUE);
  tft.drawCircle(xpos-35, 117, 17, TFT_CYAN);
  tft.drawCircle(xpos-35, 117, 16, TFT_CYAN);
  tft.fillRect(xpos-45, 111,22,15,TFT_BLACK); //erase
  if(hh&lt;10){tft.setCursor(xpos-45, 111); tft.setTextSize(2);
  tft.print('0'); tft.setCursor(xpos-33, 111);}
  else{
  tft.setCursor(xpos-45, 111);}
  tft.setTextSize(2);
  tft.print(hh);
  //tft.setCursor(xpos-65, 111);
  //tft.println(':');
  if (hh&gt;=0 &amp;&amp; hh&lt;12) d='A'; else {d='P';}
  tft.drawRoundRect(xpos-14,72,29,21,5,TFT_CYAN);
  tft.fillRect(xpos-11, 75,23,15,TFT_BLACK); //erase
  tft.setCursor(xpos-11, 75);
  tft.setTextSize(2);
  tft.print(d);
  tft.println('M');
  }
  }
  //home page
  if (currentpage == 1){
  digitalWrite(13, HIGH);
  TSPoint p = ts.getPoint();     // Read touchscreen
  digitalWrite(13, LOW);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  {if (p.z &gt; 10 &amp;&amp; p.z &lt; 1000)
    {
      if (p.x &gt; 736 &amp;&amp; p.x &lt; 855 &amp;&amp; p.y &gt; 255 &amp;&amp; p.y &lt; 725  &amp;&amp; p.z &gt; MINPRESSURE &amp;&amp; p.z &lt; MAXPRESSURE)
      {
        Serial.println(&quot;Pedometer&quot;);
        tft.fillRoundRect(60, 180, 200, 40, 8, TFT_WHITE);
        delay(70);
        tft.fillRoundRect(60, 180, 200, 40, 8, TFT_WHITE);
        tft.drawRoundRect(60, 180, 200, 40, 8, TFT_YELLOW);
        tft.setCursor(65, 195);
        tft.println(&quot;  Pedometer&quot;);
        delay(70);
        currentpage = 2;
        startPedo();
      }
      else if (p.x &gt; 563 &amp;&amp; p.x &lt; 683 &amp;&amp; p.y &gt; 275 &amp;&amp; p.y &lt; 750)
      {
        Serial.println(&quot;Glucose Sensor&quot;);

        tft.fillRoundRect(60, 130, 200, 40, 8, TFT_WHITE);   //rgb led
        delay(70);
        tft.fillRoundRect(60, 130, 200, 40, 8, TFT_WHITE);   //rgb led
        tft.drawRoundRect(60, 130, 200, 40, 8, TFT_YELLOW);   //rgb led
        tft.setCursor(105, 145);
        tft.print(&quot;Glucose Sensor&quot;);
        delay(70);
        currentpage = 3;
        startGlu();
      }
      if (p.x &gt; 403 &amp;&amp; p.x &lt; 525 &amp;&amp; p.y &gt; 271 &amp;&amp; p.y &lt; 725)
      {
        Serial.println(&quot;Pulse Oximeter&quot;);
        currentpage = 4;
        tft.fillRoundRect(60, 80, 200, 40, 8, TFT_WHITE);
        delay(70);
        tft.fillRoundRect(60, 80, 200, 40, 8, TFT_WHITE);
        tft.drawRoundRect(60, 80, 200, 40, 8, TFT_YELLOW);
        tft.setCursor(80, 95);
        tft.print(&quot;Pulse Oximeter&quot;);
        delay(70);
       // startPulse();

      }
    }
  }
  if (currentpage == 2) //pedo
  {
    senseBack();
    if (backsensed)
    {
      currentpage = 1;
      drawHome();
    }
    sensereset();
    calibrate();
  }
  if (currentpage == 3) // glucose
  {
     senseBack();
     if (backsensed)
     {
      currentpage = 0;
      drawHome();
     }
    sensereset();
  }

  if (currentpage == 4)  //pulse oxi
  {
    senseBack();
     if (backsensed)
     {
      currentpage = 1;
      drawHome();
     }
    sensereset();
    pinMode(pulseLED, OUTPUT);
  pinMode(readLED, OUTPUT);
  if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed  // Initialize sensor
  {
    Serial.println(F(&quot;MAX30105 was not found. Please check wiring/power.&quot;));
    while (1);
  }
  Serial.println(F(&quot;Attach sensor to finger with rubber band. Press any key to start conversion&quot;));
  while (Serial.available() == 0) ; //wait until user presses a key
  Serial.read();
  byte ledBrightness = 60; //Options: 0=Off to 255=50mA
  byte sampleAverage = 4; //Options: 1, 2, 4, 8, 16, 32
  byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
  byte sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
  int pulseWidth = 411; //Options: 69, 118, 215, 411
  int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
  particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
  startPulse();
  }
}

}
// Wait one touch
TSPoint waitOneTouch() { 
  TSPoint p;
  do {
    p= ts.getPoint(); 
    pinMode(XM, OUTPUT); //Pins configures again for TFT control
    pinMode(YP, OUTPUT);
  } while((p.z &lt; MINPRESSURE )|| (p.z &gt; MAXPRESSURE));
  return p;
}
void sensereset()
{
  digitalWrite(13, HIGH);
  TSPoint p = ts.getPoint();     // Read touchscreen
  digitalWrite(13, LOW);
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  if (p.x &lt; 199 &amp;&amp; p.y &lt; 293 &amp;&amp; !running)
    resetsensed = true;
  else
    resetsensed = false;
}
void startPedo()        //-------------------pedometer function--------------------------
{
  int acc = 0;
  float totvect[100] = {0};
  float totave[100] = {0};
  float xaccl[100] = {0};
  float yaccl[100] = {0};
  float zaccl[100] = {0};
  for (int a = 0; a &lt; 100; a++)
  {
    xaccl[a] = float(analogRead(xpin) - 345);
    delay(1);
    yaccl[a] = float(analogRead(ypin) - 346);
    delay(1);
    zaccl[a] = float(analogRead(zpin) - 416);
    delay(1);
    totvect[a] = sqrt(((xaccl[a] - xavg) * (xaccl[a] - xavg)) + ((yaccl[a] - yavg) * (yaccl[a] - yavg)) + ((zval[a] - zavg) * (zval[a] - zavg)));
    totave[a] = (totvect[a] + totvect[a - 1]) / 2 ;
    Serial.println(&quot;totave[a]&quot;);
    Serial.println(totave[a]);
    delay(100);
    if (totave[a] &gt; threshold &amp;&amp; flag == 0)
    {
      steps = steps + 1;
      flag = 1;
    }
    else if (totave[a] &gt; threshold &amp;&amp; flag == 1)
    {
      // Don't Count
    }
    if (totave[a] &lt; threshold   &amp;&amp; flag == 1)
    {
      flag = 0;
    }
    if (steps &lt; 0) {
      steps = 0;
    }
    Serial.println('\n');
    Serial.print(&quot;steps: &quot;);
    Serial.println(steps);
    delay(1000);
  }
  delay(1000);
  //print results on lcd
  tft.setCursor(130, 145);
  tft.setTextSize(3);
  tft.setTextColor(TFT_WHITE);
  tft.print(steps);

  }
void calibrate()            //&lt;----------------------calibrate funtion---------------------&gt;
{
  float sum = 0;
  float sum1 = 0;
  float sum2 = 0;
  for (int i = 0; i &lt; 100; i++) {
    xval[i] = float(analogRead(xpin) - 345);
    sum = xval[i] + sum;
  }
  delay(100);
  xavg = sum / 100.0;
  Serial.println(xavg);
  for (int j = 0; j &lt; 100; j++)
  {
    yval[j] = float(analogRead(ypin) - 346);
    sum1 = yval[j] + sum1;
  }
  yavg = sum1 / 100.0;
  Serial.println(yavg);
  delay(100);
  for (int q = 0; q &lt; 100; q++)
  {
    zval[q] = float(analogRead(zpin) - 416);
    sum2 = zval[q] + sum2;
  }
  zavg = sum2 / 100.0;
  delay(100);
  Serial.println(zavg);
}
void senseBack()
{
  digitalWrite(13, HIGH);
  TSPoint p = ts.getPoint();     // Read touchscreen
  digitalWrite(13, LOW);

  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  if (p.y &lt; 305 &amp;&amp; p.x &lt; 285 &amp;&amp; p.x &gt; 239 &amp;&amp; !running)
  {
    backsensed = true;

  }
  else
    backsensed = false;
}
void startGlu()                 //-------------------------------startGlu-------------------------
{
  }
void startPulse()             //--------------------------------startpulse-----------------------
{
  bufferLength = 100; //buffer length of 100 stores 4 seconds of samples running at 25sps

  //read the first 100 samples, and determine the signal range
  for (byte i = 0 ; i &lt; bufferLength ; i++)
  {
    while (particleSensor.available() == false) //do we have new data?
      particleSensor.check(); //Check the sensor for new data
    redBuffer[i] = particleSensor.getRed();
    irBuffer[i] = particleSensor.getIR();
    particleSensor.nextSample(); //We're finished with this sample so move to next sample
    Serial.print(F(&quot;red=&quot;));
    Serial.print(redBuffer[i], DEC);
    Serial.print(F(&quot;, ir=&quot;));
    Serial.println(irBuffer[i], DEC);
  }
  //calculate heart rate and SpO2 after first 100 samples (first 4 seconds of samples)
  maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &amp;spo2, &amp;validSPO2, &amp;heartRate, &amp;validHeartRate);
  //Continuously taking samples from MAX30102.  Heart rate and SpO2 are calculated every 1 second
  while (1)
  {
    //dumping the first 25 sets of samples in the memory and shift the last 75 sets of samples to the top
    for (byte i = 25; i &lt; 100; i++)
    {
      redBuffer[i - 25] = redBuffer[i];
      irBuffer[i - 25] = irBuffer[i];
    }
    //take 25 sets of samples before calculating the heart rate.
    for (byte i = 75; i &lt; 100; i++)
    {
      while (particleSensor.available() == false) //do we have new data?
        particleSensor.check(); //Check the sensor for new data
      digitalWrite(readLED, !digitalRead(readLED)); //Blink onboard LED with every data read
      redBuffer[i] = particleSensor.getRed();
      irBuffer[i] = particleSensor.getIR();
      particleSensor.nextSample(); //We're finished with this sample so move to next sample
      //send samples and calculation result to terminal program through UART
      Serial.print(F(&quot;red=&quot;));
      Serial.print(redBuffer[i], DEC);
      Serial.print(F(&quot;, ir=&quot;));
      Serial.print(irBuffer[i], DEC);
      Serial.print(F(&quot;, HR=&quot;));
      Serial.print(heartRate, DEC);
      Serial.print(F(&quot;, HRvalid=&quot;));
      Serial.print(validHeartRate, DEC);
      Serial.print(F(&quot;, SPO2=&quot;));
      Serial.print(spo2, DEC);
      Serial.print(F(&quot;, SPO2Valid=&quot;));
      Serial.println(validSPO2, DEC);
    }

    //After gathering 25 new samples recalculate HR and SP02
    maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &amp;spo2, &amp;validSPO2, &amp;heartRate, &amp;validHeartRate);
    //print results on lcd
    tft.setCursor(130,145);
  tft.setTextSize(3);
  tft.setTextColor(TFT_WHITE);
  tft.print(heartRate);

  }
    
}
</code></pre>
<p>Please see above code whether it works perfect as i want. I am getting error when i try to compile this code which is given below
<a href="https://i.stack.imgur.com/QKE4X.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QKE4X.png" alt="enter image description here" /></a></p>
<p>My questions are:</p>
<ol>
<li>whether this code is right, if not then point out my mistakes.</li>
<li>Solution of error shown in image.</li>
</ol>
 68788: <p>The Arduino Uno doesn't have enough SRAM, the current Rev3 has just 2 KB. You can circumvent that by using less variables, use <code>#define xpin</code> instead of <code>const int</code>, etc.
<a href="https://create.arduino.cc/projecthub/john-bradnam/reducing-your-memory-usage-26ca05" rel="nofollow noreferrer">Here</a> is an article about how to reduce memory usage, give it a try.<br>
Another option is to use a different Microcontroller with more SRAM, with at least 3KB like a Arduino Mega, ESP32. But first try to optimize your program, if you don't have another unused board lying around anyway.
<br><strong>EDIT:</strong><br>
You might also take a look at the header files if you can optimize anything there.</p>
 31668: <p>Arduino Mega has <strong>8192 bytes</strong> of SRAM which will solve your problem.</p>
<p>Reduce the RAM usage (<a href="https://arduinojson.org/v6/how-to/reduce-memory-usage/" rel="nofollow noreferrer">ref</a>)</p>
<p><a href="https://i.stack.imgur.com/4Pn8B.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4Pn8B.png" alt="enter image description here" /></a></p>
