Programming questions on DC motor

Hello. I am now using a PIR motion sensor that will detect the human movement. It's been programmed to stop the entire DC motor from spinning when it does not detect human presence or movement and vice-versa. When i try to run the program, the motion sensor is unable to detect human motion. Any error in the codes?

//Arduino PWM Speed Control?
int E2 = 6;
int M2 = 7;
int PIR_MOTION_SENSOR = 2; //Use pin 2 to receive the signal from the module

void setup()
{
pinsInit();
}
/code

void pinsInit()
{
pinMode(PIR_MOTION_SENSOR, INPUT);
pinMode(M2, OUTPUT);

}
void turnOnMotor()
{
digitalWrite(M2,HIGH);
}
void turnOffMotor()
{
digitalWrite(M2,LOW);
}
void loop()
{
int value;
if(isPeopleDetected())//if it detects the moving people?
{

for(value = 0 ; value <= 255; value+=255)
{
turnOnMotor(); //PWM Speed Control
analogWrite(E2, value); //PWM Speed Control
delay(30);
}

}
else
turnOffMotor();

}
boolean isPeopleDetected()
{
int sensorValue = digitalRead(PIR_MOTION_SENSOR);
if(sensorValue == HIGH)//if the sensor value is HIGH?
{
return true;//yes,return ture
}
else
{
return false;//no,return false
}
}

Firstly, put your code between

Why have function to do what is required in setup? Just put what you have in pinsinit straight in setup.
The program runs in void loop(). You have functions but nothing to run them in.
Give E2 and M2 meaningful names.

You for loop should be
For (value = 0, value <=255, value+)
Your value+=255 will set the value to 255 and hence drop out first time into the loop.

Weedpharma

Moderator edit: Code tags corrected.

it should actually be For (value = 0, value <=255, value++) (notice the second "+" on the third parameter). everything should work.

Partsofme, is of course correct. Not thinking straight.

Weedpharma

it should actually be For (value = 0, value <=255, value++)

{cough} semicolon, not comma {cough}

{cough} semicolon, not comma {cough}

lol that too.. try this one more time?

for (value = 0; value <=255; value++)

Delta_G:
And to the root of your problem. That for loop is going to go through 255 iterations and it will delay 30ms during each of those iterations. While you're inside that for loop (which will be 99.999999% of the time with this program) you're not checking the PIR sensor.

I have done everything for example as what they told me...but the motor is now moving at a slower revolution. and the motion sensor is still unable to detect human presence.
for(value = 0 ; value <= 255; value++)
{
turnOnMotor(); //PWM Speed Control
analogWrite(E2, value); //PWM Speed Control
delay(30);
}

You can write some helper functions to aid situations like this, basically another version
of delay that calls another loop function:

void active_delay (unsigned long del)
{
  unsigned long ts = millis () ;
  while (millis () - ts < del)
    my_loop () ;
}

void my_loop ()
{
  // here test anything that needs attention within the call to active_delay
}

void loop ()
{
  if (some condition)
  {
    for (int i = 0 ; i < 255 ; i++)
    {
       ....
      active_delay (30) ;
    }
  }
}

This creates two levels of loop, my_loop() handles the stuff that doesn't take
time or need any delaying, whereas the outer loop() can handle the higher level
stuff.

Can simplify things that otherwise would need a finite-state-machine approach,
but is not as general.

for(value = 0 ; value <= 255; value++)
  { 
    turnOnMotor();    //PWM Speed Control
    analogWrite(E2, value);   //PWM Speed Control
    delay(30);
  }

What kind of crappy motor needs to be turned on 255 times?

Delta_G:
Where in that for loop are you even trying to read the motion sensor?

and you mean that i need to implement a program in the for loop that reads the motion sensor? Isn't that implemented in the IF loop?

if(isPeopleDetected())//if it detects the moving people?
{

for(value = 0 ; value <= 255; value+=255)
{
digitalWrite(M2,HIGH); //PWM Speed Control
analogWrite(E2, value); //PWM Speed Control
delay(30);
}

}
else
{

digitalWrite(M2,LOW);
}
}
boolean isPeopleDetected()
{
int sensorValue = digitalRead(PIR_MOTION_SENSOR);
if(sensorValue == HIGH)//if the sensor value is HIGH?
{
return true;//yes,return ture
}
else
{
return false;//no,return false
}
}

and you mean that i need to implement a program in the for loop that reads the motion sensor? Isn't that implemented in the IF loop?

All together now: An if STATEMENT is not a LOOP!

The if statement does check that there are people present, but then the for LOOP blocks, because of the delay().

How many times is that for loop really going to iterate, and why the hell do you have a loop that iterates exactly once?