I want to set my plus and minus buttons to increment and decrement as long as it is pressed

#include <avr/io.h>
 
 volatile int flow_frequency; 
 
 const int PLUS_BUTTON = A1;
 const int MINUS_BUTTON= A2;
 const int BUTTON_PIN = A3; 
 const int BUTTON2_PIN= A4;
 const int BUTTON3_PIN= A5;
const int RELAY_PIN  = 13;
int relayState=0;
float counter = 0.0;
int plusbuttonState=0;
int lastplusState=0;
int minusbuttonState=0;
int lastminusState=0;
bool bPress= false;
float vol = 0.0,l_minute;
unsigned char flowsensor = 2; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 9, 5, 4, 3);

void flow () // Interrupt function
{ 
   flow_frequency++;
}
void setup() {
  // put your setup code here, to run once:
    Serial.begin(9600);            
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(RELAY_PIN, OUTPUT);        
  pinMode(PLUS_BUTTON, INPUT_PULLUP);
  pinMode (MINUS_BUTTON, INPUT_PULLUP);
   pinMode(flowsensor, INPUT);
   digitalWrite(flowsensor, HIGH); 
   Serial.begin(9600);
   lcd.begin(16, 2);
   attachInterrupt(digitalPinToInterrupt(flowsensor), flow, RISING);
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("ID Technology");
   lcd.setCursor(0,1);
   currentTime = millis();
   cloopTime = currentTime;


}

void loop() {
  // put your main code here, to run repeatedly:
 checkUp();
 checkDown();
 
if (bPress){
  
  bPress=false;
  
  }
     currentTime = millis();
  
   if(currentTime >= (cloopTime + 1000))
   {
      
    cloopTime = currentTime; // Updates cloopTime

    if(flow_frequency != 0){
      
      l_minute = (flow_frequency / 7.5); // (Pulse frequency x 60 min) / 7.5
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Rate: ");
      lcd.print(l_minute);
      lcd.print(" L/M");
      l_minute = l_minute/50;
      lcd.setCursor(0,1);
      vol = vol +l_minute;
      lcd.print("Vol:");
      lcd.print(vol);
      lcd.print(" L");
      flow_frequency = 0; 
      Serial.print(l_minute, DEC); 
      Serial.println(" L/Sec");
      
    }
    else {
      Serial.println(" flow rate = 0 ");
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Rate: ");
      lcd.print( flow_frequency );
      lcd.print(" L/M");
      lcd.setCursor(0,1);
      lcd.print("Vol:");
      lcd.print(vol);
      lcd.print(" L");
    }
     lcd.setCursor(20,0);
    lcd.print("Value:"); 
    lcd.print(counter);
    
   }
   int buttonOld;
   int buttonNew;
  int button2Old;
  int button2New;
  int button3Old;
  int button3New;
  buttonNew = digitalRead(BUTTON_PIN);
  button2New = digitalRead(BUTTON2_PIN); 
  button3New = digitalRead(BUTTON3_PIN); 
  if (buttonNew ==1) {
    if (relayState ==0){

      digitalWrite (RELAY_PIN, HIGH);
      relayState=1;
     
    }

    
      } 
  else if (button2New==1) {
    if (relayState ==1){
      digitalWrite (RELAY_PIN, LOW);
      relayState=0;
      }

     
}
 else if (button3New==1){
  
  {
    digitalWrite (RELAY_PIN, LOW);
      relayState=0;
       vol=0.0;
    counter=0.0;
    }
  
  
  }
  else if (vol>=counter){
     if (relayState ==1){     
    digitalWrite (RELAY_PIN, LOW);
    relayState=0;
    vol=0.0;
   // counter=0.0;
  }
 
    
      }
}
void checkUp()
{  plusbuttonState = digitalRead(PLUS_BUTTON);

  if (plusbuttonState != lastplusState) {
    if (plusbuttonState == LOW) {
        bPress = true;
      counter= counter+0.5;
      Serial.println(counter);
    }
    else {
    }
    delay(10);
  }
  lastplusState = plusbuttonState;
}
void checkDown()
{
  minusbuttonState = digitalRead(MINUS_BUTTON);

  if (minusbuttonState != lastminusState) {

    if (minusbuttonState == LOW) {
        bPress = true;
      counter= counter-0.5;
      Serial.println(counter);
    } 
    else {
    }
    delay(10);
  }
  lastminusState = minusbuttonState;
}

@ibrahimd96, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

I guess that you can fill in the else in checkUp and checkDown(). Something like

void checkDown()
{
  minusbuttonState = digitalRead(MINUS_BUTTON);

  if (minusbuttonState != lastminusState) {

    if (minusbuttonState == LOW) {
      bPress = true;
      counter = counter - 0.5;  // sterretje: don't do this, see below
      Serial.println(counter);
    }
    else {
      // sterretje
      bPress = false;
    }
    delay(10);
  }
  lastminusState = minusbuttonState;
}

And in loop() you can, in it's simplest form, use a while-loop

do
{
  checkDown();
  if(bpress == true)
  {
    counter -= 0.5
    delay(200);
  }
} while(bpress == true);

As long as the minus button is pressed, the counter will go down every 200 milliseconds.

Obviously, this is blocking code till you release the min button so you have to puzzle a bit if you want to get rid of that.

Notes:

  1. Don't change the counter in checkUp() and checkDown(); you should seperate the button reading from the actual action.
  2. Use two variables to remember the button states, one for the plus button and one for the minus button.

Try the marked changes so the counter increments during a button press

  if (plusbuttonState != lastplusState) {  // *** That requires a change of state before the counter is incremented. Try removing it.
    if (plusbuttonState == LOW) {
        bPress = true;
      counter= counter+0.5;
      Serial.println(counter);
    }
    else {
    }
    delay(10);
  }  // *** and remove the corresponding '}'

EDIT
I didn't see the answer from @sterretje before posting.

Why? You've been given a solution. You're asking someone to do your job.

No you have a go at correcting your code. If you get stuck then you can always post your code that doesn't work and ask again so we can tell you where you went wrong. Otherwise you will never learn anything.

not really :slight_smile: i tries to edit my code while something went wrong thats why i am asking :slight_smile:

You didn't post the results of your edit to the code. So

can U post the full code please

Also post details of the "something went wrong" so we know what it is.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.