Easy counter

Hi

I think it's very simple problem.

I want a counter that's counts up from 0 to 3 and then Counts down from 3 to 0 then i push the button up or down.

I Think its wrong in the IF(….)

const int buttonPinup = 2; // the number of the pushbutton pin
const int buttonPindown = 3; // the number of the pushbutton pin

// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int count = 0;

void setup() {
Serial.begin(115200);
pinMode(buttonPinup, INPUT);
pinMode(buttonPindown, INPUT);
}

void loop() {

// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPinup);
buttonState2 = digitalRead(buttonPindown);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
if ( count >= 0 and count == 2 ); {
count = count +1;
delay(500);
Serial.println(count);
}

}
else if (buttonState2 == HIGH) {
if (count >= 0 and count <= 2); {
count = count -1;
delay(500);
Serial.println(count);

} }}

There's a semicolon here that should not be:

    if ( count >= 0 and count == 2 ); {

Same thing further down too.

Also, if you're checking for count to be two, why do you need another check that it's greater than or equal to zero?

I see i wrote ==2 and <=2 but nothing change.
Then I test it dont stop counting with number 3 or 0.

I change to:

if ( count < 3 ); {
count = count +1;
delay(500);
Serial.println(count);

What should i wrote? It dont stop with number 3!

Your if statement is still broken, it has no effect at all because of the semicolon.

So easy!!!

Thanks very much!!!

Are where any easy way that you must release the button between every up/down counter?

plutten:
Are where any easy way that you must release the button between every up/down counter?

See if INPUT_PULLUP makes any difference.

Thanks but sorry, it didn´t help.

Another way to do it?

You need to increment your count when the button BECOMES pressed, not when it IS pressed.

Look at the StateChangeDetection example in the IDE.