// RC Car ver2.

#include

AF_DCMotor motor4(4, MOTOR12_1KHZ); // create motor #4, 1KHz pwm

AF_DCMotor motor3(3, MOTOR12_1KHZ); // create motor #3, 1KHz pwm

int receiverpin = 2; // OUT pin of IR receiver to Arduino digital pin 2

#include

IRrecv irrecv(receiverpin);

decode_results results;

void setup() {

irrecv.enableIRIn();

}

void goForward()

{

 //go forward

  motor3.setSpeed(255);

  motor4.setSpeed(255);

  motor3.run(FORWARD);

  motor4.run(FORWARD);

  delay(500);

  motor3.run(RELEASE);

  motor4.run(RELEASE);

}

void goBackward()

{

  // go backward

  motor3.setSpeed(255);

  motor4.setSpeed(255);

  motor3.run(BACKWARD);

  motor4.run(BACKWARD);

  delay(500);

  motor3.run(RELEASE);

  motor4.run(RELEASE);

}

void turnRight()

{

  // turn right

  motor3.setSpeed(192);

  motor4.setSpeed(255);

  motor3.run(FORWARD);

  motor4.run(FORWARD);

  delay(500);

  motor3.run(RELEASE);

  motor4.run(RELEASE);

}

void turnLeft()

{

  // turn left

  motor3.setSpeed(255);

  motor4.setSpeed(192);

  motor3.run(FORWARD);

  motor4.run(FORWARD);

  delay(500);

  motor3.run(RELEASE);

  motor4.run(RELEASE);

}

void turnRightBK()

{

  // turn rightBK

  motor3.setSpeed(192);

  motor4.setSpeed(255);

  motor3.run(BACKWARD);

  motor4.run(BACKWARD);

  delay(500);

  motor3.run(RELEASE);

  motor4.run(RELEASE);

}

void turnLeftBK()

{

  // turn leftBK

  motor3.setSpeed(255);

  motor4.setSpeed(192);

  motor3.run(BACKWARD);

  motor4.run(BACKWARD);

  delay(500);

  motor3.run(RELEASE);

  motor4.run(RELEASE);

}

void twistLeft()

{

  // turn twistLeft

  motor3.setSpeed(255);

  motor4.setSpeed(255);

  motor3.run(FORWARD);

  motor4.run(BACKWARD);

  delay(500);

  motor3.run(RELEASE);

  motor4.run(RELEASE);

}

void twistRight()

{

  // turn twistRight

  motor3.setSpeed(255);

  motor4.setSpeed(255);

  motor4.run(FORWARD);

  motor3.run(BACKWARD);

  delay(500);

  motor3.run(RELEASE);

  motor4.run(RELEASE);

}

void translateIR()

{

  switch(results.value)

  {

   

    case 0x9D62807F: goForward(); break;  // Go Forward straight

    case 0x9D6240BF: goForward(); break;  // Go Forward straight

    case 0x9D62C03F: goBackward(); break; // Go Backward straight

    case 0x9D6220DF: goBackward(); break; // Go Backward straight

    case 0x9D62A05F: turnRight(); break;  // Go Forward and turn Right

    case 0x9D62609F: turnRight(); break;  // Go Forward and turn Right

    case 0x9D62E01F: turnLeft(); break;   // Go Forward and turn Left

    case 0x9D6210EF: turnLeft(); break;   // Go Forward and turn Left

    case 0x9D62906F: turnLeftBK(); break; // GO Backward and turn Left

    case 0x507: turnLeftBK(); break; // GO Backward and turn Left

    case 0xD09: turnRightBK(); break; // Go Backward and turn Right

    case 0x509: turnRightBK(); break; // Go Backward and turn Right

    case 0xD01: twistRight(); break; // twist Right

    case 0x501: twistRight(); break; // twist Right

    case 0xD03: twistLeft(); break; // twist Left

    case 0x503: twistLeft(); break; // twist Left

  }

}

void loop()

{

 

  if (irrecv.decode(&results))

  {

    translateIR();

    irrecv.resume();

   

  }

}