Need help in choosing the right code for my project

Hello everyone, I'm Barak and I'm pretty new in programming and Arduino.
I'm asking for help with the code, I'm having trouble choosing the right timing code and coding it.
This is the code I make:

const int buttonPin = 2;       // the number of the pushbutton pin
const int RELAYPIN =  8;      // the number of the RELAY pin
const int CW = 9;            // the number of the CW pin
const int CCW =  10;        // the number of the CCW pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  pinMode(CW, OUTPUT);// initialize the CW pin as an output:
  pinMode(CCW, OUTPUT);// initialize the CCW pin as an output:
  pinMode(RELAYPIN, OUTPUT); // initialize the Relay pin as an output:
  pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    digitalWrite(CW,HIGH); //Motor runs clockwise//
    digitalWrite(CCW, LOW); //Motor stops//
    digitalWrite(RELAYPIN, LOW);//Relay On//
      } else {
    digitalWrite(RELAYPIN, HIGH);//RELAY Off//
    digitalWrite(CCW, HIGH);//Motor runs counter-clockwise//
    digitalWrite(CCW, LOW); //Motor stops//
  }
}

What I need in this cod, is actually when I press the ON button, I want the CW relay to go into operation for about 20 seconds, it will shut itself automatically at the end of 20 seconds, and it will ON RELAYPIN relay and keep It ON.

When leaving the button - OFF mode, Relay RELAYPIN going OFF , and turn on the CCW relay for 20 seconds, after that CCW Relay go OFF.

Thanks for the helpers...

barak96:
What I need in this cod, is actually when I press the ON button, I want the CW relay to go into operation for about 20 seconds, it will shut itself automatically at the end of 20 seconds, and it will ON RELAYPIN relay and keep It ON.

When leaving the button - OFF mode, Relay RELAYPIN going OFF , and turn on the CCW relay for 20 seconds, after that CCW Relay go OFF.

Can you clarify what you mean by 'leaving the button'? Do you mean the exact point when the user stops pressing it? Or just any time that it is not pressed?
What happens if the user presses the button during the CW ON time or the CCW ON time?

Or do you mean?
When user presses the button
CW goes ON
Wait 20 seconds
CW goes OFF and RELAY goes ON
CCW goes ON
Wait 20 seconds
CCW goes OFF and RELAY goes OFF
Nothing will happen until the user presses the button again

Ultimately you need to be able to fully describe all the states of your system in terms of CW, CCW, RELAY and have clear rules on when the system should change from one state to another (e.g. after a certain time, when an input changes etc). Then it will be a lot easier to help.
You have tried to do this but at the moment it is not very clear (at least not to me).

In the meantime you could also google 'State Machine' and also read this forum post.

Look up for function millis().
You can assign a variable with the initial millis() when the button is pressed, and simply millis() to check if:

millis() - initial_millis >= time

if so, turn off whatever you want, since it has passed the time.

You'll also probably need a boolean control variable containing information if the button was pressed already or not.
Like:
if button was pressed -> pressed = true;
if pressed is true -> register initial_millis, turn on whatever you want AND check if 20 seconds (20*1000 ms) have passed: if so -> turn of whatever you want and turn pressed false.

can you show me this in a code?

barak96:
can you show me this in a code?

It's in your IDE examples, BlinkWithoutDelay.