serial.print junk while counting button presses

Hi,

Well, certainly not quite junk, but useless all the same. I've two buttons and am using one to increment and one to decrement a buttonCount variable each time they are pressed. The idea is it works like a volume display. The result is written to the serial monitor.
I want it to display '1' and then '2' as I press the up button twice, then '1' as I press the down button once.
The fault is not quite uniform each time, but it broadly goes like this -
A whole continuing string of zeros, then 'up' a whole string of twos, then 'up' a whole string of fours then 'up' a whole string of sixes then 'down' a whole string of fives then 'down' a whole string of threes etc.

I think there are two problems -
a) continually writes buttonCount when it should do it once only after a state change
b) counts in twos - no idea.

I would be very grateful for some help

Regards,

Al

The code is here -

//declarations

//constant variables

const int upButton = 7;
const int downButton = 8;

//variable variables
//buttonCount will later become my speed variable
int buttonCount = 0;
//quicker or slower
int upButtonState = 0;
//quicker or slower
int downButtonState = 0;
//last time through the loop,for comparison
int lastButtonState = 0;

void setup()
{
  // put your setup code here, to run once:
  pinMode (upButton, INPUT);
  pinMode (downButton, INPUT);
  Serial.begin(9600);
}

void loop()
{
  // put your main code here, to run repeatedly:

  //first do the upButtonState checking
  upButtonState = digitalRead(upButton);
  if (upButtonState != lastButtonState)
  {
    if (upButtonState == HIGH)
    {
      buttonCount ++;
    }
    delay (50);
  }
  lastButtonState = upButtonState;
  Serial.print(buttonCount);

  //now do the downButtonState checking
  downButtonState = digitalRead(downButton);
  if (downButtonState != lastButtonState)
    // if they are different, now need to decrement the buttonCount
  {
    if (downButtonState == HIGH)
    {
      buttonCount --;
    }
    delay (50);
  }
  lastButtonState = downButtonState;
  Serial.print(buttonCount);
}

Your Serial.print(buttonCount) is outside the scope of the "if (upButtonState != lastButtonState)" so naturally it prints every time round the loop.

Steve

Do you have a pull-down resistor on the buttons? If not, then the inputs will be reading noise on the pins.

Are you sure it is counting in twos? I mean - if the button is bouncing or there is noise, there might be a '3' buried in there between the '2' and '4' but it's scrolling past to quickly for you to read it.

if(lastButtonCount != buttonCount) {
  Serial.print(buttonCount);
  lastButtonCount = buttonCount;
}

Hi Slipstick and Paul, yes think you’re both right.
Fixing the scope sorted the endless iteration, and slowing my finger down stops the double no’s.
I’ve some new switches coming to see if they are snappier, ie capture the state change more cleanly.
What value of resister do you suggest? I’m using 1K at present.
Regards,
Al

Physwiz:
I’ve some new switches coming to see if they are snappier, ie capture the state change more cleanly.

The usual method is to add debouncing code. Essentially, if the button changes state, then you ignore any other changes for - say - 50ms.

byte buttonState;
uint32_t buttonChangeTime;

loop() {

  if(millis() - buttonChangeTime >= 50) {
    byte buttonStateNow = digitalRead(pin);
    if(buttonStateNow != buttonState) {
      buttonChangeTime = millis();
      buttonState = buttonStateNow;
    }
  }

  // and continue on from here;
}

What value of resister do you suggest? I’m using 1K at present.

The arduino has an internal pull-up resistor of (I think) 5k. You use it by selecting pinMode INPUT_PULLUP. This means that rather than your button going between the pin and 5v, the button shorts the pin to ground. This is a useful way to do things because ground is usually not very far away, especially if your project is in a metal box.

This means that your button is pressed when it is LOW, rather than when it is HIGH.

But if you already have a pull-down resistor, then 1k is fine, and your problem is not noise.

PaulMurrayCbr - thanks for yiour help :slight_smile: