Simple button counter that increments by 5

As the title says, i need a button counter that increments by 5 for each push of the button.

Here is what i have so far:

buttonState3 = digitalRead(alfaButton);

if (buttonState3 == HIGH){ 
  alfa = ++ alfaVal;
    alfaVal2 = alfa / 100 * 5;
    lcd.setCursor(2,1);      
    lcd.print(alfaVal2);

This actually displays what i want, but i have to keep the button pressed for about a second to make it increment.
Anyone knows a fix for this?

Here is the full code:

#include <Wire.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);



#define buttonPin 9                 // button on pin 4
#define alfaButton 8

int value = LOW;                    // previous value of the LED
int buttonState;                    // variable to store button state
int lastButtonState;                // variable to store last button state
long interval = 100;                // blink interval - change to suit
long previousMillis = 0;            // variable to store last time LED was updated
long startTime ;                    // start time for stop watch
long elapsedTime ;                  // elapsed time for stop watch
int fractional;                     // variable used to store fractional part of time
int alfa = 0;
int alfaVal = 5;
int alfaVal2 = 0;


boolean start = false;
byte lastbuttonState2 = 0;          // new global variable
byte buttonState1 = 0;
byte buttonState2 = 0;
byte buttonState3 = 0;
byte buttonState4 = 0;

void setup()
{
  Serial.begin(9600);

  lcd.begin(16, 4);
  // Print a message to the LCD.
  lcd.print("PRAKTISK SKYTING");

    lcd.setCursor(0,1);      
    lcd.print("A:");

    lcd.setCursor(-4,2);   
    lcd.print("C:");
    
    lcd.setCursor(-4,3);
    lcd.print("D:");    


  pinMode(buttonPin, INPUT);       // not really necessary, pins default to INPUT anyway
  pinMode(alfaButton, INPUT);       // not really necessary, pins default to INPUT anyway

  digitalWrite(buttonPin, HIGH);   // turn on pullup resistors. Wire button so that press shorts pin to ground.
  digitalWrite(alfaButton, HIGH);   // turn on pullup resistors. Wire button so that press shorts pin to ground.


}

void loop()
{ 
  buttonState1 = digitalRead(buttonPin);
  delay(10);
  
  buttonState2 = digitalRead(buttonPin);

  if(buttonState1 == buttonState2) {
    if(buttonState2 == LOW){
      start = true;
      startTime = millis();
      lastButtonState = buttonState2;
    }
  
  if(start == true){
  elapsedTime =   millis() - startTime;              // store elapsed time
    lastButtonState = buttonState;                     // store buttonState in lastButtonState, to compare next time

     
    lcd.setCursor(7,3);
    lcd.print("");
    // routine to report elapsed time 
    lcd.print( (int)(millis() / 1000UL));         // divide by 1000 to convert to seconds - then cast to an int to print

    lcd.print(".");                             

    fractional = (int)(millis() % 1000UL);

    if (fractional == 0)
      lcd.print("000");     
    else if (fractional < 10)    
      lcd.print("00");       
    else if (fractional < 100)
      lcd.print("0");        

    lcd.print(fractional);  // print fractional part of time 
  }

  }
  
  
  
  
  
// Sound timer  
  
  
    // check for button press
    buttonState = digitalRead(buttonPin);                   // read the button state and store

    if (buttonState == HIGH && lastButtonState == LOW){     // check for a high to low transition
       // if true then found a new button press while clock is not running - start the clock

       startTime = elapsedTime;                                  // store the start time
       delay(5);                                               // short delay to debounce switch
       lastButtonState = buttonState;                          // store buttonState in lastButtonState, to compare next time

    }

    else if (buttonState == LOW && lastButtonState == HIGH){     // check for a high to low transition
       // if true then found a new button press while clock is running - stop the clock and report

         elapsedTime =   millis(); - startTime;              // store elapsed time
         lastButtonState = buttonState;                     // store buttonState in lastButtonState, to compare next time




  
  
  
  lcd.setCursor(4,2);
  lcd.print("   ");
        // routine to report elapsed time 
         lcd.print( (int)(elapsedTime / 1000UL));         // divide by 1000 to convert to seconds - then cast to an int to print

         lcd.print(".");                             // print decimal point

         // use modulo operator to get fractional part of time 
        fractional = (int)(elapsedTime % 1000UL);

        // pad in leading zeros - wouldn't it be nice if 
        // Arduino language had a flag for this? :)
        if (fractional == 0)
           lcd.print("000");      // add three zero's
        else if (fractional < 10)    // if fractional < 10 the 0 is ignored giving a wrong time, so add the zeros
           lcd.print("00");       // add two zeros
        else if (fractional < 100)
           lcd.print("0");        // add one zero

        lcd.print(fractional);  // print fractional part of time 

    }

    else{
       lastButtonState = buttonState; 
  
  
  
  
}
buttonState3 = digitalRead(alfaButton);

if (buttonState3 == HIGH){ 
  alfa = ++ alfaVal;
    alfaVal2 = alfa / 100 * 5;
    lcd.setCursor(2,1);      
    lcd.print(alfaVal2);

}
}

my advice would be lose the delay and use the method underline in blink without delay. the other thing is to debounce the switch

I realize that the code is far from optimal, but there should be a fix for my emmidiate problem, as i have one other button that reacts when being pushed.

When i hold the button down it behaves as a timer and increment by 5 each second... Any suggestions to why it does this?

Is this line doing what you intend ?

         elapsedTime =   millis(); - startTime;              // store elapsed time

Yes, for the time beeing..

try this, its my code, see how i did the press thing

uint8_t Switch = 2;
uint8_t Led =13;

boolean LedState =LOW;
int SwitchState =0;
int SwitchDebounce;
int LastSwitchState=HIGH;
int LastSwitchDebounce=LOW;
int Counter=0;

unsigned long LastDebounceTime = 0;
unsigned long DebounceDelay = 50;

void setup()
{
  pinMode(Switch,INPUT);
  digitalWrite(Switch,HIGH);
  pinMode(Led,OUTPUT);
  Serial.begin(9600);
}

void loop() 
{
  int CurrentSwitch = digitalRead(Switch);
  if (CurrentSwitch != LastSwitchDebounce)
  {
    LastDebounceTime = millis();
  } 
  if ((millis() - LastDebounceTime) > DebounceDelay) 
  {
    if (CurrentSwitch != LastSwitchState) 
    {
      if (CurrentSwitch == LOW)
      {
        Counter=Counter+5;
        Serial.println(Counter);
      } 
    }
    LastSwitchState=CurrentSwitch;
  }
  LastSwitchDebounce = CurrentSwitch;
}

It works great, but i need it to start from zero and not from 5 as it is now. Do you know how i can make that happen?

this part

int Counter=0;

change to this

int Counter=5;

If i changed it to 5 it started at 10, but you showed me what figure to change, and if i changed it to -5 it started at zero as i wanted.

Thank you "ash901226" for solving my problem:)

