IF statement with RETURN: HELP!

I am writing some code for my serial VT100 TFT display (Byvac4629).

Now i have added some code for the buttons and the serial output of the button variables, but it doesn't work and i really don't know what to do anymore.

All i know is that it has something to do with the "return;" command being in the wrong place. Can anyone tell me what i do wrong?
Here is the sub-function code.

void buttoncheck() {
  char buf[BUFSZ];
  int char_number = 0;
  buttonState = analogRead(buttonPin);
  if (buttonState > 975){ // UP
    scaling = scaling + 1;
    Serial.write("\e[?10L"); //set landscape mode
    delay(2);
    snprintf(buf, BUFSZ,"\e{15 225pZOOM= %dx", scaling*10);
    Serial.write(buf);
    delay(750);
    Serial.write("\e[0;0;0f\e{2 225 254 319F");
    Serial.write("\e[?10L"); //set landscape mode
  }else {
  if ((buttonState > 900)&&(buttonState < 950)){ // LEFT
    sample_delay=sample_delay+1;
    Serial.write("\e[?10L"); //set landscape mode
    delay(2);
    snprintf(buf, BUFSZ,"\e{15 225pSAMPLE-TIME= %dms", sample_delay);
    Serial.write(buf);
    delay(750);
    Serial.write("\e[0;0;0f\e{2 225 254 319F");
    Serial.write("\e[?10P"); //set landscape mode
  }else{
   
  //else{
  if ((buttonState > 820)&&(buttonState < 870)){ // DOWN
    scaling = scaling - 1;
    Serial.write("\e[?10L"); //set landscape mode
    delay(2);
    snprintf(buf, BUFSZ,"\e{15 225pZOOM= %dx", scaling*10);
    Serial.write(buf);
    delay(750);
    Serial.write("\e[0;0;0f\e{2 225 254 319F");
    Serial.write("\e[?10P"); //set landscape mode
    if (scaling < 0){
      scaling = 0;  
      Serial.write("\e[?10L"); //set landscape mode
      delay(20);
      Serial.write("\e{15 225pZOOM SET TO HIGHEST!");
      delay(1500);
      Serial.write("\e[0;0;0f\e{2 225 254 319F");
      delay(500);
      Serial.write("\e[?10P"); //set landscape mode
      delay(2);
    }else{
      
   
  //else {
  if ((buttonState > 750)&&(buttonState < 790)){  // RIGHT
    sample_delay=sample_delay-1;
    Serial.write("\e[?10L"); //set landscape mode
    delay(2);
    snprintf(buf, BUFSZ,"\e{15 225pSAMPLE-TIME= %dms", sample_delay);
    Serial.write(buf);
    delay(750);
    Serial.print("\e[0;0;0f\e{2 225 254 319F");
    Serial.write("\e[?10P"); //set landscape mode
    if (sample_delay < 1){
      sample_delay = 0;
      //char_number = 26; // NUMBER OF CHARACTERS IN THE MESSAGE
      Serial.write("\e[?10L"); //set landscape mode
      delay(2);
      Serial.print("\e{15 225pSAMPLE-TIME SET TO FASTEST!");
      delay(1500);
      Serial.write("\e[0;0;0f\e{2 225 254 319F");
      delay(500);
      Serial.write("\e[?10P"); //set landscape mode
      delay(2);
    }return;
      }
    }
    }
    }
  }
}

All i know is that it has something to do with the "return;" command being in the wrong place.

Why do you believe that?

You haven't explained what the code is supposed to do. You haven't explained what the code actually does.

It's nothing to do with the return - you can delete it and the routine will function just the same.

What kind of button it this? A contact button? If so, are there multiple in series with resistors? A good clear discription of both your code and project will help us greatly.

Also where is your code?

OK, it doesn't work, but what does it do ?
Come to that, what should it do ?

The 'return;' shouldn't be a problem since the only thing after it is a bunch of close-brackets. After those close-brackets the function will return anyway.

I think your bracketing was a bit wonky. Try this instead:

