Step #9 : IR Receiver (KY-022) and drive control using the infrared remote control

arduino4fun
Site Admin
Posts: 37
Joined: Mon Jun 06, 2016 1:16 pm

Step #9 : IR Receiver (KY-022) and drive control using the infrared remote control

Postby arduino4fun » Thu Jun 09, 2016 9:23 am

IR Receiver (KY-022) and drive control using the infrared remote control : this test shows how to use the infrared remote control to pilot the robot forward/backward/turn left/turn right; I haven't used the stock remote control because it's a poor quality !! the transmitted codes are not always the same; there is sometime a delay between 2 transmissions or there is no transmission at all ! so, I have decided to use Sony TV remote control which works like a charm; you only need to configure the keys according to your own R.C.

Here is the video of the test : https://youtu.be/mD2bttT5Y2o

Code: Select all

// ---------------------------------------------------------------------------------------------------------------
 // This sketch allows to check the infrared sensor and the remote control to pilot the robot
 //
 // L298 ---- Sensor Shield V5
 // ENA -- 5 - yellow
 // IN1 -- 2 - grey
 // IN2 -- 4 - green
 // IN3 -- 7 - mauve
 // IN4 -- 8 - orange
 // ENB -- 10 - blue
 // GND black
 // +5V red
 //
 // INFRARED --- infrared receiver
 // S -- 12 orange
 // GND black
 // +5V red
 //
 //
 // https://www.facebook.com/arduino4fun
 // http://www.arduino4fun.com
 // ---------------------------------------------------------------------------------------------------------------

‪#‎include‬ <IRremote.h>

// ************ L298 & motor control section **********************************************
 int pinLB=2; // Steering wheel control ---- motor driver board IN1
 int pinLF=4; // Steering wheel control ---- motor driver board IN2
 int pinRB=7; // Steering wheel control ---- motor driver board IN3
 int pinRF=8; // Steering wheel control ---- motor driver board IN4

// Movements
 int Lpwm_pin = 10; // Motor vehicle speed control pin driver board ENB
 int Rpwm_pin = 5; // Motor vehicle speed control pin driver board ENA

// car state
 int Car_state=0;

‪#‎define‬ _FORWARD 1
 #define _BACKWARD 2
 #define _TURNRIGHT 3
 #define _TURNLEFT 4
 #define _STOP 5

#define SONY_TV_REMOTECONTROL
 //#define STOCK_REMOTECONTROL

// ************ infrared section **********************************************************

int RECV_PIN = 12; // the IR is connected to pin analog 12
 IRrecv irrecv(RECV_PIN);
 decode_results results;

‪#‎ifdef‬ SONY_TV_REMOTECONTROL
 long goAdvance = 0xE8455D8E; // key '2' to go forward
 long goBack = 0xC67C01B6; // key '8' to go backward
 long goStop = 0x905ED4F5; // key '5' to stop
 long goLeft = 0x2C1F3172; // key '4' to go to the left
 long goRight = 0x8CE1E3FC; // key '6' to go to the right
‪#‎endif‬
#ifdef STOCK_REMOTECONTROL
 long goAdvance = 0xFF18E7; // key '2' to go forward
 long goBack = 0xFF4AB5; // key '8' to go backward
 long goStop = 0xFF38C7; // key '5' to stop
 long goLeft = 0xFF10EF; // key '4' to go to the left
 long goRight = 0xFF5AA5; // key '6' to go to the right
 #endif

long samecmd = 0xFFFFFFFF; // same command, i.e. the button is kept pressed

bool on = false;
 unsigned long last = millis();

// ****************************************************************************************
 // ****************************************************************************************

void forward(int val=0) // Forward
 {
 digitalWrite(pinRB,HIGH); // The motor ( right rear ) Action
 digitalWrite(pinRF,LOW);
 digitalWrite(pinLB,HIGH); // The motor ( rear left ) Action
 digitalWrite(pinLF,LOW);
 delay( val * 100);
 Car_state = 1;
 show_state();
 }

