0

I recently wrote the code for this project but it's weird because when I press button on the remote nothing shows up on the Serial Monitor and consequently the robot doesn't respond. I know it's not a hardware issue because when I used the IRrecv Demo sketch values were displayed on the serial monitor

Please tell me what's wrong! I'm sure there are a lot of talented programmers out there!!

#include <IRremote.h>
#include <AFMotor.h>
AF_DCMotor rightMotor(1); // Right motor connected to port 1
AF_DCMotor leftMotor(2); // Left motor connected to port 2
IRrecv irrecv(A2);// IR receiver connected to pin A2(ANALOG PINS ON MOTOR             SHIELD USED AS DIGITAL INS!)
decode_results results;
#define Straight 0xC1CC42BD
#define Right 0xC1CCA25D
#define Left 0xC1CC22DD
#define Back 0xC1CCC23D

void setup(){
  Serial.begin(9600); // Establishing Serial Communication, THIS IS     IMPORTANT!!
  irrecv.enableIRIn(); // Start the receiver
}
void loop() {
  if (irrecv.decode(&results))
  { 
      Serial.println(results.value, HEX);

      if((results.value)== Straight)
      {
      moveForward();       
      } 
      if((results.value)== Right)
      {
        turnRight();      
       }
      if((results.value)== Left)
      {
        turnLeft();      
      }
      if((results.value)== Back)
      {
        moveBackward();
      }
      else 
      {
        Stop();
      }
      irrecv.resume();
  }
  delay(100);
}

void moveForward() {

    leftMotor.setSpeed(255);
    rightMotor.setSpeed(255);
    leftMotor.run(FORWARD);
    rightMotor.run(FORWARD);
}

void turnRight(){

  leftMotor.setSpeed(200);
  rightMotor.setSpeed(200);
  leftMotor.run(FORWARD);
  rightMotor.run(BACKWARD);
  delay(400);

  leftMotor.run(FORWARD);
  rightMotor.run(FORWARD);      
} 

void turnLeft() {

  leftMotor.setSpeed(200);
  rightMotor.setSpeed(200);
  leftMotor.run(BACKWARD);
  rightMotor.run(FORWARD);
  delay(400);

  leftMotor.run(FORWARD);
  rightMotor.run(FORWARD);
}  
void moveBackward() {

    leftMotor.setSpeed(200);
    rightMotor.setSpeed(200);
    leftMotor.run(BACKWARD);
    rightMotor.run(BACKWARD);
}

void Stop(){
    leftMotor.setSpeed(0);
    rightMotor.setSpeed(0);

} 
0

You are using two libraries (Arduino-IRremote & Adafruit-Motor-Shield-library) which both use Timer2 on the microcontroller.

The motor library actually changes the setup of timer 2 (see line 92 of AFMotor.cpp) which will disrupt the timing that the IR sensor uses to determine the IR code received.

As far as I can see, the IR library is using Timer2 only. This means you should be able to change your motors over to port 3 & 4 which use Timer0 instead.

AF_DCMotor rightMotor(3); // Right motor connected to port 3
AF_DCMotor leftMotor(4); // Left motor connected to port 4
0

Try replacing you loop function with this one. It won't fix it.

void loop() 
{
    if (irrecv.decode(&results))
    { 
        Serial.println(results.value, HEX);

        switch (results.value)
        {
        case Straight:
            moveForward();
            break;
        case Right:
            turnRight();
            break;
        case Left:
            turnLeft();
            break;
        case Back:
            moveBackward();
            break;
        default:
            Stop();
        }
        irrecv.resume();
    }
    else
    {
        Serial.println ("Nothing received");    // This message will annoy you so comment it out as soon as you are happy with what is happening.
    delay(100);
}

The switch statement is just a neater way of doing your ifs. The Nothing received message will show you if decode is ever returning. If it is returning you need to work out why it is returning false, by looking in the library code. If it isn't returning then you need to look in the library code and see why. If it is returning, but not giving you an expected value, you need to look in the library code and see why.

Sorry but from what you describe it seems to be an issue with the library or your use of it, could be anything, but when you can get some more info post it back here.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.