void buttoncheck() {
  char buf[BUFSZ];
  int char_number = 0;
  buttonState = analogRead(buttonPin);
  if(buttonState > 975) { // UP
    scaling = scaling + 1;
    Serial.write("\e[?10L"); //set landscape mode
    delay(2);
    snprintf(buf, BUFSZ,"\e{15 225pZOOM= %dx", scaling*10);
    Serial.write(buf);
    delay(750);
    Serial.write("\e[0;0;0f\e{2 225 254 319F");
    Serial.write("\e[?10L"); //set landscape mode
  }
  else if(buttonState > 900) { // LEFT
    sample_delay=sample_delay+1;
    Serial.write("\e[?10L"); //set landscape mode
    delay(2);
    snprintf(buf, BUFSZ,"\e{15 225pSAMPLE-TIME= %dms", sample_delay);
    Serial.write(buf);
    delay(750);
    Serial.write("\e[0;0;0f\e{2 225 254 319F");
    Serial.write("\e[?10P"); //set landscape mode
  }
  else if(buttonState > 820) { // DOWN
    scaling = scaling - 1;
    Serial.write("\e[?10L"); //set landscape mode
    delay(2);
    snprintf(buf, BUFSZ,"\e{15 225pZOOM= %dx", scaling*10);
    Serial.write(buf);
    delay(750);
    Serial.write("\e[0;0;0f\e{2 225 254 319F");
    Serial.write("\e[?10P"); //set landscape mode
    if (scaling < 0) {
      scaling = 0;  
      Serial.write("\e[?10L"); //set landscape mode
      delay(20);
      Serial.write("\e{15 225pZOOM SET TO HIGHEST!");
      delay(1500);
      Serial.write("\e[0;0;0f\e{2 225 254 319F");
      delay(500);
      Serial.write("\e[?10P"); //set landscape mode
      delay(2);
    }
  }
  else if(buttonState > 750) {  // RIGHT
    sample_delay=sample_delay-1;
    Serial.write("\e[?10L"); //set landscape mode
    delay(2);
    snprintf(buf, BUFSZ,"\e{15 225pSAMPLE-TIME= %dms", sample_delay);
    Serial.write(buf);
    delay(750);
    Serial.print("\e[0;0;0f\e{2 225 254 319F");
    Serial.write("\e[?10P"); //set landscape mode
    if (sample_delay < 1){
      sample_delay = 0;
      //char_number = 26; // NUMBER OF CHARACTERS IN THE MESSAGE
      Serial.write("\e[?10L"); //set landscape mode
      delay(2);
      Serial.print("\e{15 225pSAMPLE-TIME SET TO FASTEST!");
      delay(1500);
      Serial.write("\e[0;0;0f\e{2 225 254 319F");
      delay(500);
      Serial.write("\e[?10P"); //set landscape mode
      delay(2);
    }
  }
}

i'm sorry for not being clear...

I want this code to check the analog buttonpin for a value (every button has a different value). Then when a certain button is pushed the coupled var needs to be updated + or - "1" and the updated value is send to the display. Then a delay for X seconds. Then the text is being removed by drawing a black bar over it.

as for what it accually does.... nothing!
The program crashes or gets stuck before anything is send to the display. It DOES goes through the setup though!

here is my complete sketch:

// Setup settings
#define DISP_WIDTH 220
#define DISP_HEIGHT 320
#define BUFSZ 64

int dataA[DISP_HEIGHT]; // data gelezen uit analoog input (2 tabellen)
int dataB[DISP_HEIGHT];
int sample_delay = 0; // tijd tussen metingen analoog input
int analog_range = 1024; // bereik van de analoog input
int skip = 4 ; // spatie tussen punten
int offset = 180;
int calibration = -5;
// DO NOT TOUCH!!!
int vMin=1024;
int vMax=0;
int vMed=0;
int D1=0;
int D2=0;
int D3=0;
const int buttonPin = A7;     // the number of the pushbutton pin
//const int samplePin = 4;
int buttonState = 0;         // variable for reading the pushbutton status
//int sampleState = 0; // variable for reading the pushbutton status
int scaling = 0;
int y = 0;
int z = 0;





