Robot problem

Hello everybody,
I recently programmed a little robot with an arduino, a official motor shield, an HC-SE04, two motors, etc…
I managed to make it totally autonomous but then I decided to add a way to control the robot thanks to a remote control. However, in my new code, when the instruction “irrecv.enableIRIn();” is in the code, the two motors do not want to work (But the others things like DEL for example are steel working). If I remove this instruction, the motor restart, but I need it to control the robot with remote control…
Here is my code:

  //Radiocommande:
          #include <IRremote.h>
          int RECV_PIN = 10;
          IRrecv irrecv(RECV_PIN);
          decode_results results;

  //Capteur HC-SR04:             ( VCC=5v; GND=GND; Echo=7; Trig=6)
          #define echoPin 7 // Echo Pin
          #define trigPin 6 // Trigger Pin
          int maximumRange = 150; // Maximum range needed
          int minimumRange = 0; // Minimum range needed
          long duration, distance; // Durée que l'on utilise pour calculer la distance

void setup()
{
  //Affichage sur l'ordinateur:
          Serial.begin(9600);
          
  //Radiocommande:
          irrecv.enableIRIn(); // Active le récepteur IR    Stop the motors!!!!!!!

  //LED:
          pinMode(2,OUTPUT); //Led rouge qui brillera quand distance <= 15 cm
          pinMode(4,OUTPUT); //Led verte qui brillera quand ça avance

  //Capteur HC-SR04:
          pinMode(trigPin, OUTPUT);
          pinMode(echoPin, INPUT);
 
  //Moteur A:
          pinMode(9, OUTPUT);
          pinMode(12, OUTPUT);
    
  //Moteur B:
          pinMode(13, OUTPUT);
          pinMode(8, OUTPUT);
  
}

void loop() {
          if (irrecv.decode(&results)) {  //Si on appui sur un bouton
                    Serial.println(results.value, HEX); //Affiche la valeur en hexadécimal du bouton pressé
                    
                    if (results.value==0xFFE21D){ //Si le bouton pressé est celui CH+
                      //Robot mit en fonctionnement automatique
                          //LEDS:
                                digitalWrite(4,HIGH); //LED verte allumée en temps normal
                                digitalWrite(2,LOW); //LED rouge éteinte en temps normal

                          //Motor A:
                                digitalWrite(12, LOW); //Direction (Forward ici)
                                digitalWrite(9, LOW);   //Frein (Disengage ici)
                                analogWrite(3, 120);   //Valeur pour vitesse
                      
                          //Motor B:
                                digitalWrite(13, LOW);  //Direction (Forward ici)
                                digitalWrite(8, LOW);   //Frein (Disengage ici)
                                analogWrite(11, 117);    //Valeur pour vitesse
                       
                          //Capteur HC-SR04:
                                digitalWrite(trigPin, LOW); 
                                delayMicroseconds(2);
                                digitalWrite(trigPin, HIGH);
                                delayMicroseconds(10); 
                                digitalWrite(trigPin, LOW);
                                duration = pulseIn(echoPin, HIGH);
                                distance = duration/58.2; //Calcul de la distance en cm à l'aide de la vitesse du song
                       
                      
                          if (distance >= maximumRange || distance <= minimumRange){
                      
                                Serial.println("-"); //"-" s'affiche à l'écran de l'ordinateur lorsque le capteur HC-SR04 ne détecte pas la distance ou que celle-ci est hors de l'intervalle de tolérance
                          }
                          
                          else { //La distance est correctement mesurée par le capteur
                            
                                Serial.println(distance); //La distance est affiché en cm à l'écran de l'ordinateur
                                
                                if (distance <=15){ 
                                        //LEDS:
                                            digitalWrite(2,HIGH); //Allume la LED rouge
                                            digitalWrite(4,LOW); //Eteint la LED verte
                      
                                   
                       
                                        //Recul:
                                            digitalWrite(12, HIGH); //Direction A (Backward ici)
                                            digitalWrite(9, LOW);   //Frein A (Disengage ici)
                                            analogWrite(3, 100);   //Valeur A pour vitesse
                                            digitalWrite(13, HIGH);  //Direction B (Backward ici)
                                            digitalWrite(8, LOW);   //Frein B (Disengage ici)
                                            analogWrite(11, 100);    //Valeur A pour vitesse
                                            digitalWrite(2,HIGH); //Allume la LED rouge
                                            delay(500); //Temps de recule
                                         
                                       
                      
                                        //Rotation:
                                           (...)
                      
                                 
                      
                                                  }
                                else{ //Dans le cas où la distance mesurée est suffisamment grande pour continuer d'avancer
                                            //Ne change pas les instructions intiales données en premier
                                    }
                                }
                                
                            delay(70); //Temps avant une nouvelle acquisition de la distance (Une faible valeur permet de meilleur résultats, ie moins d'obstacle pris dans la gueule
                                            
                                          }

 (...)

                                          
              
                                          }
                                          irrecv.resume(); // Receive the next value
                                          }
                                          
                                        }

