Go Down

Topic: Want to control 2 DC motors with MP3 player Remote (Read 382 times) previous topic - next topic

fahad5468

Hi. This is my first ever post on this forum.
Also i am a beginner to arduino,
I want to control two DC motors probably 2.5/3 volt. I have tested motors with example code named "MOTOR PARTY" fortunately motors are working fine.
Now i want to control them with a remote of MP3 player.
Here is my code:

#include <IRremote.h>
#include <AFMotor.h>

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
int RECV_PIN = A5;

IRrecv irrecv(RECV_PIN);

decode_results results;
unsigned long lastCode;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver

  motor1.setSpeed(100);
  motor1.run(RELEASE);
  motor2.setSpeed(100);
  motor2.run(RELEASE);


}

void loop() {
  if (irrecv.decode(&results))
  {
    if(results.value == 0xFFFFFFFF)
     {
       results.value = lastCode;
     }
     
     if(results.value == 0x807FA05F)
     {
       lastCode = results.value;
       up();
     } 
     if(results.value == 0x807F609F)
     {
       lastCode = results.value;
       down();
     } 
     if(results.value == 0x807F906F)
     {
      lastCode = results.value;
       right();
     } 
     if(results.value == 0x807F807F)
     {
      lastCode = results.value;
       left();
     }       
     
     
    delay(20);
    Serial.println(lastCode, HEX);
    irrecv.resume(); // Receive the next value
  }
 // delay(100);
}

void up()
{
  motor1.run(FORWARD);
  motor2.run(FORWARD);
}

void down()
{
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
}

void right()
{
  motor1.run(FORWARD);
  motor2.run(BACKWARD);
}

void left()
{
  motor1.run(BACKWARD);
  motor2.run(FORWARD);
}


On "Serial monitor" it is giving absolutely correct output. But the motors are not moving at all. Please help me asap. Thanks in advance.

TheMemberFormerlyKnownAsAWOL

The motors are moving fine and the serial monitor output is absolutely correct.

So what's the problem?

(Please remember to use code tags when posting code)

fahad5468

Motors are working when i tested them with Example code named Motor Party.
But when i upload that ir code i mentioned above, and press/hold any remote button, Serial Monitor is giving perfect output but motors are not working.

In short motors are not working with my code.  :(

fahad5468

Hi. This is my first ever post on this forum.
Also i am a beginner to arduino,
I want to control two DC motors probably 2.5/3 volt. I have tested motors with example code named "MOTOR PARTY"  motors are working fine.
Now i want to control them with a remote of MP3 player.
Here is my code:

#include <IRremote.h>
#include <AFMotor.h>

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
int RECV_PIN = A5;

IRrecv irrecv(RECV_PIN);

decode_results results;
unsigned long lastCode;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver

  motor1.setSpeed(100);
  motor1.run(RELEASE);
  motor2.setSpeed(100);
  motor2.run(RELEASE);


}

void loop() {
  if (irrecv.decode(&results))
  {
    if(results.value == 0xFFFFFFFF)
     {
       results.value = lastCode;
     }
     
     if(results.value == 0x807FA05F)
     {
       lastCode = results.value;
       up();
     } 
     if(results.value == 0x807F609F)
     {
       lastCode = results.value;
       down();
     } 
     if(results.value == 0x807F906F)
     {
      lastCode = results.value;
       right();
     } 
     if(results.value == 0x807F807F)
     {
      lastCode = results.value;
       left();
     }       
     
     
    delay(20);
    Serial.println(lastCode, HEX);
    irrecv.resume(); // Receive the next value
  }
 // delay(100);
}

void up()
{
  motor1.run(FORWARD);
  motor2.run(FORWARD);
}

void down()
{
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
}

void right()
{
  motor1.run(FORWARD);
  motor2.run(BACKWARD);
}

void left()
{
  motor1.run(BACKWARD);
  motor2.run(FORWARD);
}


On "Serial monitor" it is giving absolutely correct output. But the motors are not moving at all.

In short motors are not working by the code mentioned above. When i am uploading this code and pressing a button, Serial Monitor is giving correct output, but motors are not working.

Please help me asap.
Thanks.

GoForSmoke

For all I know you might only have 1 bad connection. Is this on a breadboard? Has anything moved since you tested the motors?

Try adding more print statements and speeding up the serial baud rate to 115200 just to clear the serial output buffer quicker.

Check out the switch - case statement when you're ready to clear all those if () statements.

https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/

1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

hillbillymic888

#5
Sep 16, 2019, 09:50 am Last Edit: Sep 16, 2019, 11:27 am by hillbillymic888 Reason: Correcting some errors i made in jumping on before reading sticky.
fahad5468, i'm also a newbie with Arduino.

I am also experiencing similar difficulties with my code.