void setup()  {
  pinMode(buttonPin, INPUT);
  //pinMode(samplePin, INPUT);
  
  Serial.begin(2000000);//,0,'*');
  Serial.write("\n"); //send <CR>
  //Serial.begin(115200);
  Serial.write("\n"); //send <CR>
  Serial.write("\n"); //send <CR>
  delay(500);
  Serial.write("\e[?25I"); // Hide cursor
  delay(2000);
  Serial.write("\e[?10P"); // portrait
  Serial.write("\e[2J"); // clear screen
  delay(400);
   //Serial.write("\e[?10L"); //set landscape mode
  Serial.write("\e(6"); // set fontsize to 5 points
  Serial.write("\e[63;63;0f\e{215 0p\e{215 320L");
  Serial.write("\e[?10L"); //set landscape mode
  delay(500);
  Serial.write("\e[63;63;0f\e{5 4p");
  Serial.write("|min V           |med V           |max V        ");
  delay(100);
  Serial.write("\e[63;63;63f      DSO");
  Serial.write("\e[?10P"); // portrait
  delay(500);
}


//#define analogReference(EXTERNAL);
// TE TRAAG!
void sample() {
 int i;
 //int K = analog_range - analogRead(A0);
 //delay(sample_delay);
 //int L = analog_range - analogRead(A0);
 //delay(sample_delay);
 //int M = analog_range - analogRead(A0);
 //if (analog_range - analogRead(A0) < 3){// && (L > K)){// && (K > M)){
   
   for (i = 0; i < DISP_HEIGHT / skip; ++i)
   {
      
      int vA;
      int vB;
      vA = analog_range - analogRead(A0); // voor "+" waarde
      vB = analog_range - analogRead(A1); // voor "-" waarde
      
      dataA[i] = vA;
      dataB[i] = vB;
      delay(sample_delay);
   }
  }
// }
void buttoncheck() {
  char buf[BUFSZ];
  int char_number = 0;
  buttonState = analogRead(buttonPin);
  if (buttonState > 975){ // UP
    scaling = scaling + 1;
    Serial.write("\e[?10L"); //set landscape mode
    delay(2);
    snprintf(buf, BUFSZ,"\e{15 225pZOOM= %dx", scaling*10);
    Serial.write(buf);
    delay(750);
    Serial.write("\e[0;0;0f\e{2 225 254 319F");
    Serial.write("\e[?10L"); //set landscape mode
  }
  if ((buttonState > 900)&&(buttonState < 950)){ // LEFT
    sample_delay=sample_delay+1;
    Serial.write("\e[?10L"); //set landscape mode
    delay(2);
    snprintf(buf, BUFSZ,"\e{15 225pSAMPLE-TIME= %dms", sample_delay);
    Serial.write(buf);
    delay(750);
    Serial.write("\e[0;0;0f\e{2 225 254 319F");
    Serial.write("\e[?10P"); //set landscape mode
  }
   
  //else{
  if ((buttonState > 820)&&(buttonState < 870)){ // DOWN
    scaling = scaling - 1;
    Serial.write("\e[?10L"); //set landscape mode
    delay(2);
    snprintf(buf, BUFSZ,"\e{15 225pZOOM= %dx", scaling*10);
    Serial.write(buf);
    delay(750);
    Serial.write("\e[0;0;0f\e{2 225 254 319F");
    Serial.write("\e[?10P"); //set landscape mode
  if (scaling < 0){
    scaling = 0;  
    Serial.write("\e[?10L"); //set landscape mode
    delay(20);
    Serial.write("\e{15 225pZOOM SET TO HIGHEST!");
    delay(1500);
    Serial.write("\e[0;0;0f\e{2 225 254 319F");
    delay(500);
    Serial.write("\e[?10P"); //set landscape mode
    delay(2);
  }
  }
      
   
  //else {
  if ((buttonState > 750)&&(buttonState < 790)){  // RIGHT
    sample_delay=sample_delay-1;
    Serial.write("\e[?10L"); //set landscape mode
    delay(2);
    snprintf(buf, BUFSZ,"\e{15 225pSAMPLE-TIME= %dms", sample_delay);
    Serial.write(buf);
    delay(750);
    Serial.print("\e[0;0;0f\e{2 225 254 319F");
    Serial.write("\e[?10P"); //set landscape mode
  if (sample_delay < 1){
    sample_delay = 0;
    //char_number = 26; // NUMBER OF CHARACTERS IN THE MESSAGE
    Serial.write("\e[?10L"); //set landscape mode
    delay(2);
    Serial.print("\e{15 225pSAMPLE-TIME SET TO FASTEST!");
    delay(1500);
    Serial.write("\e[0;0;0f\e{2 225 254 319F");
    delay(500);
    Serial.write("\e[?10P"); //set landscape mode
    delay(2);
  }
  }
}
   

  
  

