return; doesn't work

So i have this program to unlock my PC using an IR remote, and i user the "IRremote.h" and "Keyboard.h" library for arduino Leonardo, but when i press the button, my computer locks, unlocks, locks, and so on.
I found out, that the "return;" part doesn't work.
I wanted to use it to exit the loop.

Anyone knows why it doesn't work?

#include <boarddefs.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <ir_Lego_PF_BitStreamEncoder.h>

#include "Keyboard.h"

int x = 1;
int RECV_PIN = 11;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup() {
  Serial.begin(9600);   // Initialize serial communications with the PC
      // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  Keyboard.begin();
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
   if (irrecv.decode(&results)) {
    Serial.println(results.value);
    irrecv.resume(); // Receive the next value
  }
  
  if (results.value == 2704)
  {  
    if (x == 2){
      delay(1000);
      Keyboard.press(KEY_LEFT_GUI);
      Keyboard.press('l');
      delay(100);
      Keyboard.releaseAll();
      x = 1;
      return;
    }
    if (x == 1)
    {
      delay(1000);
    Keyboard.press(KEY_ESC);
    delay(100);
    Keyboard.releaseAll();
    delay(500);
    Keyboard.print("8659");
    x = 2;
    return;
    }  

  }
  else
  {
    
  }
delay(100);
}

The loop() function is called repeatedly by main(), which you don't see. If you "return" it is called again immediately.

Oh! Thanks man!
I have an idea now! :slight_smile:
You guys really are fast

Try this for your loop function:

void loop() 
{
	if (irrecv.decode(&results)) 
	{
		Serial.println(results.value);
		irrecv.resume(); // Receive the next value
	}
 
	if (results.value == 2704)
	{ 
		if (x == 2)
		{
			delay(1000);
			Keyboard.press(KEY_LEFT_GUI);
			Keyboard.press('l');
			delay(100);
			Keyboard.releaseAll();
			x = 1;
		}
		else if (x == 1)
		{
			delay(1000);
			Keyboard.press(KEY_ESC);
			delay(100);
			Keyboard.releaseAll();
			delay(500);
			Keyboard.print("8659");
			x = 2;
		} 
		else	
		{
			delay(100);
		}
	}
	else
	{
   
	}
}

JEREDEKK:
You guys really are fast

That depends who's on line at any moment; there are folk in most timezones I think.