Arduino IR Remote Code

Hello,
I have an IR remote and I have connected it successfully to my Arduino Uno and Rover 5.
My current code is as follows:

#include <IRremote.h>
#include <IRremoteInt.h>

int receiver = 10; // Signal Pin of IR receiver to Arduino Digital Pin 11

/*-----( Declare objects )-----*/
IRrecv irrecv(receiver);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'

const byte P1 = 3; // Pin on arduino (front left motor)
const byte P2 = 5;// Pin on arduino (back left motor)
const byte P3 = 6; // Pin on arduino    (front right motor)                    
const byte P4 = 11;   // Pin on arduino  (back right motor)

const byte M1 = 4;// Pin on arduino (left motor)
const byte M2 = 7;// Pin on arduino (left motor)
const byte M3 = 12;  // Pin on arduino     (right motor)                  
const byte M4 = 13;   // Pin on arduino     (right motor) 

void setup() 
{ 
  irrecv.enableIRIn(); // Start the receiver
  
  pinMode(M1, OUTPUT);   // set as output
  pinMode(M2, OUTPUT);  // set as output
  pinMode(M3, OUTPUT);    // set as output
  pinMode(M4, OUTPUT);  // set as output

  pinMode(P1, OUTPUT);    // set as output
  pinMode(P2, OUTPUT);  // set as output
  pinMode(P3, OUTPUT);    // set as output
  pinMode(P4, OUTPUT); // set as output
} 

void loop() 
{ 
 if (irrecv.decode(&results)) // have we received an IR signal?

  {
    switch(results.value)

    {

      case 0xFF629D: // UP button pressed, go forward
 digitalWrite(M1,HIGH);   //direction control
  digitalWrite(M2,LOW);   //direction control
  digitalWrite(M3,HIGH);   //direction control
  digitalWrite(M4,LOW );   //direction control
  
  analogWrite(P1, 200);   //PWM Speed Control (fast)
  analogWrite(P2, 200);   //PWM Speed Control ( fast)
  analogWrite(P3, 200);   //PWM Speed Control (fast)
  analogWrite(P4, 200);   //PWM Speed Control ( fast) 
                      break;
      case 0xFFA857: // DOWN button pressed
                      
                      break;
      
      case 0xFF22DD: // LEFT button pressed
                      
                      break;

      case 0xFFC23D: // RIGHT button pressed
                      
                      break;
                      
      case 0xFF02FD: // OK button pressed
                      
                      break;

      case 0xFF6897: // 1 button pressed
                     
                      break;
      
      case 0xFF9867: // 2 button pressed
                       
                      break;

      case 0xFFB04F: // 3 button pressed
                       
                      break;
                      
      case 0xFF30CF: // 4 button pressed
                       
                      break;

      case 0xFF18E7: // 5 button pressed
                      
                      break;
      
      case 0xFF7A85: // 6 button pressed
                      
                      break;

      case 0xFF10EF: // 7 button pressed
                      
                      break;
                      
      case 0xFF38C7: // 8 button pressed
                       
                      break;

      case 0xFF5AA5: // 9 button pressed
                      
                      break;
      
      case 0xFF42BD: // * button pressed
                     
                      break;

      case 0xFF4AB5: // 0 button pressed
                      
                      break;    
      
      case 0xFF52AD: // # button pressed
                      
                      break;             
      
      
      
    }
    
      irrecv.resume(); // receive the next value
  }  

 


}

  
  /*
  digitalWrite(M1,LOW);   //direction control
  digitalWrite(M2,LOW);   //direction control
  digitalWrite(M3,LOW);   //direction control
  digitalWrite(M4,LOW);   //direction control
  
  analogWrite(P1, 250);   //PWM Speed Control (fast)
  analogWrite(P2, 250);   //PWM Speed Control ( fast)
  analogWrite(P3, 250);   //PWM Speed Control (fast)
  analogWrite(P4, 250);   //PWM Speed Control ( fast)
   delay(2000); // delay 2 sec
   
  digitalWrite(M1,HIGH);   //direction control
  digitalWrite(M2,HIGH);   //direction control
  digitalWrite(M3,HIGH);   //direction control
  digitalWrite(M4,HIGH);   //direction control
  
  analogWrite(P1, 250);   //PWM Speed Control (fast)
  analogWrite(P2, 250);   //PWM Speed Control ( fast)
  analogWrite(P3, 250);   //PWM Speed Control (fast)
  analogWrite(P4, 250);   //PWM Speed Control ( fast)
     delay(2000); // delay 2 sec
   
  digitalWrite(M1,HIGH);   //direction control
  digitalWrite(M2,HIGH);   //direction control
  digitalWrite(M3,HIGH);   //direction control
  digitalWrite(M4,HIGH);   //direction control
  
  analogWrite(P1, 150);   //PWM Speed Control (slower)
  analogWrite(P2, 150);   //PWM Speed Control ( slower)
  analogWrite(P3, 150);   //PWM Speed Control (slower)
  analogWrite(P4, 150);   //PWM Speed Control ( slower)
  
  delay(2000); // delay 2 sec
   
  digitalWrite(M1,LOW);   //direction control
  digitalWrite(M2,LOW);   //direction control
  digitalWrite(M3,HIGH);   //direction control
  digitalWrite(M4,HIGH);   //direction control
  
  analogWrite(P1, 150);   //PWM Speed Control (slower)
  analogWrite(P2, 150);   //PWM Speed Control ( slower)
  analogWrite(P3, 150);   //PWM Speed Control (slower)
  analogWrite(P4, 150);   //PWM Speed Control ( slower)
  
     delay(2000); // delay 2 sec

} */

My problem is that once I press the up button on my remote as specified on the code (loop section, the first case), the Rover 5 goes forward and does not stop. How do i make it so that the Rover 5 goes forward until the OK button is pressed (see lower down the code)?
Any suggestions would be greatly appreciated.
Joccer

Just repeat the same code in the "ok" case as you have in the "up" case, but make all the Mx outputs LOW.

(It's the HIGH/LOW pairs on each motor that provide direction. If you swap the HI/LOW for LOW/HI you'll get the other direction. Check the driver data sheet: depending on which chip, you may find that LOW/LOW like I suggested on each motor will be a coast, but HI/HI will brake.)

Thank you for your speedy response :) , However when I tried this it did not work. I copied the commands from under the Up case and did as you said. I tried both all LOWs and all HIGHs. Is there another way that would stay within the Up case to trigger the "break;" command by pressing the OK button? Alternatively, is there a way to make the robot go forward for 3 seconds and then initiate the "break;" command?

Thanks Joccer

First you should use Serial to print out some messages from inside all those cases, so you can be satisfied it’s actually going where you think.

Stick a…

Serial.begin(9600);

… in setup().

Then in the “ok” case, have…

case 0xFF02FD: // OK button pressed
Serial.println("In the OK part");
break;

Right now, you have no evidence that it’s actually reacting to the remote’s code.

Hi,
I have done this, however with no luck. The code will not leave the ‘Forward’ case - I need some sort of code that will leave that case when I press the OK button. Any idea???

Joccer

Well what made you think the OK produced a 0xFF02FD in the first place?

I had watched a video of someone using the exact same remote and downloaded their code. Also, just to check this I manually recorded the signals that I was receiving using another code that I downloaded, and they were correct.