0

So, I tried making an Arduino Project(Controlling Motors by Remote and Ultrasonic Sensor would activate Servo when the distance is less than 50cm). I am trying to build a project, A remote controlled car, which will pick up objects when it encouters an object within 50cm. The remote controlled car works fine, but, to pick up objects i am trying to make it work with ultrasonic sensor, but to no avail

First when I just build the circuit for Remote Controled Motors the circuit worked fine, but after adding an Ultrasonic Sensor and Servo the circuit is not working)

This is the code without Ultrasonic and Servo:

#include <IRremote.h>      //including the remote library

#define Next_button 16615543  // code received from next button
#define Prev_button 16619623  // code received from previous button
#define left_button 16591063  // code received from left button
#define right_button 16607383 // code received from right button
#define Stop_button 16580863   // code received from stop button

int receiver_pin = 12;      //output pin of IR receiver to pin 2 of arduino
//initializing the pins for leds
int left_motor1 = 2;      //pin 6 of arduino to pin 7 of l293d
int left_motor2 = 3;      //pin 7 of arduino to pin 2 of l293d
int right_motor1  =4;      //pin 5 of arduino to pin 10 of l293d
int right_motor2 = 5;      //pin 4 of arduino to pin 15 of l293d

IRrecv receiver(receiver_pin); //Arduino will take output of IR receiver from pin 2
decode_results output;

void setup() {
  Serial.begin(9600);
  receiver.enableIRIn(); // Start to take the output from IR receiver
  //initializing all the pins where we have connected the led's as output pins
  pinMode(left_motor1, OUTPUT);
  pinMode(left_motor2, OUTPUT);
  pinMode(right_motor1, OUTPUT);
  pinMode(right_motor2, OUTPUT);
}

void loop() {
  if (receiver.decode(&output)) {
    unsigned int value = output.value;
    switch(value) {
      case Next_button:
           digitalWrite(left_motor1,LOW);
           digitalWrite(left_motor2,HIGH);
           digitalWrite(right_motor1,HIGH);
           digitalWrite(right_motor2,LOW);
           break;
      case Prev_button:
           digitalWrite(left_motor1,HIGH);
           digitalWrite(left_motor2,LOW);
           digitalWrite(right_motor1,LOW);
           digitalWrite(right_motor2,HIGH);
           break;
      case left_button: 
           digitalWrite(left_motor1,LOW);
           digitalWrite(left_motor2,HIGH);
           digitalWrite(right_motor1,HIGH);
           digitalWrite(right_motor2,HIGH);
           break;
      case right_button:
           digitalWrite(left_motor1,HIGH);
           digitalWrite(left_motor2,HIGH);
           digitalWrite(right_motor1,HIGH);
           digitalWrite(right_motor2,LOW);
           break;
      case Stop_button:
           digitalWrite(left_motor1,LOW);
           digitalWrite(left_motor2,LOW);
           digitalWrite(right_motor1,LOW);
           digitalWrite(right_motor2,LOW);
           break;
    }
    receiver.resume();
  }
}

Circuit Successfully Working

This is the code with Ultrasonic and Servo:

#include <IRremote.h>      //including the remote library
#include <Servo.h>

#define Next_button 16615543  // code received from next button
#define Prev_button 16619623  // code received from previous button
#define left_button 16591063  // code received from left button
#define right_button 16607383 // code received from right button
#define Stop_button 16580863   // code received from stop button
#define trigPin 11
#define echoPin 10

int receiver_pin = 12;      //output pin of IR receiver to pin 2 of arduino
//initializing the pins for leds
int left_motor1 = 2;      //pin 6 of arduino to pin 7 of l293d
int left_motor2 = 3;      //pin 7 of arduino to pin 2 of l293d
int right_motor1  =4;      //pin 5 of arduino to pin 10 of l293d
int right_motor2 = 5;      //pin 4 of arduino to pin 15 of l293d
Servo servo;

IRrecv receiver(receiver_pin); //Arduino will take output of IR receiver from pin 2
decode_results output;

void setup() {
  Serial.begin(9600);
  receiver.enableIRIn(); // Start to take the output from IR receiver
  //initializing all the pins where we have connected the led's as output pins
  pinMode(left_motor1, OUTPUT);
  pinMode(left_motor2, OUTPUT);
  pinMode(right_motor1, OUTPUT);
  pinMode(right_motor2, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  servo.attach(8);
}

void loop() {
  if (receiver.decode(&output)) {
    unsigned int value = output.value;
    switch(value) {
      case Next_button:
           digitalWrite(left_motor1,LOW);
           digitalWrite(left_motor2,HIGH);
           digitalWrite(right_motor1,HIGH);
           digitalWrite(right_motor2,LOW);
           break;
      case Prev_button:
           digitalWrite(left_motor1,HIGH);
           digitalWrite(left_motor2,LOW);
           digitalWrite(right_motor1,LOW);
           digitalWrite(right_motor2,HIGH);
           break;
      case left_button: 
           digitalWrite(left_motor1,LOW);
           digitalWrite(left_motor2,HIGH);
           digitalWrite(right_motor1,HIGH);
           digitalWrite(right_motor2,HIGH);
           break;
      case right_button:
           digitalWrite(left_motor1,HIGH);
           digitalWrite(left_motor2,HIGH);
           digitalWrite(right_motor1,HIGH);
           digitalWrite(right_motor2,LOW);
           break;
      case Stop_button:
           digitalWrite(left_motor1,LOW);
           digitalWrite(left_motor2,LOW);
           digitalWrite(right_motor1,LOW);
           digitalWrite(right_motor2,LOW);
           break;
    }
    receiver.resume();
  }
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  if (distance < 50) {
    Serial.println("the distance is less than 5");
    servo.write(90);
  }
  else {
    servo.write(0);
  }
  if (distance > 60 || distance <= 0) {
    Serial.println("The distance is more than 60");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
}

Overall Circuit Close up

  • Lose the big delay. Are you really trying to use a PP3 to power it all? – Majenko Mar 2 at 15:47
  • Yes, 9v. Should I use more volts or this is fine. – Mutant101was_here Mar 2 at 18:45
  • It's not the volts, it's the current. Those batteries are insanely weak. They are not suitable for driving motors. – Majenko Mar 2 at 19:34
  • "is not working" not a sufficient problem description. We cannot know, how it is not working. Have you tried to debug your code by using Serial prints? As Majenko already stated, the 500ms delay at the end of loop will really break down the performance of the code (since nothing can be done during these 500ms). Look into the BlinkWithoutDelay example, that comes with the Arduino IDE, to learn, how delays can be avoided. – chrisl Mar 2 at 20:21
  • As you mentioned, I used a Variable Power Supply and it actually increased the RPM of the motor. But, can you elaborate how to supply more amps. – Mutant101was_here Mar 3 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.