Thanks for your help! :slight_smile:

IRremote.h uses by default timer2

timer2 controls PWM on pin 9 and 10

  //Moteur A:
          pinMode([color=red]9[/color], OUTPUT);
          pinMode(12, OUTPUT);

--> so you are conflicting with one of your Motor pin.

change the timer for IR or change the pins for your motor

The matter is that I use a shield motor so the motor pin are already chose, I can’t change them… :confused:

J-M-L:
timer2 controls PWM on pin 9 and 10

I don’t realy understand, I am already on the pin 10 for my remote control…? Which one I have to choose for it? :slight_smile:

so you need to change the timer in the IR Library. check at the end of this file https://github.com/z3t0/Arduino-IRremote/blob/master/boarddefs.h depending on your Arduino Board

Could you be more specific please?
First I have 2 libraries, one whose name is "Arduino-IRremote-master" and another whose name is "IRremote", which one I have to modify?
Then in each one, I have several document but I don't realy know which file I have to modify, and when I want to open one of them, Arduino tell me that it is impossible to do it...
I can only open it with an another text software but if I modified the file, I can't replace the hold one...
Help me please...

I gave you the link above to Ken Shirriff’s library / Paul Stoffregen modification and the specific file to change

Depending on which arduino you have you’ll need to comment/uncomment one of the lines here

//------------------------------------------------------------------------------
// Define which timer to use
//
// Uncomment the timer you wish to use on your board.
// If you are using another library which uses timer2, you have options to
//   switch IRremote to use a different timer.
//

// Arduino Mega
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
	//#define IR_USE_TIMER1   // tx = pin 11
	#define IR_USE_TIMER2     // tx = pin 9
	//#define IR_USE_TIMER3   // tx = pin 5
	//#define IR_USE_TIMER4   // tx = pin 6
	//#define IR_USE_TIMER5   // tx = pin 46

// Teensy 1.0
#elif defined(__AVR_AT90USB162__)
	#define IR_USE_TIMER1     // tx = pin 17

// Teensy 2.0
#elif defined(__AVR_ATmega32U4__)
	//#define IR_USE_TIMER1   // tx = pin 14
	//#define IR_USE_TIMER3   // tx = pin 9
	#define IR_USE_TIMER4_HS  // tx = pin 10

// Teensy 3.0 / Teensy 3.1
#elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
	#define IR_USE_TIMER_CMT  // tx = pin 5

// Teensy-LC
#elif defined(__MKL26Z64__)
  #define IR_USE_TIMER_TPM1 // tx = pin 16

// Teensy++ 1.0 & 2.0
#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
	//#define IR_USE_TIMER1   // tx = pin 25
	#define IR_USE_TIMER2     // tx = pin 1
	//#define IR_USE_TIMER3   // tx = pin 16

// MightyCore - ATmega1284
#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)
	//#define IR_USE_TIMER1   // tx = pin 13
	#define IR_USE_TIMER2     // tx = pin 14
	//#define IR_USE_TIMER3   // tx = pin 6

// MightyCore - ATmega164, ATmega324, ATmega644
#elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) \
|| defined(__AVR_ATmega324P__) || defined(__AVR_ATmega324A__) \
|| defined(__AVR_ATmega324PA__) || defined(__AVR_ATmega164A__) \
|| defined(__AVR_ATmega164P__)
	//#define IR_USE_TIMER1   // tx = pin 13
	#define IR_USE_TIMER2     // tx = pin 14
	
