Pin timer

Is there any way to set a pin high for a set amount of time? I mean if I push a button to set a pin high, then it stays high for a set amount of time and then goes low again....

Simple way

digitalWrite(thePin, HIGH);
delay(1000);  //1 second as an example
digitalWrite(thePin, LOW);

Try it and come back if/when you spot the problems arising from using delay()

Thank you, I tried that, but it only puts the pin high and low with 1 sec intevals. How can I get the button to start it. If I push the button once, the pin is set high for the set time, and then turns low until the button is pushed again?

By learning how to detect a button press... Hint, open the state change detect sketch.

And pro tip, you want to drop delay() than and use millis()

frodv:
Thank you, I tried that, but it only puts the pin high and low with 1 sec intevals. How can I get the button to start it. If I push the button once, the pin is set high for the set time, and then turns low until the button is pushed again?

Use a boolean variable to determine whether the button has been pressed (ie has changed state) to start/stop flashing, then use it to test whether the code should be run or not

if (turnOnFlashing == true)
  {
    //code here to turn the LED on and off
  }

As I hinted in reply #1, using delay() will cause problems, hence the suggestion to use millis() for timing. I suggest that you get the version using delay() working first then consider a version using millis()

frodv:
Is there any way to set a pin high for a set amount of time? I mean if I push a button to set a pin high, then it stays high for a set amount of time and then goes low again…

Yes, it’s just a matter of programming logic in the code.

In my code the loop function would read something like this:

void loop()
{
  static unsigned long buttonPressTime;
  if (buttonPtressed()) buttonPressTime=millis();
  digitalWrite(buttonPin,(millis()-buttonPressTime)<amountOfTime);    
}

In addition, you’d need a buttonPressed() function to return a true value if a state change is detected from state “button released” to “button pressed” and false otherwise.
A full program should not be much longer than the code snippet above.

I don’t think that the OP wants the button pin to stay high for the period but rather another pin.

digitalWrite(buttonPin,(millis()-buttonPressTime<amountOfTime);

I fear that this will confuse the OP and would be better implemented initially as an if/else

if (millis() - buttonPressTime < amountOfTime)
{
  digitalWrite(outputPin, HIGH);
}
else
{
    digitalWrite(outputPin, LOW);
}

This is more verbose but may be easier to understand as it is more like normal language.

UKHeliBob:
I don't think that the OP wants the button pin to stay high for the period but rather another pin.

Good point!

The OP possibly wants to have two pins, represented by variables or constants buttonPin and ledPin.