welcome.

i think among a few thing that people should know when starting o learn how to program arduino is the blink without delay , debounce and state change... and flag based programming . this all have a powerful effect toward solving most problem

Webca:
Yes, for the time beeing…

OK, but can you explain to me what it is doing ?

Webca: It works great, but i need it to start from zero and not from 5 as it is now. Do you know how i can make that happen?

ash901226's code starts from zero and increments by five each time the button is pressed. So the first button press changes to the value to five, the next button press changes the value to ten, and so on. If that's not what you want, what do you want?

HazardsMind: Instead of setting counter to -5, and having "Counter=Counter+5;", just have "Counter+5;"

Counter=Counter+5; starts counter at 0, then adds 5 to counter and result is counter = 5. (5 = 0 + 5) thats why it starts at 5 and not 0

Counter+5, this will start the timer a 0 and then after it loops, it will increment. its the same as saying count++, which means count +1.

If I understood you correctly then that is more or less completely wrong. (Perhaps I misunderstood - that happens quite often.) Perhaps you could post a code fragment illustrating the technique you're suggesting?

UKHeliBob: Is this line doing what you intend ?

         elapsedTime =   millis(); - startTime;              // store elapsed time

Semicolons go at the ends of statements, not in the middle. With that you are just setting elapsedTime equal to millis() but you're not doing any subtraction. Is that really what was intended?

When i opened the serial monitor it showed 5 without pressing the button..

Delta_G The code works as i want it to, but the code is probably full of wrong code and i will clean it up when the project is complete:)

I’ll explain what you have.

This will show 5 when the button is pressed. NOTE it will not start at 0, even when declaired “int Counter = 0;” the reason your Serial.println().

if (CurrentSwitch == LOW)
      {
        Counter=Counter+5;
        Serial.println(Counter); // <= This is the reason you see 5 instead of 0 because it is inside the same IF statement, however if you move
                                       // Serial.println(Counter); outside the IF statement it will start at 0, but it will show continious numbers.
      }