//MegaCore - ATmega64, ATmega128
#elif defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__)
 	#define IR_USE_TIMER1     // tx = pin 13

// MightyCore - ATmega8535, ATmega16, ATmega32
#elif defined(__AVR_ATmega8535__) || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__)
 	#define IR_USE_TIMER1     // tx = pin 13

// Atmega8
#elif defined(__AVR_ATmega8__)
	#define IR_USE_TIMER1     // tx = pin 9

// ATtiny84
#elif defined(__AVR_ATtiny84__)
  #define IR_USE_TIMER1     // tx = pin 6

//ATtiny85
#elif defined(__AVR_ATtiny85__)
  #define IR_USE_TIMER_TINY0   // tx = pin 1

// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, etc
// ATmega48, ATmega88, ATmega168, ATmega328
#else
	//#define IR_USE_TIMER1   // tx = pin 9
	#define IR_USE_TIMER2     // tx = pin 3

#endif

for example if you are on a MEGA the library will use TIMER2 because of this:

// Arduino Mega
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
	//#define IR_USE_TIMER1   // tx = pin 11
[color=green]	#define IR_USE_TIMER2     // tx = pin 9
[/color]	//#define IR_USE_TIMER3   // tx = pin 5
	//#define IR_USE_TIMER4   // tx = pin 6
	//#define IR_USE_TIMER5   // tx = pin 46

if you want to use TIMER3 then you would modify the library this way

// Arduino Mega
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
	//#define IR_USE_TIMER1   // tx = pin 11
	[color=red]//[/color] #define IR_USE_TIMER2   // tx = pin 9  [b][color=purple]<-- COMMENTED OUT[/color][/b]
	[color=green]#define IR_USE_TIMER3   // tx = pin 5[/color]  [b][color=purple]<-- REMOVE THE // COMMENTS[/color][/b]
	//#define IR_USE_TIMER4   // tx = pin 6
	//#define IR_USE_TIMER5   // tx = pin 46

You can edit those files with the text editor of your choice… remember that this will affect all the projects using that library when you compile and links against those files. so you might want to make a local copy in your projet and import/link against that local copy instead

Thank you for your nice reply! It is realy specific but I have a last problem... I have looked what kind of arduino I have, and I have got the Arduino Uno where it's written:
"1344
ATMEGA328P-PU"

But I didn't find this king of arduino in the list of the code... :confused:

OK so you have a UNO, that changes what I wrote in post #1 (not sure why I had a mega in mind)

Arduino UNO has 3 Timers and 6 PWM output pins. The relation between timers and PWM outputs is:
Pins 5 and 6: controlled by timer0
Pins 9 and 10: controlled by timer1
Pins 11 and 3: controlled by timer2

so timer 2 is actually controlling pins 11 and 3 which seems you don't use for your motor. Can you share a link to your motor shield?

This is good reading about Timers on Arduino

just to answer the previous question though (for future reference) On a UNO - you are in the last one and would change

// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, etc
// ATmega48, ATmega88, ATmega168, ATmega328
#else
	[color=red]//[/color]#define IR_USE_TIMER1   // tx = pin 9
	[color=green]#define IR_USE_TIMER2[/color]     // tx = pin 3

into

// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, etc
// ATmega48, ATmega88, ATmega168, ATmega328
#else
	[color=green]#define IR_USE_TIMER1[/color]   // tx = pin 9
	[color=red]//[/color] #define IR_USE_TIMER2     // tx = pin 3

to use timer1 instead of timer2

Hum... Here is a new matter... Indeed, I use a motor shield where the pin for its are already chose, and 3 and 9 are ones of them... So even I change pin 3 to pin 9, I will have this problem. But I will try because the pin 9 is the brake of one of the motor, may be it is less a matter than with the PWM of a motor...

It's working!!!!!!!!!!!! :smiley:

Indeed, I use a motor shield where the pin...

which one? do you have a link for it?

Good !