1

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 

16
  • you have no IR sensor code
    – jsotola
    Commented May 8, 2023 at 14:56
  • @jsotola IR_COLOR is IR Sensor. Commented May 8, 2023 at 15:05
  • what is IR_RED then?
    – jsotola
    Commented May 8, 2023 at 15:06
  • @jsotola It was made for future work but the initial task is not completed of stoping motor pin for 500ms and starting motor again. Motor stops as IR detects object but it remains off as object is still ahead of IR sensor. Commented May 8, 2023 at 15:10
  • so what will you name the color sensor? ... it seems like you are using confusing names
    – jsotola
    Commented May 8, 2023 at 15:17

1 Answer 1

2

You have the general idea.

Your code looks a bit cluttered. Comments are not part of the program, keep them away from the code.

The if statements reduce to if (true), that means that == true part is redundant. All you need is if(state). The if block will execute when state is true. Conversely if (!state) tests for state being false

When there are only two possible values, then else if(state == true) can be simply else.

Identical code that is in both parts of the if-else block should be moved to outside of the block, rememberTime=millis(); should be moved to after the closing brace } of the if-else block.

Here is some code that does what you want.

This code has not been tested on a hardware device.

https://wokwi.com/projects/364353854313747457

const int     sensor_blocked  = LOW;              // define sensor values
const int     sensor_clear    = HIGH;

const int     motor_on        = HIGH;
const int     motor_off       = LOW;

const int     master_enable   =  2;
const int     motor_pin       =  3;
const int     sensor_pin      = 12;

int           sensor_state;
int           sensor_previous = sensor_clear;
int           motor_state     = motor_on;

unsigned long millis_previous = 0;
unsigned long millis_now;

const long    interval        = 500;  

void setup() {

  pinMode(master_enable, INPUT_PULLUP);
  pinMode(sensor_pin,    INPUT_PULLUP);
  pinMode(motor_pin,     OUTPUT);

}

void loop() {

  sensor_state = digitalRead( sensor_pin );

  // ---------------------------------------------------------------------------------------------------

  if ( sensor_state != sensor_previous ) {             // only run this code when sensor state changes
    sensor_previous = sensor_state;                    // remember new sensor state
    
    if ( sensor_state == sensor_blocked ) {            // sensor became blocked
      motor_state = motor_off;                         // turn off motor
      millis_previous = millis();                      // start of "motor off" interval
    }
    delay(20);                                         // debounce sensor
  }

  // ---------------------------------------------------------------------------------------------------

  if ( motor_state == motor_off ) {                    // timer check is relevant only when motor is off
    millis_now = millis();
    if ( millis_now - millis_previous >= interval ) {  // check if timer expired
      motor_state = motor_on;                          // enable motor
    }
  }

  // ---------------------------------------------------------------------------------------------------

  if ( digitalRead( master_enable ) != LOW ) motor_state = motor_off;     // master switch

  // ---------------------------------------------------------------------------------------------------

  digitalWrite(motor_pin, motor_state);                // translate motor state into action

} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.