PIR motion sensor and DC motor programming

Hi, I am doing a project for the school and I cannot figure out how to program a software. Basically, I need a PIR motion sensor to detect movement first, but not activate the DC motor yet, it has to start a timer and after a certain amount of time (let's say 15 seconds) check again if it still detects a motion. If yes, then reset the timer and keep the motor LOW, if it doesn't detect any motion, then the motor has to activate for a second and then stop.

If any further explanation of the project is needed, I can provide it.

This is urgent!

This is urgent!

Then you best get started.

Reading a PIR sensor is trivial. Turning a pin on when motion is detected is trivial. Waiting to see if the motion detected state is still true 15 seconds later is not hard.

We expect YOU to try stuff, first.

I already did most of the stuff, I did my hardware part and tried really hard on working with software, but no matter how hard i tried, it did not go anywhere.

this is a sample of my code

const int CW = 7; //clockwise
const int CCW = 8; //counter clockwise
const int pirPower = 13; //Motion detector power supply pin
const int pirIn = 12; //Motion detector input pin
boolean pirDetected = false;
unsigned long motionTime;
int interval = 15000;
int previousMillis = 1;

void setup() {
Serial.begin(9600);
pinMode(CW, OUTPUT);
pinMode(CCW, OUTPUT);
pinMode(pirPower, OUTPUT);
pinMode(pirIn, INPUT);
digitalWrite(pirPower, HIGH);
}

void loop() {
int value = digitalRead(pirIn);
while (value == HIGH)
{
motionTime = millis();
digitalWrite(CW, pirDetected);
digitalWrite(CCW, pirDetected);
}
if(motionTime > millis() + 15000 && value == HIGH)
{
if ((motionTime - previousMillis) >= interval)
{
digitalWrite(CW, LOW);
digitalWrite(CCW, LOW);
}
}
else if(motionTime > millis() + 15000 && value == LOW)
{
digitalWrite(CW,HIGH); //Motor runs clockwise//
delay(500); //for 1 second//
digitalWrite(CW, LOW); //Motor stops//
digitalWrite(CCW, HIGH);//Motor runs counter-clockwise//
delay(200); //For 1 second//
digitalWrite(CCW, LOW);
delay(500);
digitalWrite(CW, pirDetected);
digitalWrite(CCW, pirDetected);//Motor stops//
}

}

but no matter how hard i tried, it did not go anywhere.

The code you posted incorrectly does something. Feel free to tell us what it actually does.

You expect the code to do something. Feel free to tell us what you expect.

    int value = digitalRead(pirIn);
   while (value == HIGH)
   {
      motionTime = millis();
      digitalWrite(CW, pirDetected);
      digitalWrite(CCW, pirDetected);
   }

Once value is assigned a value of HIGH, that while loop will never end. It is NOT sufficient to read the pin once.

In the piece of code, I wanted to start a timer to check the motion later on in the code. I expected while loop to only activate when the value is HIGH, but when it is LOW, I expected it to do the other part of the coding. Other way, I am not certain how to do it.

wretchetry:
In the piece of code, I wanted to start a timer to check the motion later on in the code. I expected while loop to only activate when the value is HIGH, but when it is LOW, I expected it to do the other part of the coding. Other way, I am not certain how to do it.

By other part of coding i meant this part

if(motionTime > millis() + 15000 && value == HIGH)
{
if ((motionTime - previousMillis) >= interval)
{
digitalWrite(CW, LOW);
digitalWrite(CCW, LOW);
}
}
else if(motionTime > millis() + 15000 && value == LOW)
{
digitalWrite(CW,HIGH); //Motor runs clockwise//
delay(500); //for 1 second//
digitalWrite(CW, LOW); //Motor stops//
digitalWrite(CCW, HIGH);//Motor runs counter-clockwise//
delay(200); //For 1 second//
digitalWrite(CCW, LOW);
delay(500);
digitalWrite(CW, pirDetected);
digitalWrite(CCW, pirDetected);//Motor stops//
}

}

Other way, I am not certain how to do it.

You need to detect when the state of the pin changed. There is an example for that.

When the pin becomes HIGH (or becomes LOW, whichever means motion now detected), record the time, using millis().

When the pin becomes LOW (or becomes HIGH, whichever means motion no longer detected), clear the time.

Periodically, compare now (using millis() to then. If motion has been happening for long enough, do whatever needs doing.

There will be NO while statements in your finished code. If you find yourself trying to use one, STOP.

int value = digitalRead(pirIn);

if(value == HIGH)
{
motionTime = millis();
}

if(motionTime > millis() + 15000 && value == HIGH)
{
if ((motionTime - previousMillis) >= interval)
{
digitalWrite(CW, LOW);
digitalWrite(CCW, LOW);
}
}
else if(motionTime > millis() + 15000 && value == LOW)
{
digitalWrite(CW,HIGH); //Motor runs clockwise//
delay(500); //for 1 second//
digitalWrite(CW, LOW); //Motor stops//
digitalWrite(CCW, HIGH);//Motor runs counter-clockwise//
delay(200); //For 1 second//
digitalWrite(CCW, LOW);
delay(500);
digitalWrite(CW, LOW);
digitalWrite(CCW, LOW);//Motor stops//
}

This is what i managed to write, and it does not work still

and it does not work still

It does something. Feel free to share your observations and your definition of "work".

The time that motion starts is NOT when the pin is HIGH. It is when the pin is HIGH now but was not HIGH last time. Look at the state change detection example.

You should NOT be adding times.

   if(motionStarted > 0 && millis() - motionStarted > 15000)

By clearing the time in motionStarted when motion is no longer detected, that stops the body of the statement from being executed even though motion started long ago.

As soon as i addeed the code, the mechanics started working just as i expected, the only problem i've ecnountered is that the motor keeps on spinning, while i need it to only spin for one second, how can i fix that?

Please don't cross-post.
Duplicate deleted.

wretchetry:
As soon as i addeed the code, the mechanics started working just as i expected, the only problem i've ecnountered is that the motor keeps on spinning, while i need it to only spin for one second, how can i fix that?

With a text editor, of course.

If you need help, post your code.