Pages: [1] 2   Go Down
Author Topic: Simple button counter that increments by 5  (Read 850 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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:

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);

}
}
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4018
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is this line doing what you intend ?
Code:
         elapsedTime =   millis(); - startTime;              // store elapsed time
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, for the time beeing..
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

try this, its my code, see how i did the press thing
Code:
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;
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this part
Code:
int Counter=0;
change to this
Code:
int Counter=5;
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:)
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4018
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, for the time beeing..
OK, but can you explain to me what it is doing ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12534
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12534
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
God Member
*****
Karma: 17
Posts: 721
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is this line doing what you intend ?
Code:
         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? 
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2   Go Up
Jump to: