Shield + IR??

Hello,

I'd like to use an IR receiver in addition to a motor shield but the problem is that my code actually works without the shield but doesn't with. I'm using a arduino UNO R3 and the L298P shield (https://www.amazon.fr/Shield-dextension-Conducteur-Arduino-Mega2560/dp/B01LVXM0JS/ref=sr_1_1?ie=UTF8&qid=1542207787&sr=8-1&keywords=L298P+shield
Here is the code (given in the Starter pack i bought) :

#include "IRremote.h"

int receiver = 11; // Signal Pin of IR receiver to Arduino Digital Pin 11

/-----( Declare objects )-----/
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'

/-----( Function )-----/
void translateIR() // takes action based on IR code received

// describing Remote IR codes

{

switch(results.value)

{
case 0xFFA25D: Serial.println("POWER"); break;
case 0xFFE21D: Serial.println("FUNC/STOP"); break;
case 0xFF629D: Serial.println("VOL+"); break;
case 0xFF22DD: Serial.println("FAST BACK"); break;
case 0xFF02FD: Serial.println("PAUSE"); break;
case 0xFFC23D: Serial.println("FAST FORWARD"); break;
case 0xFFE01F: Serial.println("DOWN"); break;
case 0xFFA857: Serial.println("VOL-"); break;
case 0xFF906F: Serial.println("UP"); break;
case 0xFF9867: Serial.println("EQ"); break;
case 0xFFB04F: Serial.println("ST/REPT"); break;
case 0xFF6897: Serial.println("0"); break;
case 0xFF30CF: Serial.println("1"); break;
case 0xFF18E7: Serial.println("2"); break;
case 0xFF7A85: Serial.println("3"); break;
case 0xFF10EF: Serial.println("4"); break;
case 0xFF38C7: Serial.println("5"); break;
case 0xFF5AA5: Serial.println("6"); break;
case 0xFF42BD: Serial.println("7"); break;
case 0xFF4AB5: Serial.println("8"); break;
case 0xFF52AD: Serial.println("9"); break;
case 0xFFFFFFFF: Serial.println(" REPEAT");break;

default:
Serial.println(" other button ");

}// End Case

delay(500); // Do not get immediate repeat

} //END translateIR
void setup() /----( SETUP: RUNS ONCE )----/
{
Serial.begin(9600);
Serial.println("IR Receiver Button Decode");
irrecv.enableIRIn(); // Start the receiver

}/--(end setup )---/

void loop() /----( LOOP: RUNS CONSTANTLY )----/
{
if (irrecv.decode(&results)) // have we received an IR signal?

{
translateIR();
irrecv.resume(); // receive the next value
}
}/* --(end main loop )-- */

But i have no idea how to use it with my shield and then use the remote to control a motor, can you please help me ?

Which pins does the motor shield actually use? I would not be surprised that pin 11 was used, so not passed through.

Yes it is, what's wrong ? why does it change using the shield ?

HLSNI:
Yes it is, what's wrong ? why does it change using the shield ?

Shields should pass all pins through, whether the shield uses the pin or not. But, your shield MIGHT not pass pins that it uses through, since it assumes that the reason for using the shield means that the pins it uses ARE in use, so you can't use them for other purposes.

It isn't that difficult to try the IR on a different pin, with the shield in place.

Done. But i don't understand why my motor turns forward for 3s (when I push the POWER button of the remote) and then stops while I'd like it to turn backward for 3s. Any suggestions ? Here is the code I use :

#include "IRremote.h"

int receiver = 4; // Signal Pin of IR receiver to Arduino Digital Pin 4

/-----( Declare objects )-----/
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'

/-----( Function )-----/
void translateIR() // takes action based on IR code received

// describing Remote IR codes

{

switch(results.value)

{
case 0xFFA25D: Serial.println("POWER"); break;
case 0xFFE21D: Serial.println("FUNC/STOP"); break;
case 0xFF629D: Serial.println("VOL+"); break;
case 0xFF22DD: Serial.println("FAST BACK"); break;
case 0xFF02FD: Serial.println("PAUSE"); break;
case 0xFFC23D: Serial.println("FAST FORWARD"); break;
case 0xFFE01F: Serial.println("DOWN"); break;
case 0xFFA857: Serial.println("VOL-"); break;
case 0xFF906F: Serial.println("UP"); break;
case 0xFF9867: Serial.println("EQ"); break;
case 0xFFB04F: Serial.println("ST/REPT"); break;
case 0xFF6897: Serial.println("0"); break;
case 0xFF30CF: Serial.println("1"); break;
case 0xFF18E7: Serial.println("2"); break;
case 0xFF7A85: Serial.println("3"); break;
case 0xFF10EF: Serial.println("4"); break;
case 0xFF38C7: Serial.println("5"); break;
case 0xFF5AA5: Serial.println("6"); break;
case 0xFF42BD: Serial.println("7"); break;
case 0xFF4AB5: Serial.println("8"); break;
case 0xFF52AD: Serial.println("9"); break;
case 0xFFFFFFFF: Serial.println(" REPEAT");break;

default:
Serial.println(" other button ");

}// End Case

delay(500); // Do not get immediate repeat

} //END translateIR
void setup() /----( SETUP: RUNS ONCE )----/
{
Serial.begin(9600);
Serial.println("IR Receiver Button Decode");
irrecv.enableIRIn(); // Start the receiver

//Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin

}/--(end setup )---/

void loop() /----( LOOP: RUNS CONSTANTLY )----/
{
if (irrecv.decode(&results)) {// have we received an IR signal?

if(results.value == 0xFFA25D) {
//forward @ full speed
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed

delay(3000);

digitalWrite(9, HIGH); //Engage the Brake for Channel A

delay(1000);

//backward @ half speed
digitalWrite(12, LOW); //Establishes backward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 123); //Spins the motor on Channel A at half speed

delay(3000);

digitalWrite(9, HIGH); //Engage the Brake for Channel A

delay(1000);
}
translateIR();
irrecv.resume(); // receive the next value
}
}/* --(end main loop )-- */

But i don't understand why my motor turns forward for 3s (when I push the POWER button of the remote) and then stops while I'd like it to turn backward for 3s. Any suggestions ?

Yes. Ditch ALL of the code for reading from the IR, until you have hardcoded instructions for forward(), backword(), stop(), doTheHokeyPokey(), etc. working.

      digitalWrite(9, HIGH); //Engage the Brake for Channel A

So, why doesn't pin 9 have a nice name like channelABrakePin? Then, we wouldn't need a useless comment AND you couldn't possible engage channel B's brake instead of channel A's brake.

All of the pins should have nice names, so the code is self-documenting.

Learn what the correct settings of the brake pin, the direction pin, and the speed pin are, to make the motor spin in one direction, to make the motor stop, and to make the motor spin in the other direction.

Then, combine that knowledge with this sketch.