void displine() {
 int i=0;
 int vA;
 int vB;
 char buf[BUFSZ];
 for (i = 0; i < DISP_HEIGHT / skip; ++i)
 {
   float vA = dataA[i];
   float vB = dataB[i];
   //offset = v - (DISP_WIDTH / (2*analog_range));
   y = (vA * DISP_WIDTH / (4096));
   z = (vB * DISP_WIDTH / (4096)); 
      /* if (y < 35){
         y =35;
       }
       if (y > 205){
         y =205;
       }
       if (z < 35){
         z =35;
       }
       if (z > 205){
         z =205;
       }*/
       if (vMax < (512 - (vA/2))){// && (vMax > vMin)){
         vMax =512 - (vA/2);
       }
       if (vMin > (512 - (vA/2))){// && (vMin < vMax)){
         vMin =512 - (vA/2);
       }
      
       vMed =((vMax + vMin) / 2);
   
  
   snprintf(buf, BUFSZ,"\e[0;0;0f\e{0 %dp\e{205 %dL\e[0;63;0f\e{%d %dP", i*skip, i*skip, (-y + (y / 10) * scaling)- offset +(calibration * scaling), i*skip - skip);
   Serial.write(buf);
     delay(2);
   snprintf(buf, BUFSZ,"\e{15 %dp\e[63;35;0f\e{%d %dP", i*skip, (-z + (z / 10) * scaling)- offset +(calibration * scaling), i*skip - skip);
   Serial.write(buf);
     delay(2);
   if (i >= DISP_HEIGHT){
     i=0;
  
     //snprintf(buf, BUFSZ, "\e[0;0;0f\e{35 %dp\e{205 %dL", i*skip, i*skip);
     //Serial.write(buf);
    // vMin=1024;
     //vMed=0;
     //vMax=0;
   }

 }
}

void loop() {
  vMin=1024;
  vMed=0;
  vMax=0;
  

  //readFast(10);
  sample();
  displine();
  buttoncheck();
  
//  buttons();
  Serial.write("\e[?10L"); //set landscape mode
  if (vMin < D3){
   Serial.write("\e[0;20;63f\e{55 4p"); Serial.print(D3);
   //Serial.write(" "); //write space to delete old characters on screen
     delay(8);
  }
  if ((vMed > D2)||(vMed < D2)){
   float D2= vMed * (5.0 / 2048.0);
   Serial.write("\e[0;63;0f\e{145 4p"); Serial.print(D2);
   //Serial.write(" "); //write space to delete old characters on screen
     delay(8);
  }
  if ((vMax > D1) || (vMax==1024)){
   float D1= vMax * (5.0 / 2048.0);
   Serial.write("\e[63;0;0f\e{238 4p"); Serial.print(D1);
   //Serial.write(" "); //write space to delete old characters on screen
     delay(8);
  } 
  Serial.write("\e[?10P"); //set Portrait mode
  D1=vMax;
  D2=vMed;
  D3=vMin;
  delay(8);
  Serial.flush();
  
}

@JohnWasser,

Thank you, i tried your adjusted code, but still no difference in running. allthough i am certain that your version is better than mine. i guess there must be another problem

as for the buttons:

i indeed use the series resistor, multiple buttons on one pin type of setup

