IR Controlled Lego Duplo Train

This is one of those long shot post but maybe someone can see a simple solution.

I’m making an IR controled train, for my niece and nephew, from this project.

This codes lets you switch an LED on and off and control the speed and direction of a motor. Everything works fine unti the first IR signal to the motor, I can make the train go on it’s slowest setting, but after that the IR receiving function breaks.

The first IR signal to the motor is read correct, every IR signal after that it reads a 0. The LED function works repeatedly until I send the first motor signal. So something in receiving the IR for the motor control breaks the IR receive function.

I have very little programing experience so I don’t see why the LED function is fine but the Motor Control function breaks the program.

It would be nice to use the existing code but if I can’t make it work I’m not set on using IR. Suggestions for other ways to control the train motor are welcome as well.

Thanks

/**The MIT License (MIT)
Copyright (c) 2017 by Bin Sun
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
See more at 
https://www.instructables.com/id/IR-Controlled-Lego-Duplo-Compatible-Motorized-Loco/
*/

#include "IRLibAll.h"

int aa1;  //LED status
int aa2;  //Motor status
int p1=3; //LED pin
int RECV_PIN=2;  //Receiver pin
int Buzzer_PIN=4;

// wired connections
#define HG7881_B_IA 10 // D10 --> Motor B Input A --> MOTOR B +
#define HG7881_B_IB 11 // D11 --> Motor B Input B --> MOTOR B -
 
// functional connections
#define MOTOR_B_PWM HG7881_B_IA // Motor B PWM Speed
#define MOTOR_B_DIR HG7881_B_IB // Motor B Direction
 
// the actual values for "fast" and "slow" depend on the motor
#define PWM_SLOW 150  // arbitrary slow speed PWM duty cycle
#define PWM_FAST 255 // arbitrary fast speed PWM duty cycle
#define DIR_DELAY 1000 // brief delay for abrupt motor changes
//#define TONE_PIN 4

int Speed = 150;
int SpeedStep = 50;
int Sound = 250;

//Create a receiver object to listen on pin 2
IRrecvPCI myReceiver(RECV_PIN);

//Create a decoder object 
IRdecodeNEC myDecoder;   

void setup() {
  // initialize the digital pin as an output.
  aa1=1; //LED start status ON
  pinMode(p1, OUTPUT);  
  pinMode( MOTOR_B_DIR, OUTPUT );
  pinMode( MOTOR_B_PWM, OUTPUT );
  pinMode( Buzzer_PIN, OUTPUT );
  digitalWrite( p1, HIGH);// LED start on
  digitalWrite( MOTOR_B_DIR, LOW ); //Motor start OFF
  digitalWrite( MOTOR_B_PWM, LOW );
  
  Serial.begin(9600);
//  delay(2000); while (!Serial); //delay for Leonardo
  myReceiver.enableIRIn(); // Start the receiver
  Serial.println(F("Ready to receive IR signals"));

}

void loop() {
  //Continue looping until you get a complete signal received
  if (myReceiver.getResults()) {

    myDecoder.decode();           //Decode it
    myDecoder.dumpResults(false);  //Now print results. Use false for less detail
    translateIR();
    myReceiver.enableIRIn();      //Restart receiver
  }
}

void translateIR() // takes action based on IR code received describing IR codes 
{
  
      switch(myDecoder.value) {
          case 0xFF22DD:  //Mode
          if(aa1==0){
            digitalWrite(p1,HIGH);
            aa1=1; 
            }
            else{
            digitalWrite(p1,LOW);
            aa1=0;     
            }
        break;
          case 0xFF629D:  //Play/Pause
          if(aa2 !=0 ){
            halt ();
            }
        break;
          case 0xFF906F:  //Forward
          if(aa2 !=1){
            Speed = PWM_SLOW;
            go_forward ();
            } 
            else{
            Speed = Speed + SpeedStep ;
            Speed = min(255, max(0, Speed));
                      if (Speed == 255){buzzer1();}
            go_forward();
            }      
        break;
          case 0xFFE01F: //Backward
          if(aa2 !=2){
            Speed = PWM_SLOW;
            go_backward ();
           }
            else{
            Speed = Speed + SpeedStep ;
            Speed = min(255, max(0, Speed));
            if (Speed == 255){buzzer1();}
            go_backward();
            }       
        break;       
      }
    
  }

  void go_forward ()
  {
    Serial.println ("Going forward...");
    Serial.print ("Speed is: ");
//    Serial.println ("\t");
    Serial.println (Speed);
        if (aa2 == 0){buzzer ();}
        // always stop motors briefly before abrupt changes
        if (aa2 == 2){
        halt();buzzer ();
        delay( DIR_DELAY );}
        // set the motor speed and direction
        digitalWrite( MOTOR_B_DIR, HIGH ); // direction = forward
        analogWrite( MOTOR_B_PWM, 255-Speed ); // PWM speed = fast
        aa2=1;  
  }

  void go_backward ()
  {
    Serial.println ("Going backward...");
    Serial.print ("Speed is: ");
    Serial.println (Speed);
            if (aa2 == 0){buzzer ();}
          // always stop motors briefly before abrupt changes
        if (aa2 == 1){
        halt();buzzer ();
        delay( DIR_DELAY );}
        // set the motor speed and direction
        digitalWrite( MOTOR_B_DIR, LOW ); // direction = reverse      
        analogWrite( MOTOR_B_PWM, Speed ); // PWM speed = fast
        aa2=2;
  }
  
  void halt ()
  {
  Serial.println ("Stopping...");  
  digitalWrite( MOTOR_B_DIR, LOW ); //Motor OFF
  digitalWrite( MOTOR_B_PWM, LOW );
  aa2=0;
  }
 
  void buzzer ()
  {
    for (int thisBuz=0; thisBuz < 2; thisBuz++){
    digitalWrite(Buzzer_PIN, HIGH);
    delay(200);
    digitalWrite(Buzzer_PIN, LOW);
    delay(50);
    }
  }

  void buzzer1 ()
  {
    for (int thisBuz=0; thisBuz < 1; thisBuz++){
    digitalWrite(Buzzer_PIN, HIGH);
    delay(100);
   //   for (int i = 0; i<200; i++){
      digitalWrite(Buzzer_PIN, LOW);
      //} 
      delay(50);
    }
  }
  
    void buzzer2 ()
  {
    for (int thisBuz=0; thisBuz < 255; thisBuz++){
    analogWrite(Buzzer_PIN, thisBuz);
    delay(10);
    analogWrite(Buzzer_PIN, 0);
    }
  }

Hit upon a fix. I added a 200ms delay at the end of the IR read loop. I guess this gives it time to reset before trying to read another IR signal.