Control my robot with infrared remote

Posted on 22/04/2013 by newbies
Modified on: 13/09/2018
Project
Press to mark as completed
Introduction
This is an automatic import from our previous community platform. Some things can look imperfect.

If you are the original author, please access your User Control Panel and update it.

I built some time ago a robot that can move by avoiding obstacles thanks to its sensor. Then add to the robot an infrared receiver so that it can be controlled thanks to an infrared remote control. But after having import the code (see below) the robot start to move straight forward without stopping. I want the robot to move in autonomous mode from the remote. I control my motors thanks to a L293D, I'm building another shield based on a L298N I'm french so the code is in french,here some help for ...


Control my robot with infrared remote

I built some time ago a robot that can move by avoiding obstacles thanks to its sensor. Then add to the robot an infrared receiver so that it can be controlled thanks to an infrared remote control. But after having import the code (see below) the robot start to move straight forward without stopping. I want the robot to move in autonomous mode from the remote.

I control my motors thanks to a L293D, I'm building another shield based on a L298N

I'm french so the code is in french,here some help for you :

Enavant = Go Forward Tourneragauche = Turn Left Enarriere = Go Backward

Tourneradroite = Turn Right

Modeautonome = Autonomous mode

Arret = Stop

Here is the code : 

 

 

 #include <DistanceSRF04.h>
#include <IRremote.h>
int action;
int IRdelay = 40 ;
unsigned long Enavant = 0xA16E6C93 ;
unsigned long Tourneragauche = 0xA16E40BF ;
unsigned long Enarriere = 0xA16EE817 ;
unsigned long Tourneradroite = 0xA16E00FF ;
unsigned long Modeautonome = 0xA16EC03F ;
unsigned long Arret = 0xA16E807F ;
int Avant ;
int Arriere ;
int TGauche ;
int TDroite ;
int Autonome ;
int Stop ;
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results
;
DistanceSRF04 Dist;
int distance;
int motor1Pin1 = 3; // pin 2 on L293D
int motor1Pin2 = 4;
int motor2Pin1 = 5; // pin 2 on L293D
int motor2Pin2 = 6; // pin 7 on L293D
int enablePin = 9;
int enable2Pin = 10;// pin 1 on L293D


void setup()
{
 
Serial.begin(9600);
  irrecv
.enableIRIn();
 
 
Dist.begin(12,13);


pinMode
(motor1Pin1, OUTPUT);
pinMode
(motor1Pin2, OUTPUT);
pinMode
(motor2Pin1, OUTPUT);
pinMode
(motor2Pin2, OUTPUT);
pinMode
(enablePin, OUTPUT);
pinMode
(enable2Pin, OUTPUT);
}


/**
 * Les fonctions ci dessous te permettent de ne pas répéter le code correspondant aux divers endroits. ça t'évite de devoir faire des copier coller.
 * De plus, si tu utilises cette fonction, et que tu veux modifier les noms des variables ou autre, tu n'as qu'à modifier ici, et ce sera bon dans toute la suite du code!
 * De manière générale, c'est une bonne habitude de faire des sous fonctions, et de t'appuyer dessus. Tu commences par une sous fonction, que tu testes à fond.
 * Quand tu es sur que la sous fonction marche bien, tu peux t'en servir dans une nouvelle fonction, et ainsi de suite. ça te permet de valider à chaque étape.
 * En plus ça fait un code propre, et clair, plus facile à lire!
 */

void autonome(){
 
{
distance
= Dist.getDistanceCentimeter();
Serial.print("\nDistance in centimers: ");
Serial.print(distance);
delay
(200); //make it readable
}
if (distance <= 30 ){
  tournerGauche
();
}
else {
  avancer
();
}
}
 

void startMotors()//démarre les moteurs
{
    digitalWrite
(enablePin, HIGH);
    digitalWrite
(enable2Pin, HIGH);
}

void stopMotors()//arrête les moteurs
{
    digitalWrite
(enablePin, LOW);
    digitalWrite
(enable2Pin, LOW);
}