After some more testing i found out that when i put a "return;" at the end of the first "if" statement for the buttons, that one button works. (and the main program works again...)

Though the other buttons still do nothing.

  Serial.begin(2000000);

Is the serial port capable of two million bits per second?

How are you expecting to get all of these different outcomes from doing an analogRead on a button ?????

@michinyon
Read reply #7

@johnwasser,

yes the serial port seems to handle this speed quite nicely. my display works best at this speed. and arduino copes fine with it..

i seem to have it narrowed down to one line.

In the next piece of code the uncommented code works fine. As soon as i put text in the data that is beeing send over serial, there occurs a problem and the program crashes.

in any other place in my code the text sending works fine! even right above this code!
Anyone an idea?

while (scaling <= 1) {
    scaling = 1;  
    //Serial.write("\e[?10L"); //set landscape mode
    //delay(200);
    //Serial.write("\e{15 225p ALLREADY THE HIGHEST!");<------------THIS CRASHES THE PROGRAM
      //delay(750);
    Serial.write("\e[0;0;63f\e{2 225 254 319F"); <--------------THIS WORKS, BUT IF I ADD TEXT HERE IT CRASHES
      delay(500);
    //Serial.write("\e[?10P"); //set landscape mode
    //delay(20); 
  }

This method of "disabling" a block of code ...

//  Serial.write("\e[?10L"); //set landscape mode
//  delay(200);
//  Serial.write("\e{15 225p ALLREADY THE HIGHEST!");<------------THIS CRASHES THE PROGRAM
//  delay(750);

.... is both error prone, hard to read and enable/disable.

A quicker and easier way might be any of the two following methods.

/*
    Serial.write("\e[?10L"); //set landscape mode
    delay(200);
    Serial.write("\e{15 225p ALLREADY THE HIGHEST!");<------------THIS CRASHES THE PROGRAM
    delay(750);
 */

... or ...

#if 0
    Serial.write("\e[?10L"); //set landscape mode
    delay(200);
    Serial.write("\e{15 225p ALLREADY THE HIGHEST!");<------------THIS CRASHES THE PROGRAM
    delay(750);
#endif

I suggest you ALWAYS post the latest COMPLETE version of your code to make it easier for those following along.

I suspect you are running out of SRAM. Try changing these kinds of calls:

 Serial.write("string constant");
 Serial.print("string constant");

to look like this:

 Serial.print(F("string constant"));

This will trick the compiler into leaving the string constants in FLASH and not copy them to SRAM to print.

