Help assigning different durations for LED and SSR

I am running the following code to strobe an LED and fire off an SSR on a mini arduino pro. I would like to assign the duration separately, so the SSR disconnects after 2 seconds, but the LED flashes for 4 seconds.

const int ledPin = 12;      // led connected to digital pin 13
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 5;  // threshold value to decide when the detected sound is a knock or not
const int ssrPin = 11;


// these variables will change:
int sensorReading = 0;      // variable to store the value read from the sensor pin
int ledState = HIGH;         // variable used to store the last LED status, to toggle the light
int timer = 20;

void setup() {
 pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
 pinMode(ssrPin, OUTPUT);
 pinMode(knockSensor, INPUT);
 Serial.begin(9600);      // use the serial port
}

void loop() {
  // read the sensor and store it in the variable sensorReading:
  sensorReading = analogRead(knockSensor);
    // if the sensor reading is greater than the threshold:
    
if (sensorReading >= threshold) {
   unsigned long startTime = millis();
   
   do {
     digitalWrite (ssrPin, HIGH);
     digitalWrite(ledPin , !digitalRead(ledPin ));
     delay(timer);
     digitalWrite(ssrPin, LOW);
   }
  
   while (millis()-  startTime < 4000);
}

}

Yes, you must study and understand "timing two things" - it is a frequent recurring subject.

Your code is reasonbly nice and ordered, so I'll give you the Quick-Fix.

Change the line with the digitalWrite(ssrPin, LOW);to be

if ( millis()-  startTime > 2000) digitalWrite(ssrPin, LOW);

It is a quick fix, so it repeatedly sets the SSR to low after 2 seconds to 4 seconds from the trigger time, but that only wastes a few CPU cycles, it has no real effect on the port.

If you want to extend the program with more times and intervals it should be restructured - get insiration from Two LED and Several things

Thank you for your reply.

Here is what I have now, but the the SSR and LED still stay on for the same amount of time. Found the ssr needs to run for a second, or less, but want the led to run 4 seconds.

do {
     digitalWrite (ssrPin, HIGH);
     digitalWrite(ledPin , !digitalRead(ledPin ));
     delay(timer);
     if ( millis()-  startTime > 1000) digitalWrite(ssrPin, LOW);
     // digitalWrite(ssrPin, LOW);
   }
  
   while (millis()-  startTime < 4000);
while (millis()-  startTime < 4000);

Is just as bad as

delay(4000);

Nothing gets done for 4 seconds.

You need a state machine:-
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html