void avancer() //le robot avancera jusqu'à nouvel ordre
{
    startMotors
();
    digitalWrite
(motor1Pin1, LOW); // set pin 2 on L293D low
    digitalWrite
(motor1Pin2, HIGH); // set pin 7 on L293D high
    digitalWrite
(motor2Pin1, LOW); // set pin 2 on L293D low
    digitalWrite
(motor2Pin2, HIGH);
}

void tournerGauche() //le robot tournera à gauche jusqu'à nouvel ordre
{
    startMotors
();
    digitalWrite
(motor1Pin1, LOW); // set pin 2 on L293D low
    digitalWrite
(motor1Pin2, HIGH); // set pin 7 on L293D high
    digitalWrite
(motor2Pin1, HIGH); // set pin 2 on L293D low
    digitalWrite
(motor2Pin2, LOW);
}

void tournerDroite()//le robot tournera à droite jusqu'à nouvel ordre
{
   startMotors
();
   digitalWrite
(motor1Pin1, HIGH); // set pin 2 on L293D low
   digitalWrite
(motor1Pin2, LOW); // set pin 7 on L293D high
   digitalWrite
(motor2Pin1, LOW); // set pin 2 on L293D low
   digitalWrite
(motor2Pin2, HIGH);
}

void reculer()//le robot reculera jusqu'à nouvel ordre
{
    startMotors
();
    digitalWrite
(motor1Pin1, HIGH); // set pin 2 on L293D low
    digitalWrite
(motor1Pin2, LOW); // set pin 7 on L293D high
    digitalWrite
(motor2Pin1, HIGH); // set pin 2 on L293D low
    digitalWrite
(motor2Pin2, LOW);
}

void loop()//boucle principale du programme
{
   
if (irrecv.decode(&results))
   
{
   
Serial.println(results.value, HEX);
    irrecv
.resume(); // Receive the next value
 
}
 
 
if(results.value == Enavant)
 
{
     action
= Avant ; //attention, tu avais mis action == Avant. ça n'aurait pas mis la valeur "Avant" dans action. == ça sert à voir si la valeur de gauche est égale à la valeur de droite. = tout seul sert à mettre la valeur de droite dans la variable de gauche. Attention à ne pas se tromper, ça ne fera pas d'érreur de compilation, mais le programme ne fonctionnera pas comme tu veux!
 
}
 
else if(results.value == Tourneragauche)
 
{
    action
= TGauche ;
 
}
 
else if(results.value == Tourneradroite)
 
{
     action
= TDroite ;
 
}
 
else if(results.value == Arriere)
 
{
     action
= Arriere ;
 
}
 
else if(results.value == Arret)//cas ou tu veux arrêter le robot dans son déplacement!
 
{
     action
= Stop ;
 
}
 
else if(results.value == Autonome)
 
{
     action
= Autonome ;
 
}

   effectueraction
(action);//si tu ne mets pas ça ici, le robot ne fera rien! dans ton exemple il aurait simplement avancé en continu.

}//ne pas oublier de fermer la fonction, il te manquait cela. Tu ne dois pas déclarer une fonction au sein d'une autre, à moins de savoir précisément pourquoi. Ici, tu n'en as pas besoin.
   
   
     
   
void effectueraction(int act)//tu avais oublié les parenthèses pour la fonction!!!
   
{  
   
if (act == Avant)
     
{
        avancer
();
     
}
     
     
else if ( act == TGauche)
     
{
       tournerGauche
();
     
}
     
     
else if ( act == TDroite )
     
{
       tournerDroite
();
     
}
     
     
else if ( act == Arriere)
     
{
       reculer
();
     
}
     
else if ( act == Stop)
     
{
       stopMotors
();
     
}
     
else if(act == Autonome)
     
{
      autonome
();
     
}

   
}

 

  • Actuators / output devices: Tamiya twin gearbox
  • Control method: Autonomous. Manual infra red control planned.
  • CPU: Arduino
  • Power source: 9 volts and 4.8 volts for the motor
  • Sensors / input devices: HC-SR04 ultrasonic sensor
LikedLike this to see more

Spread the word

Flag this post

Thanks for helping to keep our community civil!


Notify staff privately
It's Spam
This post is an advertisement, or vandalism. It is not useful or relevant to the current topic.

You flagged this as spam. Undo flag.Flag Post