Seeking advice on Building a conveyor belt with IR sensor and DC motor.

Hello Arduino Geniuses,

Thank you Everyone for taking the time to read my very first post on this Awesome Forum.

I want to SHOUT OUT to MR DELTA_G and MR STERRETJE for helping me solving my last puzzle. Thank you for your great support and guidance.

I am trying to build a conveyor belt using IR Sensor and two DC motor.
However im getting confused on how to join these two seperate sketch together.
here are my sketch.

FIRST SKETCH (1A)

/*with this sketch

  • IR sensor will keep the LED on and when object is detected
  • the light will be off and start again.
  • THIS IS TEST CODE
    */

int led = 11; // declaring LED to pin 11 of Arduino.
int ledOff; // declaring LED Off open integer .
int count = 0;

void setup() {
// put your setup code here, to run once:
pinMode(A0,INPUT);
pinMode(A1,OUTPUT);
pinMode(A2,OUTPUT);
pinMode(led,OUTPUT);
pinMode (13, OUTPUT);
digitalWrite(A2,HIGH);
digitalWrite(A1,LOW);
Serial.begin(9600);
}

void loop () {
// put your main code here, to run repeatedly:
//Serial.println(analogRead(A0)); // this will print the IR sensor value.
//delay(100); // printing delay.

if(analogRead(A0) < 250) // less then 250 means there is an obstacle
{digitalWrite(11,LOW); // LED will be OFF
count++;
Serial.println (count);
delay (5000); // LED off for 5 seconds
digitalWrite (11,HIGH); // LED ON again
delay (3000);} // for 3 seconds

else // if IR doesnt sense an obstacle
digitalWrite(11,HIGH); // LED will be ON continiously
}

HERE IS THE SECOND SKETCH (2A)

// connect motor controller pins to Arduino digital pins
// motor one
int enA = 10;
int in1 = 9;
int in2 = 8;
// motor two
int enB = 5;
int in3 = 7;
int in4 = 6;

void setup()
{
// set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}

void loop ()
{
// this function will run the motors in both directions at a fixed speed
// turn on motor A
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// set speed to 255 out of possible range 0~255
analogWrite(enA, 255);
// turn on motor B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// set speed to 255 out of possible range 0~255
analogWrite(enB, 255);
delay(2000);

// REQUIREMENT
// i need to join this sketch with the IR SENSOR Sketch
// so that when the LED is OFF the motors will move in one direction
// and after 4 obstruction on the IR sensor (count ==4)
// the motors will Rotate in the opposing direction
// and when (count == 0) the motors will change direction again..

// now change motor directions
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(2000);
// now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}

void loop()
{
demoOne();
delay(5000);
}

// REQUIREMENT

// i need to join this sketch with the IR SENSOR Sketch
// so that when the LED is OFF the motors will move in one direction
// and after 4 obstruction on the IR sensor (count ==4)
// the motors will Rotate in the opposing direction
// and when (count == 0) the motors will change direction again..
// and keep it on the loop.

I am seeking your valuable guidance .
kind regards
LordStark.

1 Like

I am seeking your valuable guidance .

Go for it.

What have you tried? You have pretty simple requirements. You have "working" code (full of useless and incorrect comments. You should be able to use that code as the basis for building the program that you want.

Thank you Oh great Mr PaulS.. I appreciate your kind reply and encouraging words for my project.

Could you please tell me the following

  1. how can i connect while (count == 4 ) with motor reverse direction?

Kind Regards
LordStark

  1. how can i connect while (count == 4 ) with motor reverse direction?

I am almost certain that you do not want to. I am reasonably confident that you want to use if(count == 4), NOT while(count == 4).

if(count == 4)
{
   // reverse motor direction

   count = 0; // Reset count
}
  // now change motor directions
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  delay(2000);
  // now turn off motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);

I hope you don't drive your car that way. Stopping the motors first, and then changing direction is far easier on all the parts.

Thank you Mr PaulS. I wasnt sure which fuction to use and was confused.

Thank you so much for taking the time to read and reply to my post.

kind regards
LordStark.