(I can't tell if the .write() method supports the F() macro or not. Fortunatelt, .print() does the same as .write() for string constants.)

iSpider:
@johnwasser,

yes the serial port seems to handle this speed quite nicely. my display works best at this speed. and arduino copes fine with it..

What Arduino do you have? Running at what clock speed? According to the Atmega328P datasheet, running at 16 MHz (which is pretty typical) the maximum baud rate is 1 MBps. (Page 203).

Your baud rate of 2 MBps is out of range.

yes i know! i just had to rule out every single line of code. so i commented them out one by one.
Thanks though! i never saw the # option before.

here is the code working with all buttons but without the problem areas. I commented them out.

// Setup settings
#define DISP_WIDTH 220
#define DISP_HEIGHT 320
#define BUFSZ 64

int dataA[DISP_HEIGHT]; // data gelezen uit analoog input (2 tabellen)
int dataB[DISP_HEIGHT];
int sample_delay = 2; // tijd tussen metingen analoog input
int analog_range = 1024; // bereik van de analoog input
int skip = 4 ; // spatie tussen punten
int offset = 180;
int calibration = -5;
// DO NOT TOUCH!!!
int vMin=1024;
int vMax=0;
int vMed=0;
int D1=0;
int D2=0;
int D3=0;
const int buttonPin = A7;     // the number of the pushbutton pin
//const int samplePin = 4;
int buttonState = 0;         // variable for reading the pushbutton status
//int sampleState = 0; // variable for reading the pushbutton status
int scaling = 2;
int y = 0;
int z = 0;





void setup()  {
  pinMode(buttonPin, INPUT);
  //pinMode(samplePin, INPUT);
  
  Serial.begin(2000000);//,0,'*');
  Serial.write("\n"); //send <CR>
  //Serial.begin(115200);
  Serial.write("\n"); //send <CR>
  Serial.write("\n"); //send <CR>
  delay(500);
  Serial.write("\e[?25I"); // Hide cursor
  delay(2000);
  Serial.write("\e[?10P"); // portrait
  Serial.write("\e[2J"); // clear screen
  delay(400);
   //Serial.write("\e[?10L"); //set landscape mode
  Serial.write("\e(6"); // set fontsize to 5 points
  Serial.write("\e[63;63;0f\e{215 0p\e{215 320L");
  Serial.write("\e[?10L"); //set landscape mode
  delay(500);
  Serial.write("\e[63;63;0f\e{5 4p");
  Serial.write("|min V           |med V           |max V        ");
  delay(100);
  Serial.write("\e[63;63;63f      DSO");
  Serial.write("\e[?10P"); // portrait
  delay(500);
}


//#define analogReference(EXTERNAL);
// TE TRAAG!
void sample() {
 int i;
 //int K = analog_range - analogRead(A0);
 //delay(sample_delay);
 //int L = analog_range - analogRead(A0);
 //delay(sample_delay);
 //int M = analog_range - analogRead(A0);
 //if (analog_range - analogRead(A0) < 3){// && (L > K)){// && (K > M)){
   
   for (i = 0; i < DISP_HEIGHT / skip; ++i)
   {
      
      int vA;
      int vB;
      vA = analog_range - analogRead(A0); // voor "+" waarde
      vB = analog_range - analogRead(A1); // voor "-" waarde
      
      dataA[i] = vA;
      dataB[i] = vB;
      delay(sample_delay);
   }
  }
// }
void buttoncheck() {
  char buf[BUFSZ];
  Serial.write("\e[?10L"); //set landscape mode
  delay(20);
  buttonState = analogRead(buttonPin);
  if(buttonState > 975) { // UP
    scaling++;
    snprintf(buf, BUFSZ,"\e{15 225pZOOM= %dx", scaling*10);
    Serial.write(buf);
    delay(750);
    Serial.write("\e[0;0;0f\e{2 225 254 319F");
  }
  if ((buttonState > 900)&&(buttonState < 950)){ // LEFT
    sample_delay++;
    delay(20);
    snprintf(buf, BUFSZ,"\e{15 225pSAMPLE-TIME= %dms", sample_delay);
    Serial.write(buf);
    delay(750);
    Serial.write("\e[0;0;0f\e{2 225 254 319F");
  }
  if ((buttonState > 820)&&(buttonState < 870)){ // DOWN
    scaling--;
    delay(20);
    snprintf(buf, BUFSZ,"\e{15 225pZOOM= %dx", scaling*10);
    Serial.write(buf);
    delay(750);
    Serial.write("\e[0;0;0f\e{2 225 254 319F");
    }
  /*if (scaling <= 1) {
    scaling = 1;  
    Serial.write("\e[?10L"); //set landscape mode
    delay(200);
    Serial.write("\e{15 225p ALLREADY THE HIGHEST!");
      delay(750);
    Serial.write("\e[0;0;63f\e{2 225 254 319F");
      delay(500);
    Serial.write("\e[?10P"); //set landscape mode
    delay(20); */ 
  }
 
  if ((buttonState > 750)&&(buttonState < 790)){  // RIGHT
    sample_delay--;
    delay(20);
    snprintf(buf, BUFSZ,"\e{15 225pSAMPLE-TIME= %dms", sample_delay);
    Serial.write(buf);
    delay(750);
    Serial.print("\e[0;0;0f\e{2 225 254 319F");
  }
  if (sample_delay < 1){
    sample_delay = 0;
    /*Serial.write("\e[?10L"); //set landscape mode
    delay(20); 
    Serial.print("\e{15 225pSAMPLE-TIME SET TO FASTEST!");
    delay(1500);
    Serial.write("\e[0;0;0f\e{2 225 254 319F");
    delay(500);
    Serial.write("\e[?10P"); //set landscape mode
    delay(20); */
  }
  Serial.write("\e[?10P"); //set landscape mode */
}
   

  
  