i have 2 9g servos connected to servo pins on "Duinotech motor shield"

i have 2 dc motors on m3 & m4 of the motor shield.

The servos work as desired !

The motors receive no output voltage, therefore not turning.

I too tested with motor party in (AFMotor) and managed an obstacle avoiding bot using ultrasonic sensor (Duinotech 4 pin) with this code gleaned from this site if my memory serves me correctly : Obstacle_Avoiding_Robot_Code by Roy Pe'er

[code
#include <AFMotor.h> //import your motor shield library
#define trigPin 2 // define the pins of your sensor
#define echoPin A4
AF_DCMotor motor1(1,MOTOR12_64KHZ); // set up motors.
AF_DCMotor motor2(2, MOTOR12_8KHZ);
 
void setup() {
  Serial.begin(9600); // begin serial communitication 
  Serial.println("Motor test!");
   pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
  pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
  motor1.setSpeed(105); //set the speed of the motors, between 0-255
  motor2.setSpeed (105); 
}
 
void loop() {

   long duration, distance; // start the scan
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); // delays are required for a succesful sensor operation.
  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10); //this delay is required as well!
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;// convert the distance to centimeters.
  if (distance < 25)/*if there's an obstacle 25 centimers, ahead, do the following: */ {   
   Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!");// print out the distance in centimeters.

Serial.println (" The obstacle is declared a threat due to close distance. ");
Serial.println (" Turning !");
    motor1.run(FORWARD);  // Turn as long as there's an obstacle ahead.
    motor2.run (BACKWARD);

}
  else {
   Serial.println ("No obstacle detected. going forward");
   delay (15);
   motor1.run(FORWARD); //if there's no obstacle ahead, Go Forward!
    motor2.run(FORWARD); 
  }   
}

][/code]

Above code success, no errors.

Below code, motors not being powered. Servos used in code work correctly with ir control. serial print reads correctly on serial monitor, displaying all buttons on remote correctly.

Code: [Select]

#include <IRremote.h>
#include <Servo.h>
#include <AFMotor.h> //import your motor shield library


AF_DCMotor motor1(1, MOTOR12_64KHZ); // set up motors.
AF_DCMotor motor2(2, MOTOR12_8KHZ);
Servo myservo1;  // create servo object to control a servo // twelve servo objects can be created on most boards
Servo myservo2;  // create servo object to control a servo // twelve servo objects can be created on most boards
int pos = 90 ; // variable to store the servo position


const int RECV_PIN = A2;
IRrecv irrecv(RECV_PIN);
decode_results results;




void setup() {

  Serial.begin(9600);

  irrecv.enableIRIn();v
  irrecv.blink13(true);
  motor1.setSpeed(111); //set the speed of the motors, between 0-255
  motor2.setSpeed (100);
  myservo1.attach(10);    // attaches the servo on pin 9 to the servo object
  myservo2.attach(9);    // attaches the servo on pin 10 to the servo object
}
void loop() {
 
  unsigned long key_value = 0;

  if (irrecv.decode(&results)) {

    if (results.value == 0xFFFFFFFF)
      results.value = 0;
    irrecv.resume();         // Receive the next value }
   
    switch (results.value) {
      case 0xFF02FD: //Keypad button "UP"
        Serial.println("FORWARD");
        motor1.run(FORWARD);
        motor2.run(FORWARD);
        delay(1000);
        motor1.run(RELEASE);
        motor2.run(RELEASE);
    }
    switch (results.value) {
      case 0xFF9867: //Keypad button "UP"
        Serial.println("BACKWARD");
        motor1.run(BACKWARD); //if there's no obstacle ahead, Go Forward!
        motor2.run(BACKWARD);
        delay(1000);
        motor1.run(RELEASE);
        motor2.run(RELEASE);
    }
    switch (results.value) {
      case 0xFFE01F: //Keypad button "LEFT"
        Serial.println("LEFT");
        motor1.run(BACKWARD); //if there's no obstacle ahead, Go Forward!
        motor2.run(FORWARD);
        delay(1000);
        motor1.run(RELEASE);
        motor2.run(RELEASE);
    }
    switch (results.value) {
      case 0xFF906F: //Keypad button "RIGHT"
        Serial.println("RIGHT");
        motor1.run(FORWARD);
        motor2.run(BACKWARD);
        delay(1000);
    }
    switch (results.value) {
      case 0xFF18E7: //Keypad button "2"
        Serial.println("HEAD UP");
        myservo2.write (pos -= 15); (pos);
        delay(5);
    }
    switch (results.value) {
      case 0xFF4AB5: //Keypad button "8"
        Serial.println("HEAD DOWN");
        myservo2.write(pos += 15);
        delay(5);
    }
    switch (results.value) {
      case 0xFF10EF: //Keypad button "4"
        Serial.println("HEAD LEFT");
        myservo1.write(pos += 15);           
        delay(5);
    }
    switch (results.value) {
      case 0xFF5AA5: //Keypad button "6"
        Serial.println("HEAD RIGHT");
        myservo1.write(pos -= 15);           
        delay(5);
    }
  }
}

