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
}