void displine() {
 int i=0;
 int vA;
 int vB;
 char buf[BUFSZ];
 for (i = 0; i < DISP_HEIGHT / skip; ++i)
 {
   float vA = dataA[i];
   float vB = dataB[i];
   //offset = v - (DISP_WIDTH / (2*analog_range));
   y = (vA * DISP_WIDTH / (4096));
   z = (vB * DISP_WIDTH / (4096)); 
      /* if (y < 35){
         y =35;
       }
       if (y > 205){
         y =205;
       }
       if (z < 35){
         z =35;
       }
       if (z > 205){
         z =205;
       }*/
       if (vMax < (512 - (vA/2))){// && (vMax > vMin)){
         vMax =512 - (vA/2);
       }
       if (vMin > (512 - (vA/2))){// && (vMin < vMax)){
         vMin =512 - (vA/2);
       }
      
       vMed =((vMax + vMin) / 2);
   
  
   snprintf(buf, BUFSZ,"\e[0;0;0f\e{0 %dp\e{205 %dL\e[0;63;0f\e{%d %dP", i*skip, i*skip, (-y + (y / 10) * scaling)- offset +(calibration * scaling), i*skip - skip);
   Serial.write(buf);
     delay(2);
   snprintf(buf, BUFSZ,"\e{15 %dp\e[63;35;0f\e{%d %dP", i*skip, (-z + (z / 10) * scaling)- offset +(calibration * scaling), i*skip - skip);
   Serial.write(buf);
     delay(2);
   if (i >= DISP_HEIGHT){
     i=0;
  
     //snprintf(buf, BUFSZ, "\e[0;0;0f\e{35 %dp\e{205 %dL", i*skip, i*skip);
     //Serial.write(buf);
    // vMin=1024;
     //vMed=0;
     //vMax=0;
   }

 }
}

void loop() {
  vMin=1024;
  vMed=0;
  vMax=0;
  

  //readFast(10);
  sample();
  buttoncheck();
  displine();
//  buttons();
  Serial.write("\e[?10L"); //set landscape mode
  if (vMin < D3){
   Serial.write("\e[0;20;63f\e{55 4p"); Serial.print(D3);
   //Serial.write(" "); //write space to delete old characters on screen
     delay(8);
  }
  if ((vMed > D2)||(vMed < D2)){
   float D2= vMed * (5.0 / 2048.0);
   Serial.write("\e[0;63;0f\e{145 4p"); Serial.print(D2);
   //Serial.write(" "); //write space to delete old characters on screen
     delay(8);
  }
  if ((vMax > D1) || (vMax==1024)){
   float D1= vMax * (5.0 / 2048.0);
   Serial.write("\e[63;0;0f\e{238 4p"); Serial.print(D1);
   //Serial.write(" "); //write space to delete old characters on screen
     delay(8);
  } 
  Serial.write("\e[?10P"); //set Portrait mode
  D1=vMax;
  D2=vMed;
  D3=vMin;
  delay(8);
  Serial.flush();
  
}

I suspect you are running out of SRAM. Try changing these kinds of calls:
Code:

Serial.write("string constant");
Serial.print("string constant");

to look like this:
Code:

Serial.print(F("string constant"));

This will trick the compiler into leaving the string constants in FLASH and not copy them to SRAM to print.

(I can't tell if the .write() method supports the F() macro or not. Fortunatelt, .print() does the same as .write() for string constants.)

INDEED! this was the problem! it is working fine now! i would have never come up with this on my own. Thank you!
For now i have only added the (F(... in the buttoncheck.

can there be negative consequences to using this in my whole sketch?

can there be negative consequences to using this in my whole sketch?

No. Indeed, since you're clearly close to the edge in terms of SRAM consumption it would be a good idea to extend its use or when you next tweak the code, you may find yourself in trouble again.