The program mainly refer to the following :
Here is the working code (stop at 800ms after each action)
//............ Import Library ...............
//
#include <IRremote.h> // includes the IR Library - from Ken Shirriff's blog
//
//............ Declare PIN Variables ............
//
const int FrontLED = 13;
//............ LEFT Gear Motor .................
const int MotorA1Pin =4;
const int MotorA2Pin = 2;
const int MotorENAPin = 3;
//............ RIGHT Gear Motor ................
const int MotorB1Pin = 8;
const int MotorB2Pin = 7;
const int MotorENBPin = 5;
//............ IR Receiver Pin ..................
int RECV_PIN = 12;
IRrecv irrecv(RECV_PIN);
decode_results results;
//
//............ Arduino Setup ................
//
void setup()
{
// Serial.begin(9600);
//............ Configure pins ...............
pinMode(FrontLED,OUTPUT);
//............ LEFT Gear Motor .................
pinMode(MotorA1Pin,OUTPUT);
pinMode(MotorA2Pin,OUTPUT);
pinMode(MotorENAPin,OUTPUT);
//............ RIGHT Gear Motor ................
pinMode(MotorB1Pin,OUTPUT);
pinMode(MotorB2Pin,OUTPUT);
pinMode(MotorENBPin,OUTPUT);
//............ Enable Motors ...............
digitalWrite(MotorENAPin, HIGH);
digitalWrite(MotorENBPin, HIGH);
//............ Start IR receiver ............
irrecv.enableIRIn();
}
//
//............ Arduino Loop .................
//
void loop() {
//............ IR signal received ...........
if(irrecv.decode(&results))
{
//............ IR Input - 2 - Tank Motion _Forward
if(results.value==0xFD8877)
{
motor(0,1,1,0);
}
//............ IR Input - 8 - Tank Motion_Backward
else if(results.value==0xFD9867)
{
motor(1,0,0,1);
}
//............ IR Input - 4 - Tank Motion_Left
else if(results.value==0xFD28D7)
{
motor(0,1,0,0);
}
//............ IR Input - 6 - Tank Motion_Right
else if(results.value==0xFD6897)
{
motor(0,0,1,0);
}
//............ IR Input - 1 - Tank Motion_Fast Left
else if(results.value==0xFD08F7)
{
motor(0,1,0,1);
}
//............ IR Input - 3 - Tank Motion_Fast Right
else if(results.value==0xFD48B7)
{
motor(1,0,1,0);
}
//............ IR Input - 7 - Tank motion Right_backward
else if(results.value==0xFD18E7)
{
motor(0,0,0,1);
}
//............ IR Input - 9 - Tank motion Left_backward
else if(results.value==0xFD58A7)
{
motor(1,0,0,0);
}
//............ IR Input - 5 - Stop Motion
else if(results.value==0xFDA857)
{
motor(0,0,0,0);
}
//............ IR Input - EQ - Turn on the LED
else if(results.value==0xFDB04F)
{
if (digitalRead(FrontLED)==HIGH)
{
digitalWrite(FrontLED,LOW);
}
else
{
digitalWrite(FrontLED,HIGH);
}
}
//............ Receive the next value ...............
irrecv.resume();
//............ Short delay waiting for repeating IR signal
delay(300);
}
}
//
// Function to control the motor
// Non-return value subroutine
//
void motor(int MotorA1, int MotorA2, int MotorB1, int MotorB2) {
digitalWrite(MotorENAPin, HIGH);
digitalWrite(MotorENBPin, HIGH);
if (MotorA1==0) digitalWrite(MotorA1Pin,LOW);
if (MotorA2==0) digitalWrite(MotorA2Pin,LOW);
if (MotorB1==0) digitalWrite(MotorB1Pin,LOW);
if (MotorB2==0) digitalWrite(MotorB2Pin,LOW);
if (MotorA1==1) digitalWrite(MotorA1Pin,HIGH);
if (MotorA2==1) digitalWrite(MotorA2Pin,HIGH);
if (MotorB1==1) digitalWrite(MotorB1Pin,HIGH);
if (MotorB2==1) digitalWrite(MotorB2Pin,HIGH);
delay(800);
digitalWrite(MotorENAPin, LOW);
digitalWrite(MotorENBPin, LOW);
}
The following is the code which hang after receive any motor action. (Same code but different in the last paragraph)
//............ Import Library ...............
//
#include <IRremote.h> // includes the IR Library - from Ken Shirriff's blog
//
//............ Declare PIN Variables ............
//
const int FrontLED = 13;
//............ LEFT Gear Motor .................
const int MotorA1Pin =4;
const int MotorA2Pin = 2;
const int MotorENAPin = 3;
//............ RIGHT Gear Motor ................
const int MotorB1Pin = 8;
const int MotorB2Pin = 7;
const int MotorENBPin = 5;
//............ IR Receiver Pin ..................
int RECV_PIN = 12;
IRrecv irrecv(RECV_PIN);
decode_results results;
//
//............ Arduino Setup ................
//
void setup()
{
// Serial.begin(9600);
//............ Configure pins ...............
pinMode(FrontLED,OUTPUT);
//............ LEFT Gear Motor .................
pinMode(MotorA1Pin,OUTPUT);
pinMode(MotorA2Pin,OUTPUT);
pinMode(MotorENAPin,OUTPUT);
//............ RIGHT Gear Motor ................
pinMode(MotorB1Pin,OUTPUT);
pinMode(MotorB2Pin,OUTPUT);
pinMode(MotorENBPin,OUTPUT);
//............ Enable Motors ...............
digitalWrite(MotorENAPin, HIGH);
digitalWrite(MotorENBPin, HIGH);
//............ Start IR receiver ............
irrecv.enableIRIn();
}
//
//............ Arduino Loop .................
//
void loop() {
//............ IR signal received ...........
if(irrecv.decode(&results))
{
//............ IR Input - 2 - Tank Motion _Forward
if(results.value==0xFD8877)
{
motor(0,1,1,0);
}
//............ IR Input - 8 - Tank Motion_Backward
else if(results.value==0xFD9867)
{
motor(1,0,0,1);
}
//............ IR Input - 4 - Tank Motion_Left
else if(results.value==0xFD28D7)
{
motor(0,1,0,0);
}
//............ IR Input - 6 - Tank Motion_Right
else if(results.value==0xFD6897)
{
motor(0,0,1,0);
}
//............ IR Input - 1 - Tank Motion_Fast Left
else if(results.value==0xFD08F7)
{
motor(0,1,0,1);
}
//............ IR Input - 3 - Tank Motion_Fast Right
else if(results.value==0xFD48B7)
{
motor(1,0,1,0);
}
//............ IR Input - 7 - Tank motion Right_backward
else if(results.value==0xFD18E7)
{
motor(0,0,0,1);
}
//............ IR Input - 9 - Tank motion Left_backward
else if(results.value==0xFD58A7)
{
motor(1,0,0,0);
}
//............ IR Input - 5 - Stop Motion
else if(results.value==0xFDA857)
{
motor(0,0,0,0);
}
//............ IR Input - EQ - Turn on the LED
else if(results.value==0xFDB04F)
{
if (digitalRead(FrontLED)==HIGH)
{
digitalWrite(FrontLED,LOW);
}
else
{
digitalWrite(FrontLED,HIGH);
}
}
//............ Receive the next value ...............
irrecv.resume();
//............ Short delay waiting for repeating IR signal
delay(300);
}
}
//
// Function to control the motor
// Non-return value subroutine
//
void motor(int MotorA1, int MotorA2, int MotorB1, int MotorB2) {
if (MotorA1==0) digitalWrite(MotorA1Pin,LOW);
if (MotorA2==0) digitalWrite(MotorA2Pin,LOW);
if (MotorB1==0) digitalWrite(MotorB1Pin,LOW);
if (MotorB2==0) digitalWrite(MotorB2Pin,LOW);
if (MotorA1==1) digitalWrite(MotorA1Pin,HIGH);
if (MotorA2==1) digitalWrite(MotorA2Pin,HIGH);
if (MotorB1==1) digitalWrite(MotorB1Pin,HIGH);
if (MotorB2==1) digitalWrite(MotorB2Pin,HIGH);
}