how to control DC motor go FW, RV, and STOP with IR sensor

I’m trying to control a DC motor via IR sensor.

no idea how to coding the source with under the conditions

please help me. :’(

parts -

  1. arduino mega
  2. IR sensor
  3. L298n(motor driver)
  4. DC motor
  5. 12V power

current state = DC motor stop / IR sensor on

  1. IR sensor on - not detected - DC motor stop

  2. IR sensor detected - DC motor go forward (set time) - Stop - IR sensor no detected during (set time) -

DC motor reverse - Stop

  1. IR sensor detected - DC motor go forward (set time) - Stop - secondly, IR sensor detected -

DC motor reverse

Conditon 1.

during DC motor go forward(set time), IR sensor detected secondly -

DC motor reverse(as time go forward as) - Stop

another example

  1. - IR - detected - DC motor FW(4 sec) - Stop - IR sensor no detective untill (25sec) -

DC motor RV(4sec) - Stop

  1. IR - detected - during DC motor forwarding (2.5 sec) - IR sensor detected -

DC motor reversing(2.5 sec) - Stop

  1. - IR - detected - DC motor FW(4 sec) - Stop - (after 18sec) IR sensor detected -

DC motor reversing(4 sec) - Stop

First of all give us a link to the IR sensor. Is it a simple phototransistor or IR receiver/decoder?

Have you got any of the code done or any of the wiring done?

Weedpharma

The IR sensor has 4pin (EN/OUT/GND/VCC)

http://ko.aliexpress.com/item/Smart-Electronics-KEYES-KY-032-4pin-IR-Infrared-Obstacle-Avoidance-Sensor-Module-for-Arduino-Diy-Smart/32571332221.html?spm=2114.010208.3.309.yuj9ZX&ws_ab_test=searchweb201556_10,searchweb201602_3_10017_10034_10021_507_10033_10022_10032_10009_10020_10008_10018_10019,searchweb201603_6&btsid=49925b6f-8d30-489a-ac4f-7d48d1ba320a

and the cord i wrote it under

int Enable = 2;
int IR = 3;
int IR_status = 0;
int cnt=0;

// motor one
int enA = 10;
int a1 = 9;
int a2 = 8;
// motor two
int enB = 5;
int b1 = 7;
int b2 = 6;

void setup() {
pinMode(Enable, OUTPUT);
pinMode(IR, INPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(a1, OUTPUT);
pinMode(a2, OUTPUT);
pinMode(b1, OUTPUT);
pinMode(b2, OUTPUT);

}

void FW() { for(int i=0; i<100; i++){
analogWrite(enA,150); // Motor A
digitalWrite(a1,LOW);
digitalWrite(a2,HIGH);
analogWrite(enB,150); // Motor B
digitalWrite(b1,LOW);
digitalWrite(b2,HIGH);
}
}

void RV() {for(int i=0; i<100; i++){
analogWrite(enA,150); // Motor A
digitalWrite(a1,HIGH);
digitalWrite(a2,LOW);
analogWrite(enB,150); // Motor B
digitalWrite(b1,HIGH);
digitalWrite(b2,LOW);
}
}
void loop() {
digitalWrite(Enable,HIGH);
IR_status = digitalRead(IR);

if(IR_status){if(cnt==0)FW();}

if(cnt){if(–cnt==0)RV();}
}

weedpharma: First of all give us a link to the IR sensor. Is it a simple phototransistor or IR receiver/decoder?

Have you got any of the code done or any of the wiring done?

Weedpharma

The IR sensor has 4pin (EN/OUT/GND/VCC) light/receiver

for(int i=0; i<100; i++

Is that what you plan to use for motor on duration?

It seems your approach to manage distance (number of motor revolutions) is by having the motors turn on for a certain amount of time. Unless ALL conditions (load, voltage, friction, temperature, etc.) are EXACTLY the same every time the motor functions are called, the motor(s) won’t turn the same amount every time. So, if you are planning to attain a certain distance with that method you will probably be disappointed.

  • Scotty