Changing the value of a variable using serial monitor

Hi I’m trying to changing the value of a variable using serial monitor. But, the problem is everytime the value of the variable is changing automatically into zero. I’m using arduino uno

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

const int  Up_buttonPin   = 7;    // the pin that the pushbutton is attached to

const int  Down_buttonPin = 8;

int total;
int buzzer = 13;  
int data = 0;
int rem;
int buttonPushCounter = 0;    // counter for the number of button presses
int up_buttonState = 0;       // current state of the up button
int up_lastButtonState = 0;   // previous state of the up button

int down_buttonState = 0;         // current state of the up button
int down_lastButtonState = 0;     // previous state of the up button
bool bPress = false;


void setup()
{
  Serial.begin(9600);
  pinMode( Up_buttonPin , INPUT_PULLUP);
  pinMode( Down_buttonPin , INPUT_PULLUP);
  lcd.begin();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("pill dibo?");
  delay(3000);
  lcd.clear();
  lcd.print("Pills taken:");
  lcd.setCursor(2, 1);
  lcd.print(buttonPushCounter);
}
void loop()
{

 
  checkUp();
  checkDown();
  
  
  if(Serial.available()>0)
  {
    data=Serial.parseInt();
    total = data;
    Serial.print("total: ");
      Serial.println(total);
  }
  
  rem = total - buttonPushCounter; 
   if(rem<5 && rem>0){
    tone(13, 450);
    delay (500);
    noTone(13);
    delay (500);
    //Serial.println("dokane ja");
    }
  if ( bPress)
  {
    bPress = false;
    lcd.setCursor(2, 1);
    lcd.print("               ");
    lcd.setCursor(2, 1);
    lcd.print(buttonPushCounter);
  }

  

}
void checkUp()
{
  up_buttonState = digitalRead(Up_buttonPin);
  if (up_buttonState != up_lastButtonState)    // compare the buttonState to its previous state
  {
    if (up_buttonState == LOW)   // if the state has changed, increment the counter
    {
      bPress = true;  // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    }
    else {
      Serial.println("off"); // if the current state is LOW then the button went from on to off:
    }
    delay(50);  // Delay a little bit to avoid bouncing
  }

  up_lastButtonState = up_buttonState;   // save the current state as the last state, for next time through the loop
}

void checkDown()
{
  down_buttonState = digitalRead(Down_buttonPin);
  if (down_buttonState != down_lastButtonState)  // compare the buttonState to its previous state
  {
    if (down_buttonState == LOW)   // if the state has changed, increment the counter
    {
      bPress = true;
      buttonPushCounter--;  // if the current state is HIGH then the button went from off to on:
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      Serial.println("off");  // if the current state is LOW then the button went from on to off:
    }
    delay(50);            // Delay a little bit to avoid bouncing
  }
  down_lastButtonState = down_buttonState;  // save the current state as the last state, for next time through the loop

}

parseint() stops when a none digit is read
therefore if you text is terminated with NL and/or CR the parsing will stop and return the integer value
if you call parseint() again it finds the terminating character and returns 0
after calling parseInt() flush the input buffer, e.g.

 if(Serial.available()>0)
  {
    int data,total;
    data=Serial.parseInt(SKIP_WHITESPACE);
    Serial.print("data: ");
    Serial.println(data);
    while (Serial.available() > 0) {
       Serial.read();
}

}

have look at Serial Input Basics

horace:
parseint() stops when a none digit is read
therefore if you text is terminated with NL and/or CR the parsing will stop and return the integer value
if you call parseint() again it finds the terminating character and returns 0
after calling parseInt() flush the input buffer, e.g.

 if(Serial.available()>0)

{
    int data,total;
    data=Serial.parseInt(SKIP_WHITESPACE);
    Serial.print("data: ");
    Serial.println(data);
    while (Serial.available() > 0) {
      Serial.read();
}

}



have look at [Serial Input Basics](https://forum.arduino.cc/index.php?topic=396450.0)

Will you please explain what are NL and CR. I'm not familiar with them. Thanks in advance.

carrage return and line-feed

you may be interested in pcRead, see setting of “analogPin”

// pcRead - debugging using serial monitor

const char version [] = "PcRead 200416a";

int debug = 0;

// ---------------------------------------------------------
// toggle output bit
void
pinToggle (
    int pin)
{
    static int  bits = 0;
    int     bit = 1 << pin;

    if (debug)  {
        Serial.print ("pinToggle: ");
        Serial.println (pin);
    }

    if (bits & bit)  {
        digitalWrite (pin, LOW);
        bits &= ~bit;
    }
    else {
        digitalWrite (pin, HIGH);
        bits |= bit;
    }
}

// ---------------------------------------------------------
// toggle output bit
int
readString (
    char *s,
    int   maxChar )
{
    int  n = 0;

    Serial.print ("> ");
    do {
        if (Serial.available()) {
            int c    = Serial.read ();

            if ('\n' == c)
                break;

            s [n++] = c;
            if (maxChar == n)
                break;
        }
    } while (true);

    return n;
}

// -----------------------------------------------------------------------------
// process single character commands from the PC
#define MAX_CHAR  10
char s [MAX_CHAR] = {};

int  analogPin = 0;

void
pcRead (void)
{

    static int  val = 13;

    if (Serial.available()) {
        int c = Serial.read ();

        switch (c)  {
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            val = c - '0' + (10 * val);
            break;

        case 'A':
            analogPin = val;
            Serial.print   ("analogPin = ");
            Serial.println (val);
            val = 0;
            break;

        case 'D':
            debug ^= 1;
            break;

        case 'I':
            pinMode (val, INPUT);
            Serial.print   ("pinMode ");
            Serial.print   (val);
            Serial.println (" INPUT");
            val = 0;
            break;

        case 'O':
            pinMode (val, OUTPUT);
            Serial.print   ("pinMode ");
            Serial.print   (val);
            Serial.println (" OUTPUT");
            val = 0;
            break;

        case 'P':
            pinMode (val, INPUT_PULLUP);
            Serial.print   ("pinMode ");
            Serial.print   (val);
            Serial.println (" INPUT_PULLUP");
            val = 0;
            break;


        case 'a':
            Serial.print   ("analogRead: ");
            Serial.println (analogRead (val));
            val = 0;
            break;

        case 'c':
            digitalWrite (val, LOW);
            Serial.print   ("digitalWrite: LOW  ");
            Serial.println (val);
            val = 0;
            break;

        case 'p':
#if !defined(ARDUINO_ARCH_ESP32)
            analogWrite (analogPin, val);
            Serial.print   ("analogWrite: pin ");
            Serial.print   (analogPin);
            Serial.print   (", ");
            Serial.println (val);
            val = 0;
#endif
            break;

        case 'r':
            Serial.print   ("digitalRead: pin ");
            Serial.print   (val);
            Serial.print   (", ");
            Serial.println (digitalRead (val));
            val = 0;
            break;

        case 's':
            digitalWrite (val, HIGH);
            Serial.print   ("digitalWrite: HIGH ");
            Serial.println (val);
            val = 0;
            break;

        case 't':
            Serial.print   ("pinToggle ");
            Serial.println (val);
            pinToggle (val);
            val = 0;
            break;

        case 'v':
            Serial.print ("\nversion: ");
            Serial.println (version);
            break;

        case '\n':          // ignore
            break;

        case '"':
            while ('\n' != Serial.read ())     // discard linefeed
                ;

            readString (s, MAX_CHAR-1);
            Serial.println (s);
            break;

        case '?':
            Serial.println ("\npcRead:\n");
            Serial.println ("    [0-9] append to #");
            Serial.println ("    A # - set analog pin #");
            Serial.println ("    D # - set debug to #");
            Serial.println ("    I # - set pin # to INPUT");
            Serial.println ("    O # - set pin # to OUTPUT");
            Serial.println ("    P # - set pin # to INPUT_PULLUP");
            Serial.println ("    a # - analogRead (pin #)");
            Serial.println ("    c # - digitalWrite (pin #, LOW)");
            Serial.println ("    p # -- analogWrite (analogPin, #)");
            Serial.println ("    r # - digitalRead (pin #)");
            Serial.println ("    s   - digitalWrite (pin #, HIGH)");
            Serial.println ("    t   -- toggle pin # output");
            Serial.println ("    v   - print version");
            Serial.println ("    \"   - read string");
            Serial.println ("    ?   - list of commands");
            break;

        default:
            Serial.print ("unknown char ");
            Serial.println (c,HEX);
            break;
        }
    }
}

// -----------------------------------------------------------------------------
void
loop (void)
{
    pcRead ();
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin(115200);

    Serial.println (version);
#if defined(ARDUINO_ARCH_ESP32)
    Serial.println ("esp32");
#endif
}

CR (carriage Return ASCII 0x0D) moves the cursor to start of current line
NL (new Line) incicate end of line of text is usually LF (line feed ASCII 0x0A) moves cursor down one line

at the bottom of the Arduino Serial Monitor is a popup menu which enables the selection of line termination, e.g. none, newline, CR or both NL & CR to suit the program being executed

gcjr:
carrage return and line-feed

you may be interested in pcRead, see setting of “analogPin”

// pcRead - debugging using serial monitor

const char version = “PcRead 200416a”;

int debug = 0;

// ---------------------------------------------------------
// toggle output bit
void
pinToggle (
    int pin)
{
    static int  bits = 0;
    int    bit = 1 << pin;

if (debug)  {
        Serial.print ("pinToggle: ");
        Serial.println (pin);
    }

if (bits & bit)  {
        digitalWrite (pin, LOW);
        bits &= ~bit;
    }
    else {
        digitalWrite (pin, HIGH);
        bits |= bit;
    }
}

// ---------------------------------------------------------
// toggle output bit
int
readString (
    char *s,
    int  maxChar )
{
    int  n = 0;

Serial.print ("> ");
    do {
        if (Serial.available()) {
            int c    = Serial.read ();

if (’\n’ == c)
                break;

s [n++] = c;
            if (maxChar == n)
                break;
        }
    } while (true);

return n;
}

// -----------------------------------------------------------------------------
// process single character commands from the PC
#define MAX_CHAR  10
char s [MAX_CHAR] = {};

int  analogPin = 0;

void
pcRead (void)
{

static int  val = 13;

if (Serial.available()) {
        int c = Serial.read ();

switch (c)  {
        case ‘0’:
        case ‘1’:
        case ‘2’:
        case ‘3’:
        case ‘4’:
        case ‘5’:
        case ‘6’:
        case ‘7’:
        case ‘8’:
        case ‘9’:
            val = c - ‘0’ + (10 * val);
            break;

case ‘A’:
            analogPin = val;
            Serial.print  ("analogPin = ");
            Serial.println (val);
            val = 0;
            break;

case ‘D’:
            debug ^= 1;
            break;

case ‘I’:
            pinMode (val, INPUT);
            Serial.print  (“pinMode “);
            Serial.print  (val);
            Serial.println (” INPUT”);
            val = 0;
            break;

case ‘O’:
            pinMode (val, OUTPUT);
            Serial.print  (“pinMode “);
            Serial.print  (val);
            Serial.println (” OUTPUT”);
            val = 0;
            break;

case ‘P’:
            pinMode (val, INPUT_PULLUP);
            Serial.print  (“pinMode “);
            Serial.print  (val);
            Serial.println (” INPUT_PULLUP”);
            val = 0;
            break;

case ‘a’:
            Serial.print  ("analogRead: ");
            Serial.println (analogRead (val));
            val = 0;
            break;

case ‘c’:
            digitalWrite (val, LOW);
            Serial.print  ("digitalWrite: LOW  ");
            Serial.println (val);
            val = 0;
            break;

case ‘p’:
#if !defined(ARDUINO_ARCH_ESP32)
            analogWrite (analogPin, val);
            Serial.print  ("analogWrite: pin “);
            Serial.print  (analogPin);
            Serial.print  (”, ");
            Serial.println (val);
            val = 0;
#endif
            break;

case ‘r’:
            Serial.print  ("digitalRead: pin “);
            Serial.print  (val);
            Serial.print  (”, ");
            Serial.println (digitalRead (val));
            val = 0;
            break;

case ‘s’:
            digitalWrite (val, HIGH);
            Serial.print  ("digitalWrite: HIGH ");
            Serial.println (val);
            val = 0;
            break;

case ‘t’:
            Serial.print  ("pinToggle ");
            Serial.println (val);
            pinToggle (val);
            val = 0;
            break;

case ‘v’:
            Serial.print ("\nversion: ");
            Serial.println (version);
            break;

case ‘\n’:          // ignore
            break;

case ‘"’:
            while (’\n’ != Serial.read ())    // discard linefeed
                ;

readString (s, MAX_CHAR-1);
            Serial.println (s);
            break;

case ‘?’:
            Serial.println ("\npcRead:\n");
            Serial.println ("    [0-9] append to #");
            Serial.println ("    A # - set analog pin #");
            Serial.println ("    D # - set debug to #");
            Serial.println ("    I # - set pin # to INPUT");
            Serial.println ("    O # - set pin # to OUTPUT");
            Serial.println ("    P # - set pin # to INPUT_PULLUP");
            Serial.println ("    a # - analogRead (pin #)");
            Serial.println ("    c # - digitalWrite (pin #, LOW)");
            Serial.println ("    p # – analogWrite (analogPin, #)");
            Serial.println ("    r # - digitalRead (pin #)");
            Serial.println ("    s  - digitalWrite (pin #, HIGH)");
            Serial.println ("    t  – toggle pin # output");
            Serial.println ("    v  - print version");
            Serial.println ("    "  - read string");
            Serial.println ("    ?  - list of commands");
            break;

default:
            Serial.print ("unknown char ");
            Serial.println (c,HEX);
            break;
        }
    }
}

// -----------------------------------------------------------------------------
void
loop (void)
{
    pcRead ();
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin(115200);

Serial.println (version);
#if defined(ARDUINO_ARCH_ESP32)
    Serial.println (“esp32”);
#endif
}

Thanks a lot. It worked.