Help required to write code for Plastic cutter machine

Input- NPN/NO proximity sensor (12V)
Output- Motor Forward, Motor Reverse, Alarm

Sensor will sense tooth disk fitted on Motor shaft. When tooth disk is rotating(520 tooths per minute) then motor will rotate in FWD direction,
When the proxymity sensor input doesn't sense, motor will rotate in reverse direction for 15 second, NO Effect of input sensed on Output.
After 15seconds over, motor will rotate in Forward direction. If suppose in span of 40 seconds motor rotates 2 times in reverse direction, then Forward & Reverse relay output will be Zero. Third relay Output Alarm is ON..

The sketch is as below-

int sensor = 4;
int Forward = 8;
int Reverse = 12;
int Alarm = 13;
int Count;
//unsigned long T1;
//unsigned long Time;

void setup()

void loop()

if (digitalRead(4)==HIGH)
digitalWrite(8,HIGH); //Forward Motion ON
digitalWrite(12,LOW); // REVERSE motion Zero
digitalWrite(13,LOW); // NO ALARM
Count = 0;
else if (digitalRead(4)==LOW)
digitalWrite(8,LOW); // Forward motion Zero
digitalWrite(13,LOW); // NO ALARM
digitalWrite(12,HIGH); // Reverse motion ON
delay(15000); // 15 Sec. HOW TO LOOP IT SO, THAT OUTPUT WILL REMAIN UNCHANGED for 15Seconds.


//if (digitalRead(4)==LOW)
// digitalWrite(8,LOW); // Forward motion Zero
// digitalWrite(12,HIGH); // Reverse motion ON
// digitalWrite(13,LOW); // NO ALARM
// Reverse direction for 15 Sec

// Serial.print(Reverse);

// else
// {

 //       Count = Count + 1;
// }

if (Count>2)
digitalWrite(8,LOW); // Forward motion Zero
digitalWrite(12,LOW); // Reverse motion Zero
digitalWrite(13,HIGH); // Alarm relay activate.


For me to write code for such a project I would have to either create a flow chart of at a minimum pseudo code (i.e. kinda like simplified code with no syntax requirements).

To answer your question. For timing you should use the millis() counter. Each count is 1 millisecond. It is not resettable, it just runs and runs.

Basically at the beginning you set a variable (unsigned long) to the millis value**. Then were you have your delays you test the above value to see if it minus the current millis is => your delay.

** many folks call this the oldmillis
so ... where you have delay 15000

if oldmillis-millis() >= 15000 then
do something

Thank you John for reply.