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
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.
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.