Hit counter

Hello everybody,

I have a question for my hit counter sketch.

When I push the button it will count by 1, 2, 3 etc…

I’m trying but it will not work to change the number 1 to a 0,1 :confused:

I think it’s very easy but I’m stuck!

This is my code:

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// set up a constant for the tilt switchPin
const int setpointButton = 19;

int hits = 0;

// variable to hold the value of the switchPin
int switchState = 0;

// variable to hold previous value of the switchpin
int prevSwitchState = 0;

void setup() {
  // set up the number of columns and rows on the LCD
  lcd.begin(16, 2);

  // set up the switch pin as an input
  pinMode(setpointButton, INPUT_PULLUP);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Hit the button");
  lcd.setCursor(0, 1);
  lcd.print("to increment");
}

void loop() {
  // check the status of the switch
  switchState = digitalRead(setpointButton);

  // compare the switchState to its previous state
  if (switchState != prevSwitchState) {
    if (switchState == LOW) {
      hits = hits ++;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Hits:");
      lcd.setCursor(0, 1);
      lcd.print(hits);
    }
  }
  // save the current switch state as the last state
  prevSwitchState = switchState;
}

Sorry for my bad englisch…

I'm trying but it will not work to change the number 1 to a 0,1 :confused:

I don't understand that statement.

      hits = hits ++;

That is undefined behavior. Read the documentation on the ++ operator.

Proper code would be one of:

hits++;
hits = hits + 1;

When i push the button i want count's by 0,1, 0,2 0,3 etc..

I have tried this one;

hits = hits + 0,1;

But this was not ok..

Change these three things to increment by .1

//int hits = 0;
float hits = 0;

//hits = hits + 0,1;
hits = hits +.1;

//lcd.print(hits);
lcd.print(hits,1);

The , is not recognized as a decimal point.

Thanks! This works!

If i wanna make an up and down button will this do the job?

//int hits = 0;
float hitsup = 0;
Float hitsdown = 0;

//hits = hits + 0,1;
hitsup = hitsup +.1;
hitsdown = hitsdown -.1;

//lcd.print(hits);
lcd.print(hits,1);

Write some code, try it, and report back what you find.

It’s not working, this is the new code:

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// set up a constant for the tilt switchPin
const int setpointButtonUp = 19;
const int setpointButtonDown = 15;

//int hits = 0;
float hitsup = 0;
float hitsdown = 0;


// variable to hold the value of the switchPin
int switchUpState = 0;
int switchDownState = 0;

// variable to hold previous value of the switchpin
int prevSwitchUpState = 0;
int prevSwitchDownState = 0;

void setup() {
  // set up the number of columns and rows on the LCD
  lcd.begin(16, 2);

  // set up the switch pin as an input
  pinMode(setpointButtonUp, INPUT_PULLUP);
  pinMode(setpointButtonDown, INPUT_PULLUP);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Hit the button");
  lcd.setCursor(0, 1);
  lcd.print("to increment");
}

void loop() {
  // check the status of the switchUp
  switchUpState = digitalRead(setpointButtonUp);

  // compare the switchUpState to its previous state
  if (switchUpState != prevSwitchUpState) {
    if (switchUpState == LOW) {
      hitsup = hitsup + 1;
    }
      // check the status of the switchDown
      switchDownState = digitalRead(setpointButtonDown);

      // compare the switchDownState to its previous state
      if (switchDownState != prevSwitchDownState) {
        if (switchDownState == LOW) {
          hitsdown = hitsdown - 1;

        }

          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Hits:");
          lcd.setCursor(0, 1);
          lcd.print(hitsup, 1);
        }
      }
      // save the current switch state as the last state
      prevSwitchUpState = switchUpState;
      prevSwitchDownState = switchDownState;
    }

Your code is suffering from improper logic due to the intermixing of button reading/variable incrementing and display management. With the display management separated out into a function it is easier to get the bracket location an logic correct.

I assumed you wanted a net count of the up and down counts. I left them as floats as in your latest code, but with the increment of 1. You should choose what you really want to do.

Your choice of variable type – byte, float, int could use some review to make the code as efficient as possible. Do you really want count = x.x with a decimal?

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// set up a constant for the tilt switchPin
const int setpointButtonUp = 19;
const int setpointButtonDown = 15;

//int hits = 0;
float hitsUp = 0; 
float hitsDown = 0;

// variable to hold the value of the switchPin
int switchUpState = 0;
int switchDownState = 0;

// variable to hold previous value of the switchpin
int prevSwitchUpState = 0;
int prevSwitchDownState = 0;

void setup() {
  // set up the number of columns and rows on the LCD
  lcd.begin(16, 2);

  // set up the switch pin as an input
  pinMode(setpointButtonUp, INPUT_PULLUP);
  pinMode(setpointButtonDown, INPUT_PULLUP);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Hit the button");
  lcd.setCursor(0, 1);
  lcd.print("to increment");
}

void loop() {
  // check the status of the switches
  switchUpState = digitalRead(setpointButtonUp);
  switchDownState = digitalRead(setpointButtonDown);

  // compare the switchState to its previous state
  //update count and diplay if changed
  if (switchUpState != prevSwitchUpState) {
    if (switchUpState == LOW) {
      hitsUp = hitsUp + 1;
      updateDisplay();
    }
  }
  prevSwitchUpState = switchUpState;

  if (switchDownState != prevSwitchDownState) {
    if (switchDownState == LOW) {
      hitsDown = hitsDown + 1;
      updateDisplay();
    }
  }
  prevSwitchDownState = switchDownState;
}

  void updateDisplay()
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Hits:");
    lcd.setCursor(0, 1);
    lcd.print(hitsUp - hitsDown, 1); // display net value
  }

Thnx!

This is what i need!

I only changed this;

//hitsUp = hitsUp + 1; hitsUp = hitsUp + .1;

//hitsDown= hitsDown + 1; hitsDown = hitsDown - .1;

The only thing now is that when i push the button it count + 0.2! I think the button speed is to fast, when i push it verry shortly it counts 0.1

It needs something like an delay i guess??

The only thing now is that when i push the button it count + 0.2! I think the button speed is to fast, when i push it verry shortly it counts 0.1

It needs something like an delay i guess??

You are encountering what is as "switch bounce" What you need is called "debounce". Those are some terms for you to Google.

Debounce can be done with hardware using a RC low pass filter on the arduino input, or with software.

There are many software algorithms for debounce. Some use delay and are blocking while others do not. Study the example in the IDE File>examples>02Digital>Debounce.

There is also a very good library called Bounce2 which can be used.