I'm working on a school project where I'm building a small convyeor belt using an ARDUINO, DC motor and IR sensor for moving object detection so my color sensor can read color value.
I'm using single mosfet IRFZ44N to control DC motor. I can control DC motor using mosfet normally as turning it ON/OFF using digitalWrite using an arduino.
So, I want to stop convyeyor belt for 500ms when IR sensor detects HIGH and let color sensor read color of object and then let object move ahead normally on belt. I have written below code but the IR sensor detect object but motor never stops running.
#define IR_COLOR 34
const int MOTORpin = 32;
const long onDuration = 1000;// OFF time for LED
const long offDuration = 1000;// ON time for LED
int MOTORState =HIGH;// initial state of LED
boolean MOTOROnFlag = true;
long rememberTime=0;
void setup() {
pinMode(IR_COLOR, INPUT);
pinMode(MOTORpin,OUTPUT);// define MOTORpin as output
digitalWrite(MOTORpin,MOTORState);// set initial state
}
void loop() {
int state = digitalRead(IR_COLOR);
if( state == false )
{
if( (millis()- rememberTime) >= onDuration){
MOTORState = LOW;
rememberTime=millis();
}
}
else if(state == true)
{
if( (millis()- rememberTime) >= offDuration){
MOTORState =HIGH;// change the state of LED
rememberTime=millis();// remember Current millis() time
}
}
digitalWrite(MOTORpin,MOTORState);// turn the LED ON or OFF
}// loop ends