slipstick

I've never heard of "MOTOR PARTY". If you post that code too we could see what the difference is between what that does that apparently works and what your code is doing that doesn't work.

Steve

pert

I've merged your cross posts @fahad5468.

Cross posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes writing a detailed answer on this thread, without knowing that someone else already did the same in the other thread.

Repeated cross posting will result in a suspension from the forum.

In the future, please take some time to pick the forum section that best suits the topic of your question and then only post once to that forum section. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum section. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

fahad5468

#8
Sep 16, 2019, 05:25 pm Last Edit: Sep 16, 2019, 05:26 pm by fahad5468
switch (results.value) {
      case 0xFF02FD: //Keypad button "UP"
        Serial.println("FORWARD");
        motor1.run(FORWARD);
        motor2.run(FORWARD);
        delay(1000);
        motor1.run(RELEASE);
        motor2.run(RELEASE);
    }
    switch (results.value) {
      case 0xFF9867: //Keypad button "UP"
        Serial.println("BACKWARD");
        motor1.run(BACKWARD); //if there's no obstacle ahead, Go Forward!
        motor2.run(BACKWARD);
        delay(1000);
        motor1.run(RELEASE);
        motor2.run(RELEASE);
    }
    switch (results.value) {
      case 0xFFE01F: //Keypad button "LEFT"
        Serial.println("LEFT");
        motor1.run(BACKWARD); //if there's no obstacle ahead, Go Forward!
        motor2.run(FORWARD);
        delay(1000);
        motor1.run(RELEASE);
        motor2.run(RELEASE);
    }
    switch (results.value) {
      case 0xFF906F: //Keypad button "RIGHT"
        Serial.println("RIGHT");
        motor1.run(FORWARD);
        motor2.run(BACKWARD);
        delay(1000);
    }
    switch (results.value) {
      case 0xFF18E7: //Keypad button "2"
        Serial.println("HEAD UP");
        myservo2.write (pos -= 15); (pos);
        delay(5);
    }
    switch (results.value) {
      case 0xFF4AB5: //Keypad button "8"
        Serial.println("HEAD DOWN");
        myservo2.write(pos += 15);
        delay(5);
    }
    switch (results.value) {
      case 0xFF10EF: //Keypad button "4"
        Serial.println("HEAD LEFT");
        myservo1.write(pos += 15);          
        delay(5);
    }
    switch (results.value) {
      case 0xFF5AA5: //Keypad
Dude you are doing mistake with switch case.
Write Switch only once and then write all cases under that switch statement.

Check the image below.

hillbillymic888

Agreed. it's not the best looking script around.
The fact that the servos work and the motors don't.
The obstacle avoid program works with servos mounted and connected but not programmed to operate, yet with the same build, the program "sketch TEAK" the servos work, motors not.

I am only a couple of weeks into my endeavours of coding and have basically been sampling others' code and trialling different setups. every tutorial etc. that I have tried has succeeded until now. I've been stuck on this one for days and am about to throw the project out of the cot!

p.s. I tried with just one switch statement, with same result.

TheMemberFormerlyKnownAsAWOL


hillbillymic888

Big fingers/tiny keyboard and screen. (10 year old notebook)

My typos are easily spotted by the compiler also.

Thanks for taking the time to view it. I only started my coding journey a few weeks ago, without any prior computer skills beyond using email and google.

After actually removing all the individual switch statements to one, nothing worked right. i did not test the servos til now.




fahad5468

Now i tried it with switch case and also connected an LED with A4 from L293D. Here's my code

#include <IRremote.h>
#include <AFMotor.h>

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);

int IR_Recv = A5;
IRrecv irrecv(IR_Recv);
decode_results results;
int bluePin = A4;
void setup()
{
  motor1.setSpeed(170);
  motor1.run(RELEASE);
  motor2.setSpeed(170);
  motor2.run(RELEASE);

  Serial.begin(9600); 
  irrecv.enableIRIn();
  pinMode(bluePin, OUTPUT);
}
void loop()
{
   if (irrecv.decode(&results)){
    long int decCode = results.value;
    Serial.println(results.value);
   
    switch (results.value){
      case 2155807485:
        motor1.run(FORWARD);
        motor2.run(FORWARD);
        digitalWrite(bluePin, HIGH);   
        break;   
//       case 2155815645:
//        motor1.run(BACKWARD);
//        motor2.run(BACKWARD);
//        break;           
//       case 2155811565:
//        motor1.run(RELEASE);
//        motor2.run(RELEASE);
    }
    irrecv.resume(); // Receives the next value from the button you press

  }
  delay(10);
}

