I want to thank everyone for their help. I finished the project. I'm gonna post what I did just in case it might help someone in the future.

So, I modified the original diagram as follows:
- replaced the NE555 timer with an Arduino Nano
- added a SPDT relay
- replaced the IRF510 transistor with BC337 NPN transistor
- added a LM7805 linear voltage regulator
All those steps where taken in order to be able to control two relays simultaneously. The function of the DPDT relay is to switch the output polarity while the role of the SPDT relay is to cut out the power supply to the motor while the switching of the polarity is happening, so the motor is not stressed.


ARDUINO SCRIPT
typedef int (*stateFunctionPtr)(unsigned long millsElapsed, int* alternative);
int first_state(unsigned long millsElapsed, int* alternative)
{
digitalWrite(9, LOW);
digitalWrite(12, LOW);
if (millsElapsed > 180000)
{
return 1;
}
return 0;
}
int second_state(unsigned long millsElapsed, int* alternative)
{
digitalWrite(9, HIGH);
digitalWrite(12, LOW);
if (millsElapsed > 4000)
{
return 2;
}
return 1;
}
int third_state(unsigned long millsElapsed, int* alternative)
{
digitalWrite(9, HIGH);
digitalWrite(12, HIGH);
if (millsElapsed > 4000 && *alternative)
{
*alternative = 0;
return 4;
}
else if (millsElapsed > 4000 && !(*alternative))
{
*alternative = 1;
return 3;
}
return 2;
}
int fourth_state(unsigned long millsElapsed, int* alternative)
{
digitalWrite(9, HIGH);
digitalWrite(12, LOW);
if (millsElapsed > 4000)
{
return 0;
}
return 3;
}
int fifth_state(unsigned long millsElapsed, int *alternative)
{
digitalWrite(9, LOW);
digitalWrite(12, HIGH);
if (millsElapsed > 180000)
{
return 2;
}
return 4;
}
int (*states[5])(unsigned long,int*) = {
first_state,
second_state,
third_state,
fourth_state,
fifth_state
};
int pair = 1;
int state = 0;
unsigned long milsNow = 0;
void setup()
{
pinMode(9, OUTPUT);
pinMode(12, OUTPUT);
}
void loop()
{
int tmp = state;
unsigned long millsElapsed = millis() - milsNow;
state = states[tmp](millsElapsed, &pair);
if (state != tmp)
{
milsNow = millis();
millsElapsed = 0;
}
}