user30387: I'm struggling with getting the button configuration. Simply, I have a LED controlling setup for a button to increase its brightness when pressed. However, I would like to have it setup like this:

When button is pressed. 
change the value by 1, 
wait for 500ms,
Then go fast (with the usual void loop time).

Is it something to do with interrupts? If I just write it like below, it actually (obviously) goes by +1 by itself, even when button is not pressed.
void loop() {
  if(digitalRead(button_pressed) == HIGH) {
    current_state = current_state+1;
    delay(500);
    current_state = current_state+1;
  }
}


user2881: Non-blocking version.
bool old_button_state = LOW;
void loop()
{
    if(digitalRead(button_pressed) == HIGH)
    {
        current_state = current_state+1

        if( old_button_state==LOW )
            delay(500); // long delay
        else
            delay(100); // short delay

        old_button_state = HIGH;
    }
    else
    {
        old_button_state = LOW;
    }
} 


Simplistic version. 
void loop()
{
    if(digitalRead(button_pressed) == HIGH)
    {
        current_state = current_state+1
        delay(500);// long delay

        // repeat while the button is being pressed
        while(digitalRead(button_pressed) == HIGH)
        {
            current_state = current_state+1
            delay(100); //short delay
        }
    }
} 


Ideally you wouldn't just wait for 500ms, but 'break out' of this delay as soon as the button is released.

user28033: long timenow, timelong=500;
int current_state = 0;
void increase_state()
{
    current_state++;
    Serial.println(current_state);
}

void loop()
 {
    if(digitalRead(button_pressed)) //button is pressed
    {
        timenow=millis();   //get current time
        increase_state(); // increase by 1
        while (digitalRead(button_pressed)) //check if the button is still pressed
            {
                //rapidly increase "current_state" by 1 if button is pressed more than 500ms with interval of 50ms.
                if ((millis()-timenow)>timelong) 
                {
                increase_state();   
                delay(50);
                }
            }
    }
    delay(500)
 } 

The increment of current_state is always by 1. If button is pressed, it will always increase by 1, but if it's not a long press, you have to wait another 500ms to increase it values again.

30387: <p>I'm struggling with getting the button configuration. Simply, I have a LED controlling setup for a button to increase its brightness when pressed. However, I would like to have it setup like this:</p>

<ol>
<li>When button is pressed. </li>
<li>change the value by 1, </li>
<li>wait for 500ms,</li>
<li>Then go fast (with the usual void loop time).</li>
</ol>

<p>Is it something to do with interrupts? If I just write it like below, it actually (obviously) goes by +1 by itself, even when button is not pressed.</p>

<pre><code>void loop() {
  if(digitalRead(button_pressed) == HIGH) {
    current_state = current_state+1;
    delay(500);
    current_state = current_state+1;
  }
}
</code></pre>
 2881: <p>Non-blocking version.</p>

<pre class="lang-cpp prettyprint-override"><code>bool old_button_state = LOW;
void loop()
{
    if(digitalRead(button_pressed) == HIGH)
    {
        current_state = current_state+1

        if( old_button_state==LOW )
            delay(500); // long delay
        else
            delay(100); // short delay

        old_button_state = HIGH;
    }
    else
    {
        old_button_state = LOW;
    }
} 
</code></pre>

<p><strike>
Simplistic version. </p>

<pre class="lang-cpp prettyprint-override"><code>void loop()
{
    if(digitalRead(button_pressed) == HIGH)
    {
        current_state = current_state+1
        delay(500);// long delay

        // repeat while the button is being pressed
        while(digitalRead(button_pressed) == HIGH)
        {
            current_state = current_state+1
            delay(100); //short delay
        }
    }
} 
</code></pre>

<p></strike></p>

<p>Ideally you wouldn't just wait for 500ms, but 'break out' of this delay as soon as the button is released.</p>
 28033: <pre><code>long timenow, timelong=500;
int current_state = 0;
void increase_state()
{
    current_state++;
    Serial.println(current_state);
}

void loop()
 {
    if(digitalRead(button_pressed)) //button is pressed
    {
        timenow=millis();   //get current time
        increase_state(); // increase by 1
        while (digitalRead(button_pressed)) //check if the button is still pressed
            {
                //rapidly increase "current_state" by 1 if button is pressed more than 500ms with interval of 50ms.
                if ((millis()-timenow)&gt;timelong) 
                {
                increase_state();   
                delay(50);
                }
            }
    }
    delay(500)
 } 
</code></pre>

<p>The increment of <code>current_state</code> is always by 1. If button is pressed, it will always increase by 1, but if it's not a long press, you have to wait another 500ms to increase it values again.</p>