void turnL(int val=0) // Turn left
 {
 digitalWrite(pinRB,LOW); // The motor ( right rear ) Action
 digitalWrite(pinRF,HIGH);
 digitalWrite(pinLB,HIGH);
 digitalWrite(pinLF,LOW); // The motor (front left ) Action
 delay( val * 100);
 Car_state = 4;
 show_state();
 }

void turnR(int val=0) // Turn right

{
 digitalWrite(pinRB,HIGH);
 digitalWrite(pinRF,LOW); // The motor ( front right ) action
 digitalWrite(pinLB,LOW); // The motor ( rear left ) Action
 digitalWrite(pinLF,HIGH);
 delay( val * 100);
 Car_state = 3;
 show_state();
 }

void stopp(int val=0) // Stop
 {
 digitalWrite(pinRB,HIGH);
 digitalWrite(pinRF,HIGH);
 digitalWrite(pinLB,HIGH);
 digitalWrite(pinLF,HIGH);
 delay( val * 100);
 Car_state = 5;
 show_state();
 }

void backward(int val=0) // Backward
 {
 digitalWrite(pinRB,LOW); // The motor ( right rear ) Action
 digitalWrite(pinRF,HIGH);
 digitalWrite(pinLB,LOW); // The motor ( rear left ) Action
 digitalWrite(pinLF,HIGH);
 delay( val * 100);
 Car_state = 2;
 show_state() ;
 }

void ConfigPins()
 {
 pinMode(pinLB,OUTPUT); // Pin 2
 pinMode(pinLF,OUTPUT); // Pin 4
 pinMode(pinRB,OUTPUT); // Pin 7
 pinMode(pinRF,OUTPUT); // Pin 8
 pinMode(Lpwm_pin,OUTPUT); // Pin 10 (PWM)
 pinMode(Rpwm_pin,OUTPUT); // Pin 5 (PWM)
 }

void Set_Speed(unsigned char Left,unsigned char Right)
 {
 analogWrite(Lpwm_pin,Left);
 analogWrite(Rpwm_pin,Right);
 }

void show_state(void)
 {
 Serial.print("State : ");
 switch(Car_state)
 {
 case 1:
 //lcd.print(" Go ");
 Serial.print("Go");
 break;
 case 2:
 //lcd.print(" Back ");
 Serial.print("Back");
 break;
 case 3:
 //lcd.print(" Left ");
 Serial.print("Left");
 break;
 case 4:
 //lcd.print(" Right");
 Serial.print("Right");
 break;
 case 5:
 //lcd.print(" stop ");
 Serial.print("Stop");
 break;
 default:
 break;
 }
 Serial.println();
 }

// Display the type of infrared protocol
 void showIRProtocol(decode_results *results)
 {
 Serial.print ("Protocol:");
 // protocols types
 switch (results->decode_type)
 {
 case NEC:
 Serial.print ("NEC");
 break;
 case SONY:
 Serial.print ("SONY");
 break;
 case RC5:
 Serial.print ("RC5");
 break;
 case RC6:
 Serial.print ("RC6");
 break;
 case DISH:
 Serial.print ("DISH");
 break;
 case SHARP:
 Serial.print ("SHARP");
 break;
 case PANASONIC:
 Serial.print ("PANASONIC");
 break;
 case JVC:
 Serial.print ("JVC");
 break;
 case SANYO:
 Serial.print ("SANYO");
 break;
 case MITSUBISHI:
 Serial.print ("MITSUBISHI");
 break;
 default:
 Serial.print ("Unknown protocol");
 }
 // Print the infrared codes to Serial port
 Serial.print (", irCode:");
 Serial.print (results-> value, HEX); // Infrared code
 Serial.print (", bits: ");
 Serial.println (results-> bits); // Number of bits coded infrared
 }

