Go Down

Topic: Controlling DC motor. (Read 517 times) previous topic - next topic

zeeshanzbs1

Apr 12, 2017, 07:33 am Last Edit: Apr 12, 2017, 07:48 am by zeeshanzbs1
Hello guys, I would like to control a dc motor with the help of infrared obstacle detector sensor. I have written the following code which describes that when an IR obstacle detect something, the dc motor will stop. I want to add some if statements which shows that if the obstacle stays at front of IR obstacle sensor for more than three seconds, the dc motor change its direction. Help please, I 'll really appreciate it. I need it for one of my projects.
CODE

//DC Motor and IR obstacle Avoidance Sensor
const int motorIn1 = 9; // the one pin of the motor attach to pin 9
const int motorIn2 = 10;// the another pin of the motor attach to pin 10
const int avoidPin = 7;
/***************************************/
void setup()
{
pinMode(motorIn1,OUTPUT); //initialize the motorIn1 pin as output
pinMode(motorIn2,OUTPUT); //initialize the motorIn2 pin as output
pinMode(avoidPin, INPUT); //set the avoidPin as INPUT
}
/****************************************/
void loop()
{
  boolean avoidVal = digitalRead(avoidPin);
  if(avoidVal == HIGH)
  {
 
    digitalWrite(motorIn1,HIGH); //set the speed of motor
    digitalWrite(motorIn2,LOW); //stop the motorIn2 pin of motor
  }
  else
  {
    digitalWrite(motorIn1,LOW); //set the speed of motor
    digitalWrite(motorIn2,LOW); //stop the motorIn2 pin of motor
  }
}

zeeshanzbs1

By the way, I am using L293D to drive the motor.

TomGeorge

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks..  Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up