LED should blink certain number of times

I have one LED as an output and Taster as input. I want to make program where LED blinks 3 times if taster is pressed and then it stops BUT it should do exactly same if taster stays HIGH for long period of time, now led will blink as long as taster is pressed (HIGH), but i want to make it blink only 3 times no matter if tasters stays high for a hour or one second. I understand how loop functions and why LED blinks constantly if taster is high for longer period of time but is there a way to make it with arduino. This is my code

int led1 = 6;
int out1 = 3;

void setup() {
pinMode(led1, OUTPUT);
pinMode(out1, INPUT);
}

void loop() {
if (digitalRead(out1) == HIGH)
{
digitalWrite(led1, HIGH);
delay(500);
digitalWrite(led1, LOW);
delay(500);
digitalWrite(led1, HIGH);
delay(500);
digitalWrite(led1, LOW);
delay(500);
digitalWrite(led1, HIGH);
delay(500);
digitalWrite(led1, LOW);
delay(500);
}
else{
digitalWrite(led1,LOW);
}

}

How is the input wired ?
Do you have a pulldown resistor in place to keep it LOW except when the input is explicitly taken HIGH or is it floating at an unknown (possibly HIGH) voltage ?

Have a look at the state change detect, or the switch examples, either of which will show you how to detect a new press of your button. Then put all your blinky stuff in a function, and call that function only when a new press is is detected.

I'm not seeing "Taster" in your code, just a pin called "out1", which confusingly is an INPUT.

anthonyHope:
I'm not seeing "Taster" in your code, just a pin called "out1", which confusingly is an INPUT.

It is push button and it can become High randomly for a second or couple of hours doesnt matter

If you want to make the LED blink 3 times on demand then a neater way is to put the code in a for loop inside a function and call it when you need the LED to blink

void blink3()
{
  for (int count = 0; count < 3; count++)
  {
    digitalWrite(led1, HIGH);
    delay(500);
    digitalWrite(led1, LOW);
    delay(500);
  }
}

Elaz1234:
It is push button and it can become High randomly for a second or couple of hours doesnt matter

Yeah I got that, but it's not called Taster in the code, so why bother talking about Taster in your text. And one day you will scan down the program looking for a button and your eye will jump over out1 because your brain will tell you it's an output.

But that's really the minor by-the-way point of what I was saying: more important, you need to see a "new" press not an "existing" press.

anthonyHope:
But that's really the minor by-the-way point of what I was saying: more important, you need to see a "new" press not an "existing" press.

I understand what you're saying, i need to make it detect new push of a button. I don't know how that code will look like but i'll find a way hopefully.

Btw i mistook output for input and called it ''out1'' instead of ''input1'' or something and didn't even notice, my bad.

Elaz1234:
i need to make it detect new push of a button. I don’t know how that code will look like but i’ll find a way hopefully.

I gave you a couple of links earlier.

Maybe try that. It is non-blocking without delay() and also allows variable LED on and off times.

const byte pinMomentarySwitch = A0;
bool momentarySwitchTriggered = false;
bool lastMomentarySwitchState = HIGH;

const byte pinLED = 13;
bool stateLED = LOW;
unsigned long timeNowLED = 0;
unsigned long timeIntervalLED = 0;

void setup()
{
  pinMode(pinLED, OUTPUT);
  pinMode (pinMomentarySwitch, INPUT_PULLUP);
}

void loop()
{
  readMomentarySwitch();

  if (momentarySwitchTriggered == true)
  {
    flash(250, 100, 3);
  }
}

void flash(int timeon, int timeoff, byte flashes)
{
  static byte counter = 0;

  if (millis() - timeNowLED > timeIntervalLED)
  {
    timeNowLED = millis();

    if (stateLED == LOW)
    {
      timeIntervalLED = timeon;
      stateLED = HIGH;
      digitalWrite(pinLED, stateLED);
    }

    else
    {
      timeIntervalLED = timeoff;
      stateLED = LOW;
      counter++;
      digitalWrite(pinLED, stateLED);
    }
  }

  if (counter >= flashes)
  {
    counter = 0;
    momentarySwitchTriggered = false;

    return;
  }
}

void readMomentarySwitch() // Switch simply hardware-debounced with 1uf capacitor
{
  byte momentarySwitchState = digitalRead (pinMomentarySwitch);

  if (momentarySwitchState != lastMomentarySwitchState)
  {
    lastMomentarySwitchState = momentarySwitchState;

    if (momentarySwitchState == LOW)
    {
      momentarySwitchTriggered = true;
    }
  }

  return;
}