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