void dump(decode_results *results)
 {
 int count = results->rawlen;
 if (results->decode_type == UNKNOWN)
 {
 Serial.println ("Could not decode message");
 }
 else
 {
 if (results->decode_type == NEC)
 {
 Serial.print ("Decoded 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:");
 }
 else if (results->decode_type == DISH)
 {
 Serial.print ("Decoded DISH:");
 }
 else if (results->decode_type == SHARP)
 {
 Serial.print ("Decoded SHARP:");
 }
 else if (results->decode_type == PANASONIC)
 {
 Serial.print ("Decoded PANASONIC:");
 }
 else if (results->decode_type == JVC)
 {
 Serial.print ("Decoded JVC:");
 }
 else if (results->decode_type == SANYO)
 {
 Serial.print ("Decoded SANYO:");
 }
 else if (results->decode_type == MITSUBISHI)
 {
 Serial.print ("Decoded MITSUBISHI:");
 }
 Serial.print (results->value, HEX);
 Serial.print ("(");
 Serial.print (results->bits, DEC);
 Serial.println ("bits)");
 }
 Serial.print ("Raw (");
 Serial.print (count, DEC);
 Serial.print ("):");
 for (int i = 0; i<count; i++)
 {
 if ((i% 2) == 1)
 {
 Serial.print(results->rawbuf [i] * USECPERTICK, DEC);
 }
 else
 {
 Serial.print(-(int)results->rawbuf [i] * USECPERTICK, DEC);
 }
 Serial.print ("");
 }
 Serial.println ("");
 }

void ExecutePreviousCmd( unsigned long cmd=0 )
 {
 switch( Car_state )
 {
 case _FORWARD:
 forward();
 break;
 case _BACKWARD:
 backward();
 break;
 case _TURNLEFT:
 turnL();
 break;
 case _TURNRIGHT:
 turnR();
 break;
 case _STOP:
 stopp();
 break;
 }
 }

void ExecuteCmd( unsigned long cmd=0, int le=0 )
 {
 if (results.value == goAdvance)
 {
 forward(le);
 }
 if (results.value == goBack)
 {
 backward(le);
 }
 if (results.value == goLeft)
 {
 turnL(le);
 }
 if (results.value == goRight)
 {
 turnR(le);
 }
 if (results.value == goStop)
 {
 stopp();
 }
 }

void ShowReceivedCmd( char* txt, unsigned long cmd )
 {
 Serial.print( txt );
 Serial.println( cmd, HEX );
 }

bool IsCmdOk( unsigned long cmd )
 {
 if( cmd==goAdvance || cmd==goBack || cmd==goStop || cmd==goLeft || cmd==goRight /*|| cmd==samecmd*/)
 return true;
 return false;
 }

void setup()
 {
 Serial.begin(9600);
 Serial.println("########## SETUP STARTED ################# ²²²²²");
ConfigPins();
 delay(500);
 Set_Speed(3000,3000);
 forward(3);
 stopp();
 irrecv.enableIRIn(); // Start the receiver
 Serial.println("########## SETUP COMPLETED ###############");
 }

void loop()
 {
‪#‎if‬ _0 // simply display the type of the receiver and the keycode
 if (irrecv.decode (&results))
 { // Decoding is successful, you receive a set of infrared signals
 Serial.println("code received");
 showIRProtocol (&results); // Display the type of infrared protocol
 //if(results.decode_type!=NEC)
 // return;
 //dump(&results);
 irrecv.resume (); // Continue to accept a set of infrared signals
 //delay(300);
 }
 return;
 #endif
 if (irrecv.decode (&results))
 {
 // IR received, toggle the relay
 //if (millis() - last> 250)
 //{
 // on != on;
 // digitalWrite (13, on?HIGH:LOW);
 // dump(&results);
 //}
 //ShowReceivedCmd( "results.value = ", results.value );
 //ShowReceivedCmd( "results.value = ", results.value );
 //if( results.value == samecmd )
 // ExecutePreviousCmd();
 //else
 {
 ExecuteCmd( results.value, 1 );
 stopp();
 }
 last = millis ();
 irrecv.resume (); // Receive the next value
 }
 else
 {
 //ExecutePreviousCmd();
 //if( IsCmdOk( results.value )==true )
 //{
 // if( Car_state!=_STOP )
 // stopp();
 //}
 }
 }


Image
Image

Return to “Multifunction Bluetooth Controlled Robot Smart Car Kits For Arduino”

Who is online

Users browsing this forum: No registered users and 1 guest