IRremote to control a car

Hi, people :slight_smile:

I'm trying to make a code to control a car with a infrared control, but I'm having some troubles.

First of all, I think I should show you the codes the infrared control send to arduino when I press the up button:

14:30:59.061 -> FF02FD
14:30:59.129 -> FFFFFFFF
14:30:59.231 -> FFFFFFFF
14:30:59.334 -> FFFFFFFF
14:30:59.435 -> FFFFFFFF
14:30:59.569 -> FFFFFFFF
14:30:59.671 -> FFFFFFFF
14:30:59.774 -> FFFFFFFF
14:30:59.877 -> FFFFFFFF
14:30:59.977 -> FFFFFFFF

As we can see, the infrared control send the first code (FF02FD) and then just keep sending "FFFFFFFF" to say to arduino that the button keep pressed.

Now, my trouble:

I want the engines keeps working during and until I stop pressing the button...

I tried to do this:

void loop() {
  if (irrecv.decode(&results)) {
   if (results.value == 0xFF02FD)
    {
        // Turn on engine 1
        digitalWrite(Motor1A, HIGH);
        digitalWrite(Motor1B, LOW);
        // Turn on engine 2
        digitalWrite(Motor2A, HIGH);
        digitalWrite(Motor2B, LOW);
      while (results.value == 0xFF02FD)
      {

      }
      // Stop engine 1
      digitalWrite(Motor1A, LOW);
      digitalWrite(Motor1B, LOW);
      // Stop engine 2
      digitalWrite(Motor2A, LOW);
      digitalWrite(Motor2B, LOW);
    }
    Serial.println(results.value, HEX);
    irrecv.resume();
  }
}

But the engines keeps going even I don't press the button more.

I thought it was a mistake to say " while (results.value == 0xFF02FD)", and I put this on my code:

void loop() {
  if (irrecv.decode(&results)) {
   if (results.value == 0xFF02FD)
    {
        // Turn on engine 1
        digitalWrite(Motor1A, HIGH);
        digitalWrite(Motor1B, LOW);
        // Turn on engine 2
        digitalWrite(Motor2A, HIGH);
        digitalWrite(Motor2B, LOW);
      while (results.value == 0xFFFFFF)
      {

      }
      // Stop engine 1
      digitalWrite(Motor1A, LOW);
      digitalWrite(Motor1B, LOW);
      // Stop engine 2
      digitalWrite(Motor2A, LOW);
      digitalWrite(Motor2B, LOW);
    }
    Serial.println(results.value, HEX);
    irrecv.resume();
  }
}

But with this my engines doesn't start working.

I had asked my teacher a lot and he told me about the function (irrecv.decode(&results)), and we tried to do this:

void loop() {
  if (irrecv.decode(&results)) {
    if (results.value == 0xFF02FD)
    {
        // Turn on engine 1
        digitalWrite(Motor1A, HIGH);
        digitalWrite(Motor1B, LOW);
        // Turn on engine 2
        digitalWrite(Motor2A, HIGH);
        digitalWrite(Motor2B, LOW);
      while (irrecv.decode(&results))
      {
      }
      // Stop engine 1
      digitalWrite(Motor1A, LOW);
      digitalWrite(Motor1B, LOW);
      // Stop engine 2
      digitalWrite(Motor2A, LOW);
      digitalWrite(Motor2B, LOW);
    }
    Serial.println(results.value, HEX);
    irrecv.resume();
  }
}

But it also doesn't work (the engines keeps going even I don't press the button more)

Could anyone help me with this code, please?

You tested results.value without running irrecv again.

I tried to run " irrecv.resume();" in the begging of the loop, but it also doesn't work (the engines doesn't start working).

void loop() {
  if (irrecv.decode(&results)) {
           irrecv.resume();
    if (results.value == 0xFF02FD)
    {
        // Turn on engine 1
        digitalWrite(Motor1A, HIGH);
        digitalWrite(Motor1B, LOW);
        // Turn on engine 2
        digitalWrite(Motor2A, HIGH);
        digitalWrite(Motor2B, LOW);
       while (irrecv.decode(&results))
      {
      }
      // Stop engine 1
      digitalWrite(Motor1A, LOW);
      digitalWrite(Motor1B, LOW);
      // Stop engine 2
      digitalWrite(Motor2A, LOW);
      digitalWrite(Motor2B, LOW);
    }
    Serial.println(results.value, HEX);
    irrecv.resume();
  }
}