incrementing question, while loop?

Hey guys, quick question. Do I need to use a while loop to increment? I have a button, when pressed it = 1, when released it = 0. I want to make it so when I press it, it counts up from 1 until I let go. int button = 2 if (button > 0) { button++; }

but this doesn't seem to work. I only get it give me a value of 1. What am I missing? Thanks.

You need to post all the code if you want an answer. The code you posted will do what you said so the problem is outside what you posted. Use code tags next time. Read the how to use this forum sticky.

SteveRogers: Hey guys, quick question. Do I need to use a while loop to increment? I have a button, when pressed it = 1, when released it = 0. I want to make it so when I press it, it counts up from 1 until I let go. int button = 2 if (button > 0) { button++; }

but this doesn't seem to work. I only get it give me a value of 1. What am I missing? Thanks.

U need another variable to count.

const int button = 2; int buttonState = 0; int count = 0; void setup() { pinMode(button, INPUT); } void loop(){ buttonState = digitalRead(button); if (buttonState == HIGH) count++; }

Wrong all over!

What you have posted, is code that increments an integer variable called "button".

Incidentally, get into the habit of always posting code in a "code" window using the "[ # ]" icon to set up the tags.

What you suggested so far starts the variable at 2, checks to see whether this is greater than zero - which it is - and increments it.

Now I suspect you meant:

const int buttonpin = 2;
int button;
void setup() {
button = 0;
...
}

void loop() {
...
     if (digitalRead(buttonpin) )
     {
      button++;
     }
...
}

But even so, there is a lot more to this as that code on its own would increment hundreds of thousands of times per second. :D

That is true, that explains why it is incrementing so fast. Thanks!

How would I apply that to a variable on it's own? If button were just the name of the variable.

I must be missing something. it still prints a 1.

SteveRogers: I must be missing something. it still prints a 1.

You are missing posting your **** code.

You're quite a long way away from counting button presses - I suggest you start with the State Change Detection example sketch to see how to detect when a button press occurs, then it only needs a simple change to increment a counter at each button press.