How can i store the last IR code to check if it needs repeating?

I'm just learning Arduino and i've got a DC Motor & IR Receiver connected. It's working fine if i press the button once but i can't figure out how to keep the motor spinning if i hold the button down as the REPEAT command is the same numbers.

I figured i would store the last code sent and check if the repeat command and last code match but it doesn't seem to be working and can't figure out why.

#include <IRremote.h>

int IRpin = 11;  // pin for the IR sensor
IRrecv irrecv(IRpin);
decode_results results;
int lastCode;

void setup() {
  // put your setup code here, to run once:
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  irrecv.enableIRIn(); // Enable IR Receiver.
}

void loop() {
  // put your main code here, to run repeatedly:


if

 (irrecv.decode(&results)) {
  Serial.println(results.value);
  irrecv.resume(); 
  Serial.println("Last Code is set to: ");
  Serial.write(lastCode);


  if(results.value== 16748655 || (results.value== 4294967295 && lastCode== 16748655)) // Your ON button value                                       
  {
      digitalWrite(8, HIGH);
      digitalWrite(7, LOW);
      analogWrite(9, 255);
      delay(1000);
      analogWrite(9, 0);
      lastCode= 16748655;

  }


  else if(results.value == 16769055 || (results.value== 4294967295 && lastCode== 16769055)) // Your OFF button value 
  {
      digitalWrite(8, LOW);
      digitalWrite(7, HIGH);
      analogWrite(9, 255);
      delay(1000);
      analogWrite(9, 0);
      lastCode= 16769055;

  }
}


}

lastCode= 16769055; An int is only sixteen bits.
That value is twenty four.

Use unsigned long instead

AWOL:
lastCode= 16769055; An int is only sixteen bits.
That value is twenty four.

Use unsigned long instead

Wow that's all it was, thanks for much for the quicky reply.

1 more thing, Where im outputting the value of last code, How can i get it to be on a single line in the serial so it just shows as "The last code was: xx" rather then having a new line after the string, I only know abit of PHP and in that i just use "This is a string". $variable; but that doesn't seem to work.

  Serial.print(F("Last Code is set to: "));
  Serial.println(lastCode);

Thanks, that's all working perfectly. I've decided to add some LEDs but struggling to find out how to detect when ive let go of the button on the IR Controller i want to turn the LED off.

At the moment it turns on fine when i press it but once i let go it stays on.

How can i detect when the button is no longer being press?

#include <IRremote.h>

int IRpin = 11;  // pin for the IR sensor
int ledOne = 5;
IRrecv irrecv(IRpin);
decode_results results;
unsigned long lastCode;

void setup() {
  // put your setup code here, to run once:
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(ledOne, OUTPUT);
  Serial.begin(9600);
  irrecv.enableIRIn(); // Enable IR Receiver.
}

void loop() {
  // put your main code here, to run repeatedly:


if (irrecv.decode(&results)) {
  Serial.println(results.value);
  irrecv.resume(); 
  //Serial.print(F("Last Code is set to: "));
  //Serial.println(lastCode);

  if(results.value== 16748655 || (results.value== 4294967295 && lastCode== 16748655)) // Your ON button value                                       
  {
      digitalWrite(8, HIGH);
      digitalWrite(7, LOW);
      analogWrite(9, 255);
      delay(100);
      analogWrite(9, 0);
      lastCode= 16748655;
      digitalWrite(ledOne, HIGH); 
      results.value=0;
  }

  else if(results.value == 16769055 || (results.value== 4294967295 && lastCode== 16769055)) // Your OFF button value 
  {
      digitalWrite(8, LOW);
      digitalWrite(7, HIGH);
      analogWrite(9, 255);
      delay(100);
      analogWrite(9, 0);
      lastCode= 16769055;
  }
  
}

}

How can i detect when the button is no longer being press?

irrecv.decode() will return false when no button is being pressed. There is NO value sent that says that you released the button.

PaulS:
irrecv.decode() will return false when no button is being pressed. There is NO value sent that says that you released the button.

Thanks, I tried this and the LED lights up for a couple seconds then turns off, Have i mis-typed something?

if(irrecv.decode(&results) == false) {
    digitalWrite(ledOne, LOW);
  }

Have i mis-typed something?

Yes. You left out setup() and loop(). This is not http://snippets-r-us.com. You should pay them a visit with your snippet.

#include <IRremote.h>

int IRpin = 11;  // pin for the IR sensor
int ledOne = 5;
IRrecv irrecv(IRpin);
decode_results results;
unsigned long lastCode;

void setup()
{
  // put your setup code here, to run once:
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(ledOne, OUTPUT);
  Serial.begin(9600);
  irrecv.enableIRIn(); // Enable IR Receiver.
}

void loop()
{
  if (!irrecv.decode(&results))
  {
    digitalWrite(ledOne, LOW);
  }
  else // Decode was successful
  {
    Serial.println(results.value);

    // Handle REPEAT code
    if (results.value != 4294967295UL)
      lastCode = results.value;
      
    irrecv.resume();
    
    // Serial.print(F("Last Code is set to: "));
    // Serial.println(lastCode);

    switch (lastCode)
    {
      case 16748655UL: // Your ON button value
        digitalWrite(8, HIGH);
        digitalWrite(7, LOW);
        analogWrite(9, 255);
        delay(100);
        analogWrite(9, 0);
        digitalWrite(ledOne, HIGH);
        break;

      case 16769055UL: // Your OFF button value
        digitalWrite(8, LOW);
        digitalWrite(7, HIGH);
        analogWrite(9, 255);
        delay(100);
        analogWrite(9, 0);
        break;
    }
  }
}