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);
}
}
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);
}
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
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