PIR sensor and DC motor

Hello, I recently started working with arduinos and sensors.
My goal is to make the DC motor spin at max capacity when the PIR sensor sees motion. However, when I setup the circuit, either the DC motor turns for a specific amount of time then stops, or the DC motor just constantly spins. Therefore, it doesn't really take into account the PIR sensor input.
Here's the code I used from Arduino with PIR Motion Sensor | Random Nerd Tutorials
that was modified by Rui Santos based on PIR sensor by Limor Fried. I edited it to take into account the DC motor using the website: Arduino - DC Motor.

int motor = 12; // the pin that the motor is atteched to
int sensor = 2; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)

void setup() {

pinMode(motor, OUTPUT); // initialize motor as an output
pinMode(sensor, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial
}

void loop(){
val = digitalRead(sensor); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(motor, HIGH); // turn MOTOR ON
analogWrite(motor, 255);
delay(200); // delay 100 milliseconds

if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(motor, LOW); // turn MOTOR OFF
delay(200); // delay 200 milliseconds
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
}

There is more than one kind of Arduino, which one do you have? You cannot drive a motor directly from an output pin,not enough current, is that what your are doing? How much current does the motor require? Is the PIR input HIGH or LOW when activated?

outsider:
There is more than one kind of Arduino, which one do you have? You cannot drive a motor directly from an output pin,not enough current, is that what your are doing? How much current does the motor require? Is the PIR input HIGH or LOW when activated?

I am using the mega 2560. the motor requires 3-6 V.
once plugged in, the code calculates the value of the pir input using digitalRead then goes on to a while loop

The code you posted does not have a while loop. You did not answer my question about how you are driving the motor or if the PIR is HIGH true or LOW true, Post a link to the PIR AND the code you are using NOW.

Sorry. I mean for loop. I looked at most the websites and tried to put them together. I only followed their schematic and steps for guidance but I am not that knowledgeable enough to answer your question yet.

https://www.tutorialspoint.com/arduino/arduino_dc_motor.htm

OK, well, lets see how the PIR works, disconnect everything EXCEPT the PIR and run this test program:

void setup()
{
  pinMode(2,INPUT);
  pinMode(LED_BUILTIN,OUTPUT);
}
void loop()
{
  digitalWrite(LED_BUILTIN,digitalRead(2));
}

To see how the PIR works.

You have a variable that holds the state of the PIR pin - val.

You have a variable, state, that holds what? Why do you need two global variables that appear to hold the same thing?