I am looking for a way to close a solenoid after a period of time.

I am using a solenoid for a lock, and need it to start a timer when opened, so that it will close when that timer expires. This is the code I am using, without any sort of timer.

#define lock 12
#define sound 3 
boolean trig = true;
boolean state = true;


void setup() {
  // put your setup code here, to run once:
  pinMode(lock, OUTPUT);
  pinMode(sound, INPUT);
  
}

void loop() {
  // Checks if the button state has change
 trig = digitalRead(sound);
  if (trig == false && state == false)
  
  {
   // Turns the Lock on    
    digitalWrite(lock, HIGH);  
  state = true;
  delay(900);
  } 
  else if (trig == false && state == true)
  {
    // Turns the Lock off
    digitalWrite(lock, LOW);
    state = false;
    delay(900);
}
}

The beginner's guide to millis() for timing may be of help.

Never ever use delay() !
Well, only use it if you know why you shouldn’t.

Follow @groundFungus suggestion.

Start now to get into the habit of commenting your code.

How about:

const int pinLock = 12;
const int pinSound = 3;

const int STATE_LOCKED = 0;
const int STATE_UNLOCKED = 1;
const int STATE_TRANSITION = 2;

void setup() 
{
    // put your setup code here, to run once:
    pinMode( pinLock, OUTPUT );
    pinMode( pinLock, HIGH );       //turn lock on *matches initial state
    
    pinMode( pinSound, INPUT );
    
}//setup


void loop() 
{
    OperateLock();
        
}//loop

void OperateLock( void )
{
    static byte
        stateNextState,
        stateLockState = STATE_LOCKED;
    static unsigned long
        timeLockTimer = 0,
        timeThen = 0;
            
    switch( stateLockState )
    {
        case    STATE_LOCKED:
            if( digitalRead(pinSound) == LOW )
            {
                digitalWrite( pinLock, LOW );       //unlock
                stateNextState = STATE_UNLOCKED;
                stateLockState = STATE_TRANSITION;
                timeThen = millis();
                timeLockTimer = 900;                //time to stay unlocked
                
            }//if
            
        break;

        case    STATE_UNLOCKED:
                digitalWrite( pinLock, HIGH );       //re-lock
                
                stateNextState = STATE_LOCKED;
                stateLockState = STATE_TRANSITION;
                timeThen = millis();
                timeLockTimer = 1500;                //time before allowing unlock again
                
        break;

        case    STATE_TRANSITION:
            if( (millis() - timeThen) < timeLockTimer )
                return;

            stateLockState = stateNextState;
            
        break;
    
    }//switch
    
}//OperateLock

larryd:
Never ever use delay() !

If you want to do nothing for a certain amount of time, delay() is the easiest solution.
What's wrong with that?
I feel the it has become a tradition in this forum to advise against using delay() for no good reason.

Larry did say

Well, only use it if you know why you shouldn't.

Which is good advice.

couka:
If you want to do nothing for a certain amount of time, delay() is the easiest solution.
What's wrong with that?
I feel the it has become a tradition in this forum to advise against using delay() for no good reason.

Advising against use of delay is just shorthand for a much longer lecture that ends with:
"So as you can see, delay is best avoided until you've got more experience"

Of course there are appropriate uses for it, but once you've seen fifty threads where someone is struggling with a non-responsive program because their sketch has tens of seconds of delays in it, that shorthand comes in handy.

Also, unless you're in the unusual position of having rock solid requirements, use of delay can mean a rewrite when you decide that the system would be better if it responded to a couple of buttons as well as what it does now.