Go Down

Topic: IRreceiver and DC motors programming (Read 1 time) previous topic - next topic

rynoa

Hello everyone,

I am brandnew to Arduino (and programming, and electronics in general) and am hoping to get some help. Any advice is appreciated.

I will do my best to list and explain all necessary background info, hope it makes sense.

I am trying to build a simple tank robot using an Arduino Uno. The robot will be remote controlled and have 2 dc motors. I'm using the following components:

- Arduino Uno
- Adafruit Motor Shield (I think I accidentally bought a cheap knock off - ooops)
http://smile.amazon.com/gp/product/B00813HBBO/ref=oh_aui_detailpage_o03_s00?ie=UTF8&psc=1
- IR receiver
- TV remote
- two DC motors

I am including the IRremote.h library and the adafruit AFmotor.h library.

I started out by writing a code to get the decoded IR signals from the TV remote. Worked out just fine

Then I wrote a test code to see if the motors are working. Both are working (I took the sample code off of the adafruit website and adjusted it).

Next, I am trying to combine both so that the input from the TV remote will start and stop the motors. This is where my problems start. Here is my code

Code: [Select]
#include <AFMotor.h>
#include <IRremote.h>
 
AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
const int receiver = 2;
IRrecv irrecv(receiver);
decode_results results;
 
void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");
 
  motor1.setSpeed(200);     // set the speed of motorsto 200/255
  motor2.setSpeed(200);
  motor1.run(RELEASE);  // both motors stop
  motor2.run(RELEASE);
  irrecv.enableIRIn(); // Start IR receiver
}
 
void loop() {
  if(irrecv.decode(&results)) // have we received an IR signal?
  irrecv.resume(); // Receive the next value
  {
    {
      if(results.value == 650130)
      {
        Serial.print("tick"); 
        motor1.run(FORWARD);      // turn it on going forward
        motor2.run(FORWARD);
        delay(100);
      }
      else if(results.value == 387986)
      { 
        Serial.print("tock");
        motor1.run(BACKWARD);     // backward
        motor2.run(BACKWARD);
        delay(100);
      }
      else if(results.value == 854930)
    { 
      Serial.print("tack");
      motor1.run(RELEASE);      // stopped
      motor2.run(RELEASE);
      delay(100);
    }}}}


The code compiles and uploads fine. Also, the serial monitor will show tick, tock, tack everytime I press the remote buttons, so the arduino is receiving the signals. But, the motors are not doing anything at all.

Does anyone know what I am doing wrong?

Thank you so much in advance!

Rynoa

rynoa

In case it is needed, these are the original two codes that I used for the remote and motors

Code: [Select]
#include <IRremote.h>

const int irReceivePin = 2; //pin connected to IR detector output
IRrecv irrecv(irReceivePin); // create IR library
decode_results results; //IR results are stored here

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

void loop () {
  if(irrecv.decode(&results))
  {
    showReceivedData();
    irrecv.resume(); //Receive the next value
  }
  delay(250);
}

void showReceivedData()
{
  if (results.decode_type == UNKNOWN)
  {
    Serial.println("-Could not decode message");
  }
  else
  {
    if (results.decode_type == NEC) { Serial.print("- decode NEC: ");
    }
    else if (results.decode_type == SONY) {Serial.print("- decoded SONY: ");
    }
    else if (results.decode_type == RC5) {Serial.print("- decoded RC5: ");
    }
    else if (results.decode_type == RC6) {Serial.print("- decoded RC6: ");
    }
    Serial.print("Value = ");
    Serial.println(results.value, DEC); //Print the results as a decimal value
  }
}


Code: [Select]
#include <AFMotor.h>
 
AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
 
void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");
 
  motor1.setSpeed(200);     // set the speed to 200/255
  motor2.setSpeed(200);
}
 
void loop() {
  Serial.print("tick");
 
  motor1.run(FORWARD);      // turn it on going forward
  motor2.run(FORWARD);
  delay(5000);
 
  Serial.print("tock");
  motor1.run(BACKWARD);     // the other way
  motor2.run(BACKWARD);
  delay(5000);
 
  Serial.print("tack");
  motor1.run(RELEASE);      // stopped
  motor2.run(RELEASE);
  delay(1000);
 
  motor1.run(FORWARD);  //left
  motor2.run(BACKWARD);
  delay(3000);
 
  motor1.run(BACKWARD); // right
  motor2.run(FORWARD);
  delay(3000);
 
  motor2.run(RELEASE);
  motor1.run(RELEASE);
  delay(10000);
}

johnwasser

In place of:
Code: [Select]

  if(irrecv.decode(&results)) // have we received an IR signal?
    irrecv.resume(); // Receive the next value
  {


You probably meant to do something like:
Code: [Select]

  if(irrecv.decode(&results)) { // have we received an IR signal?
    irrecv.resume(); // Receive the next value
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

rynoa

Thank you John. I just tried your suggestion. It has not changed anything. The Arduino still receives the IR signal and prints tick tack etc on the Serial monitor, but the Motors are not spinning or doing anything at all.

Go Up