On pressing button 1 LED is glowing but still motors are not working.
I also checked the voltage on the jack M1 and M2 of l293d motor shield with multimeter, it is showing 0 voltage.

slipstick

Let's start again with the basics. You're using a library intended for use with the old Adafruit Motor Shield V1. Do you actually have an Adafruit Motor Shield V1?

Please show us a schematic of your setup and please post pictures that show all the components and exactly how everything is connected and powered.

Steve

hillbillymic888

The motor shield i have is a DUINOTECH branded unit. it has 2 servo pin sets and can run 4 dc motors or two steppers at the same time as the servos. It has 2 l293d chips. zero volts at motor out when using IR_Remote with AF_Motor, servos working, but without use of IR_Remote, everything works as it should.

I think i read somewhere that there is a conflict between IR_Remote and AF_Motor and that deleting the file IR_Robot will fix it???

i have just gone without the driver shield and AF_Motor.h and used one of the l293d chips directly. it now works fine after modifying the code.
Code: [Select]


#include <Servo.h>
#include<IRremote.h>
#define trigPin 2 //define the pins of ultrasonic sensor
#define echoPin 3

Servo myservo1;  // create servo object to control a servo // twelve servo objects can be created on most boards
Servo myservo2;  // create servo object to control a servo // twelve servo objects can be created on most boards

int pos = 90 ; // variable to store the servo position
int left = 9; //pin 9 of arduino to pin 7 of l293d
int leftrev = 8; //pin 8 of arduino to pin 2 of l293d
int right = 10; //pin 10 of arduino to pin 10 of l293d
int rightrev = 11 ; //pin 1 l293d                                                             
int RECV_PIN = 4;//pin 6 of arduino to data pin of ir receiver
IRrecv irrecv(RECV_PIN); decode_results results; // setup infra-red sensor

void setup() {
  pinMode(left, OUTPUT); //left wheel forward
  pinMode(leftrev, OUTPUT); //left wheel reverse
  pinMode(right, OUTPUT); //right wheel forward
  pinMode(rightrev, OUTPUT); // right wheel reverse
  pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
  pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
  myservo1.attach(A5);    // attaches the servo on pin 9 to the servo object
  myservo2.attach(A4);    // attaches the servo on pin 10 to the servo object
  irrecv.enableIRIn(); // Start the receiver
  Serial.begin(9600); // Start the serial monitor
}

void loop() {

  unsigned long key_value = 0;
  if (irrecv.decode(&results)) {

    if (results.value == 0xFFFFFFFF)
      results.value = 0;

    if (irrecv.decode(&results)) {
      Serial.println(results.value, HEX);
      irrecv.resume(); // Receive the next value
    }
   
    //bot moves forward
    if (results.value == 0x38863BF2) {
      digitalWrite(left, LOW);//r
      digitalWrite(leftrev, HIGH);//r
      digitalWrite(right, LOW);
      digitalWrite(rightrev, HIGH);
    }
    //bot moves backward
    if (results.value == 0x38863BFA) {
      digitalWrite(left, HIGH);//9
      digitalWrite(leftrev, LOW);//8
      digitalWrite(right, HIGH);//10
      digitalWrite(rightrev, LOW);//11
    }
    //bot moves left
    if (results.value == 0x38863BCA) {
      digitalWrite(left, HIGH);
      digitalWrite(leftrev, LOW);
      digitalWrite(right, HIGH);
      digitalWrite(rightrev, HIGH);
    }
    //bot moves right
    if (results.value == 0x38863BBC2) {
      digitalWrite(left, LOW);
      digitalWrite(leftrev, HIGH);
      digitalWrite(right, HIGH);
      digitalWrite(rightrev, LOW);
    }
    //bot stops
    if (results.value == 0x38863BDC) {
      digitalWrite(left, HIGH);
      digitalWrite(leftrev, HIGH);
      digitalWrite(right, HIGH);
      digitalWrite(rightrev, HIGH);
    }

    if (results.value == 0x38863BD0) {//head up
      myservo2.write (pos -= 30); (pos);
      delay(750);
      myservo2.write(pos += 30);
      delay(50);
    }

    if (results.value == 0x38863BC4) {//head down
      myservo2.write (pos += 30); (pos);
      delay(750);
      myservo2.write(pos -= 30);
      delay(50);
    }
    if (results.value == 0x38863BC8) {//head left
      myservo1.write (pos += 30);
      delay(750);
      myservo1.write(pos -= 30);
      delay(50);
    }
    if (results.value == 0x38863BD8) {//head right
      myservo1.write (pos -= 30); (pos);
      delay(750);
      myservo1.write(pos += 30);
      delay(50);
    }
   
  }
}






Go Up