Button slow to fast

Hi
How to switch from delay 300 to 30 when button is pressed longer than 2 sec ?

void loop()
{
  if (digitalRead(PB6) == HIGH)
  {
    if (i < 255)
    {
      i++;//if pin PB3 is pressed and the duty ratio value is less than 255
      analogWrite(PA3, i);  // analogWrite values from 0 to 255
      delay(300);
     // delay(30);
    }
  }
  if (digitalRead(PB5) == HIGH)
  {
    if (i > 0)
    {
      i--;// if pin PB5 is pressed and the duty ratio value is greater than 0
      analogWrite(PA3, i);  // analogWrite values from 0 to 255
      delay(300);
      //delay(30);
    }
  }

You might want to ask or neighbors of Snippets R Us. Or see MCVE.

But to the problem, you can use millis() to see how long a button is pressed. BUT, if you use (long) delay()'s (like you do) the response to a button press will always be shitty. Switch to using millis() for the delays as well and you're fine. See Blink without delay.

And a pro tip, use proper variable names for the pin names...

void loop()
{
  if (digitalRead(PB6) == HIGH)
  {
    if (i < 255)
    {
      i++;//if pin PB3 is pressed and the duty ratio value is less than 255
      analogWrite(PA3, i);  // analogWrite values from 0 to 255
      delay(30);
//delay(300);
    }
  }
  if (digitalRead(PB5) == HIGH)
  {
    if (i > 0)
    {
      i--;// if pin PB5 is pressed and the duty ratio value is greater than 0
      analogWrite(PA3, i);  // analogWrite values from 0 to 255
      delay(30);
//delay(300);
    }
  }
  Serial.println(i);
}

and I find this

 if (button_state == HIGH) { 
    delay_value = 100; 
  } else { 
    delay_value = 1000; 
  }

I need somewhere put 2sec

ted:
I need somewhere put 2sec

Right now, there is no place to do that... And with delay()'s it's impossible :wink:

Did you actually read the rest of my comment?

And which board are you using?

The name of the pins has nathing to do with the problem.
STM32

I know, that was curiosity (and amazement about not using self explaining names), using delay does. But you don't seem to like that answer so you just fricking ignore everything I see about it :wink:

This topic has precious little to do with General Electronics.
@Ted - please think more carefully before posting.

septillion:
I know, that was curiosity (and amazement about not using self explaining names), using delay does. But you don't seem to like that answer so you just fricking ignore everything I see about it :wink:

Thanks

If what you want to do is create some sort of repeat rate on the buttons, you could use the simple/lazy way out and use a library like TDuino:

#include <TDuino.h>

#define BUTTON_UP 6
#define BUTTON_DOWN 5
#define BUTTON_COUNT 2

const byte BUTTON_PINS[BUTTON_COUNT] = {BUTTON_UP, BUTTON_DOWN};

TButton buttons[BUTTON_COUNT];

void buttonCallback(byte pin, int state)
{
  if (pin == BUTTON_UP)
  {
    if (i >= 255) return;
    i++;
  }
  else
  {
    if (i <= 0) return;
    i--;
  }
  analogWrite(A3, i);
}

void setup()
{
  for (byte i = 0; i < BUTTON_COUNT; i++)
  {
    buttons[i].attach(BUTTON_PINS[i]);
    buttons[i].onPress(buttonCallback);
    buttons[i].setRepeat(300, 30);
  }
}

void loop()
{
  for (byte i = 0; i < BUTTON_COUNT; i++) buttons[i].loop();
}

Should do more or less what you want.

Thanks Danois90
It is look like what I need.

That's great, but now mix delay()s and the result will be not as responsive as you would expect/want...

septillion:
That's great, but now mix delay()s and the result will be not as responsive as you would expect/want...

TDuino does not work with delays but there are options like TTimer available to avoid them.

Danois90:
TDuino does not work with delays but there are options like TTimer available to avoid them.

I like TDuino, thanks for pointing that

Danois90:
TDuino does not work with delays but there are options like TTimer available to avoid them.

I said mixing it. Aka to do what to OP did. TDuino may not use them but mixing them with it will result in crap again...

I'm still in favor to make the second chapter in every Arduino (after Chapter 1: Blink) called "Chapter 2: Now NEVER use delay() again!" :smiley:

septillion:
I said mixing it.

Yes, and I meant that TDuino does not work with / will break if used with delays, so we do not disagree :slight_smile:

@Danois90, sorry! Small misunderstanding :slight_smile: