Simple Counter sketch

Hey. I was just wondering how i could make it that for example everytime i press a button the count of hits on the button would be displayed (in serial console not on an LCD). Could somebody help me with this please?

Sincerely, ~Fab

Have you tried anything yourself ?

See the StateChangeDetection example in the IDE to see how to detect when a button [u]becomes[/u] pressed rather than when it [u]is[/u] pressed. Increment a variable when that happens and Serial.print() it

lol i didnt understand one sentence xd sry im kinda new to arduino

Here's an example the change of state thinking UKHeliBob is talking about:

const int pinSwitch = 2;

int
    currSwitch,
    lastSwitch;
unsigned long
    switchCount;
    
void setup() 
{
    pinMode( pinSwitch, INPUT_PULLUP );
    lastSwitch = digitalRead( pinSwitch );
    switchCount = 0;
    Serial.begin(9600);    

}//setup

void loop() 
{
    //read the switch pin...
    currSwitch = digitalRead( pinSwitch );
    //wait 20mS and read it again
    delay(20);
    //if it's the same we assume no bounce...
    if( digitalRead( pinSwitch ) == currSwitch )
    {
        //if the switch is low now but was high then we
        //sensed a keypress
        if( currSwitch == LOW && currSwitch != lastSwitch )
        {
            //bump the count and show it
            switchCount++;
            Serial.print( "Switch count: " ); Serial.println( switchCount );
                        
        }//if
        
        //save this valid state as the last
        lastSwitch = currSwitch;
        
    }//if

}//loop

This simple code debounces the switch to avoid counting the bounces of a mechanical switch as keypresses. It uses delay() to do this. For simple examples like this, delay() is fine for a proof of concept. For more complex code you'll want to avoid it and use other forms of timing so you don't lock the processor into doing one thing for large periods of time.

Okay. Thank you <3
im gonna look over it.
~Fab

Rather than follow Black Fin's example as your solution, try the tutorials.

[u][u]Driving a car[/u][/u] is completely different from knowing [u]how to drive[/u], and [u]knowing the road rules[/u].

You're doomed to long-term failure if you don't understand what's going on around you.