If else & case, not working fully? [SOLVED]

Howdy,

I bought an Infrared Remote kit and have been able to use it to turn on a laser. If I press the “1” button, it turns on, then the “2” button turns it off.

My new idea was to have “1” turn on laser, “2” turn on and off with a short delay, press “3” to turn on and off with even shorter delay, and if I press “0” turn the laser off. With my code below, if I press “1”, then I get a stable light from the laser. If I press “2”, the laser turns off. If I press “3” then I get a blinking laser. If I press “0”, nothing happens and the laser stays blinking (or stable, if I pressed “1” before “0”). How come it’s not working - I looked up “if else” statements and seem to be doing that correctly, but maybe it’s the order?

Thanks for any help!

[Note: The code below is from a larger program, and I tried to keep only what’s applicable here. If you find that the code should work, let me know and I can post the whole thing]

#include <IRremote.h>  // Import the IR Remote library
const int receiver = 7;    // pin 1 of the IR receiver to Arduino digital pin 11
IRrecv irrecv(receiver);    //create instance of 'irrecv'
decode_results results; //create instance of 'decode_results'

int remoteBtn; // set variable to determine what button on the remote was pressed
const int laserPin = 12;  // set the pin the laser is plugged in to

void setup() {
  Serial.begin(9600);
  pinMode(laserPin, OUTPUT);
  irrecv.enableIRIn();  //start the receiver
}

void loop() {
  // This part is for the IR Receiver
  if (irrecv.decode(&results)) { // have we rec'd an IR signal?
  Serial.println(results.value, HEX); //this will show RAW value of remote press
  translateIR();
  irrecv.resume(); // receive the next value
  }
  remoteEvents(); // I created a function to keep the LOOP area 'cleaner'
} // closes LOOP


void translateIR() { //takes action based on IR code received
  switch(results.value)
  {
    case 0xFF629D: Serial.println(" FORWARD"); break;
    case 0xFF22DD: Serial.println(" LEFT");    break;
    case 0xFF02FD: Serial.println(" -OK-");    break;
    case 0xFFC23D: Serial.println(" RIGHT");   break;
    case 0xFFA857: Serial.println(" REVERSE"); break;
    case 0xFF6897: Serial.println(" 1");    remoteBtn = 1;     break;
    case 0xFF9867: Serial.println(" 2");    remoteBtn = 2;     break;
    case 0xFFB04F: Serial.println(" 3");    remoteBtn = 3;     break;
    case 0xFF30CF: Serial.println(" 4");    remoteBtn = 4;     break;
    case 0xFF18E7: Serial.println(" 5");    remoteBtn = 5;     break;
    case 0xFF7A85: Serial.println(" 6");    remoteBtn = 6;     break;
    case 0xFF10EF: Serial.println(" 7");    remoteBtn = 7;     break;
    case 0xFF38C7: Serial.println(" 8");    remoteBtn = 8;     break;
    case 0xFF5AA5: Serial.println(" 9");    remoteBtn = 9;     break;
    case 0xFF42BD: Serial.println(" *");    remoteBtn = 10;     break;
    case 0xFF4AB5: Serial.println(" 0");    remoteBtn = 0;     break;
    case 0xFF52AD: Serial.println(" #");    remoteBtn = 11;     break;
    case 0xFFFFFFFF: Serial.println(" REPEAT");break;  

    default:
    Serial.println(" other button     ");
    } // end case

    delay (500); //do not get immediate repeat
} //end translateIR

void remoteEvents(){
      if (remoteBtn == 1) {
        digitalWrite(laserPin, HIGH);
      }
      else if (remoteBtn == 2) {
       digitalWrite(laserPin, HIGH);
       delay(250);
       digitalWrite(laserPin,LOW);
       delay(250);
     } else if (remoteBtn == 3) {
        digitalWrite(laserPin,HIGH);
        delay(100);
        digitalWrite(laserPin,LOW);
        delay(100);
        }
     else if (remoteBtn == 0) {
      digitalWrite(laserPin, LOW);
    }
  }

Also, the Serial Monitor doesn’t always show the button that is pressed on the remote, but doesn’t the code above show that it should?

Please let me know if I can clarify anything at all - thank you for any ideas!

Try:

void remoteEvents(){
      if (remoteBtn == 1) {
        digitalWrite(laserPin, HIGH);
      }
      else if (remoteBtn == 2) {
       digitalWrite(laserPin, HIGH);
       delay(250);
       digitalWrite(laserPin,LOW);
       delay(250);
     } else if (remoteBtn == 3) {
        digitalWrite(laserPin,HIGH);
        delay(100);
        digitalWrite(laserPin,LOW);
        delay(100);
        }
     else if (remoteBtn == 0) {
      digitalWrite(laserPin, LOW);
    }
    remoteBtn = -1;
  }

Do you understand what I did?

luisilva:
Try:

void remoteEvents(){

if (remoteBtn == 1) {
        digitalWrite(laserPin, HIGH);
      }
      else if (remoteBtn == 2) {
      digitalWrite(laserPin, HIGH);
      delay(250);
      digitalWrite(laserPin,LOW);
      delay(250);
    } else if (remoteBtn == 3) {
        digitalWrite(laserPin,HIGH);
        delay(100);
        digitalWrite(laserPin,LOW);
        delay(100);
        }
    else if (remoteBtn == 0) {
      digitalWrite(laserPin, LOW);
    }
    remoteBtn = -1;
  }




Do you understand what I did?

I'm away from my Arduino, so I'll try it later - but I think what you did is basically have the loop see if the button is 1,2,3, or 0. and after the loop, if it's not one of those, set it to -1. You chose -1 because I have the other numbers "taken" (you could have also used like "1000" yeah?).

Yes, is exactly that, and yes I chose -1 but I could chose anything different from the values that you already used.

luisilva:
Yes, is exactly that, and yes I chose -1 but I could chose anything different from the values that you already used.

Glad I understood it! But, unfortunately, it didn’t work. I can get the stable beam with “1”, and the blinking with “3”, but pressing “2” or “0” doesn’t do anything. It is a lot (although not too complex), but here’s the full sketch…Note that this is a sketch I used mainly to see what I can do with some sensors and programming, so it’s super practical, but hopefully this can shed light on the issue.

#include <IRremote.h>  // Import the IR Remote library
#include <NewTone.h>  // import the newTone library  

const int receiver = 7;    // pin 1 of the IR receiver to Arduino digital pin 11
IRrecv irrecv(receiver);    //create instance of 'irrecv'
decode_results results; //create instance of 'decode_results'

int remoteBtn; // set variable to determine what button on the remote was pressed

// ----- SET UP PIR Sensor

const int PIRPin = 3;
int PIRstate = LOW;
int val = 5; //variable for reading pin status


// ----- SET UP SERVO
#include <Servo.h>  // set up Servo

Servo myServo;  // create a servo object 

const int potPin = A1; // analog pin used with Potentiometer
int potVal;            // variable to read value from analog pin
int angle;             // variable to hold angle for servo motor

const int photoresistor = A0;  // pin with the photoresistor
int photoresistorVal = 0; // variable to hold the value from the sensor

const int laserPin = 12;  // set the pin the laser is plugged in to

const int LED = 4;        // set the pin the LED is plugged in to

// -- RGB LED SETUP -- //
const int redLEDPin = 9;
const int blueLEDPin = 10;
const int greenLEDPin = 11;

int redValue = 0;
int blueValue = 0;
int greenValue = 0;

int colorValue; // set up the variable for the MAPPING

void setup() {
  myServo.attach(6);    // attaches servo on pin 6
  Serial.begin(9600);
  pinMode(laserPin, OUTPUT);
  pinMode(LED, OUTPUT);
  irrecv.enableIRIn();  //start the receiver

  // set the digital LED pins as outputs
  pinMode(redLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT);
  pinMode(greenLEDPin, OUTPUT);
  
  // set the PIR pin to input
  pinMode(PIRPin,INPUT);
  // delay(10000);  //Delay so the PIR Sensor has time to warm up.
  
}

void loop(){
  photoresistorVal = analogRead(photoresistor); // start reading the photoresistor to see if the laser is hitting it.
  Serial.print("The photoresistor value is: ");
  Serial.println(photoresistorVal); 
  if (photoresistorVal >= 1000) {
    digitalWrite(LED, HIGH);
  } else { digitalWrite(LED, LOW); }

  // Loop the RGB LED to change colors
  int colorValue = map(photoresistorVal, 600, 1050, 0, 255);
  analogWrite(redLEDPin,colorValue);

  // This part is for the IR Receiver
  if (irrecv.decode(&results)) { // have we rec'd an IR signal?
  Serial.println(results.value, HEX); //this will show RAW value of remote press
  translateIR();
  irrecv.resume(); // receive the next value
  // PIRservoChange();
  }
  remoteEvents();
//PotProgram();
}

void translateIR() { //takes action based on IR code received
  switch(results.value)
  {
    case 0xFF629D: Serial.println(" FORWARD"); break;
    case 0xFF22DD: Serial.println(" LEFT");    break;
    case 0xFF02FD: Serial.println(" -OK-");    break;
    case 0xFFC23D: Serial.println(" RIGHT");   break;
    case 0xFFA857: Serial.println(" REVERSE"); break;
    case 0xFF6897: Serial.println(" 1");    remoteBtn = 1;     break;
    case 0xFF9867: Serial.println(" 2");    remoteBtn = 2;     break;
    case 0xFFB04F: Serial.println(" 3");    remoteBtn = 3;     break;
    case 0xFF30CF: Serial.println(" 4");    remoteBtn = 4;     break;
    case 0xFF18E7: Serial.println(" 5");    remoteBtn = 5;     break;
    case 0xFF7A85: Serial.println(" 6");    remoteBtn = 6;     break;
    case 0xFF10EF: Serial.println(" 7");    remoteBtn = 7;     break;
    case 0xFF38C7: Serial.println(" 8");    remoteBtn = 8;     break;
    case 0xFF5AA5: Serial.println(" 9");    remoteBtn = 9;     break;
    case 0xFF42BD: Serial.println(" *");    remoteBtn = 10;     break;
    case 0xFF4AB5: Serial.println(" 0");    remoteBtn = 0;     break;
    case 0xFF52AD: Serial.println(" #");    remoteBtn = 11;     break;
    case 0xFFFFFFFF: Serial.println(" REPEAT");break;  

    default:
    Serial.println(" other button     ");
    } // end case

    delay (500); //do not get immediate repeat
} //end translateIR

void remoteEvents(){
      if (remoteBtn == 1) {
        digitalWrite(laserPin, HIGH);
      }
      else if (remoteBtn == 2) {
       digitalWrite(laserPin, HIGH);
       delay(250);
       digitalWrite(laserPin,LOW);
       delay(250);
     } else if (remoteBtn == 3) {
        digitalWrite(laserPin,HIGH);
        delay(100);
        digitalWrite(laserPin,LOW);
        delay(100);
        }
     else if (remoteBtn == 0) {
      digitalWrite(laserPin, LOW);
    }
  remoteBtn = -1;
  }


void PotProgram(){
    potVal = analogRead(potPin); // read the value of the Potentiometer
    angle = map(potVal, 0, 1023, 10, 165); // We could do from 0 to 179, but the servo shakes at extremes
    myServo.write(angle); // turn the Servo
    delay(100); // wait for the servo to get there
  }
  
  void PIRservoChange() {
    val = digitalRead(PIRPin);
    if (val == HIGH) {
     myServo.write(0); 
     delay(20);
     } else if (val == LOW) {
      myServo.write(179);
      delay(20);
    }
  }

[For what it’s worth, the RGB LED isn’t working either, but unless it’s tied to the laser issue, don’t worry about helping me out with that issue, I’ll keep troubleshooting and ask in a new thread if necessary]

Tell me one thing. Did you get this messages OK?

    case 0xFF6897: Serial.println(" 1");    remoteBtn = 1;     break;
    case 0xFF9867: Serial.println(" 2");    remoteBtn = 2;     break;
    case 0xFFB04F: Serial.println(" 3");    remoteBtn = 3;     break;
    case 0xFF30CF: Serial.println(" 4");    remoteBtn = 4;     break;

  (...)

Can you post the log of this messages, when the error happens?

luisilva:
Tell me one thing. Did you get this messages OK?

    case 0xFF6897: Serial.println(" 1");    remoteBtn = 1;     break;

case 0xFF9867: Serial.println(" 2");    remoteBtn = 2;     break;
    case 0xFFB04F: Serial.println(" 3");    remoteBtn = 3;     break;
    case 0xFF30CF: Serial.println(" 4");    remoteBtn = 4;     break;

(...)



Can you post the log of this messages, when the error happens?

AFAIK there's no error message - although I haven't checked the serial monitor. Do you mean in the serial monitor, or an error message as if I were compiling and there's an error (as happens when you forget an ';' after a line)?

If you say that 0 and 2 are not working, I want to know the result of serial monitor to know if there are messages saying that those keys where pressed.

luisilva:
If you say that 0 and 2 are not working, I want to know the result of serial monitor to know if there are messages saying that those keys where pressed.

Ah, sorry. Here's what the serial monitor outputs when I press "1", "2", "3", and "0"

FF6897

1

FF9867

2

FFB04F

3

FF4AB5

0

But occasionally (and I can't get it to duplicate this message for some reason), I press "1" (after having pressed "0") and it shows

B74DB070

other button

or, after pressing 1 a few times,

FF6897

1

2574A69

other button

FF6897

1

FF6897

1

FF

other button

FFFFFFFF

REPEAT

The problem that you describe:

But occasionally (and I can't get it to duplicate this message for some reason), I press "1" (after having pressed "0") and it shows

I think that is not a problem in your code, but in the IR device or in the library it self. Your program "works" with the information that the IR library sends to it, if the library don't sends the correct information your program can't do anything to "guess" the correct information.

About this:

I can get the stable beam with "1", and the blinking with "3", but pressing "2" or "0" doesn't do anything.

I don't know what is happen. What the function remoteEvents should do (here is another version, but that do the same):

void remoteEvents() {
  switch (remoteBtn) {
    case 1:
      digitalWrite(laserPin, HIGH);
      break;
    case 2:
      digitalWrite(laserPin, HIGH);
      delay(250);
      digitalWrite(laserPin, LOW);
      break;
    case 3:
      digitalWrite(laserPin, HIGH);
      delay(100);
      digitalWrite(laserPin, LOW);
      break;
    case 0:
      digitalWrite(laserPin, LOW);
      break;
    default:
      Serial.println("Event not coded");
  }
  remoteBtn = -1;
}

is:

  • when 1 is pressed, the laser is turned on;
  • when 2 is pressed, the laser is turned on for 250 ms and then is turned off;
  • when 3 is pressed do the same that is done when 2 is pressed but for 100ms;
  • when 0 is pressed turn off the laser;
  • the line remoteBtn = -1; is there to the program don't repeat the same action over and over again until you press another key;
  • in this version with switch/case I add an error message to let you know that the action that you want to do is not coded.

So, I don't see any problem in the code.

luisilva:
...

void remoteEvents() {

switch (remoteBtn) {
    case 1:
      digitalWrite(laserPin, HIGH);
      break;
    case 2:
      digitalWrite(laserPin, HIGH);
      delay(250);
      digitalWrite(laserPin, LOW);
      break;
    case 3:
      digitalWrite(laserPin, HIGH);
      delay(100);
      digitalWrite(laserPin, LOW);
      break;
    case 0:
      digitalWrite(laserPin, LOW);
      break;
    default:
      Serial.println("Event not coded");
  }
  remoteBtn = -1;
}



...

So, I don't see any problem in the code.

I tried using case but again, the same issues. I agree that my code looks good and therefore should be working. Could it be with my particular IR sensor (ie. a more expensive/better one would work) or with IR sensors in general? I don't know enough to look in the IRSensor library to debug that. I guess I can post this issue to that IR sensor's website/author.

Also, since the sensor works sometimes (only with "1" and "3" apparently), I doubt it's a wiring issue, yeah?

I also have this IR emitter and detector. Do you think that would work?

PS: Thanks so much for your help on this, I really appreciate it! :grin:

What you must to look for is if when the things don't work as they should, if you receive the messages with the right key. If you don't, the problem is not from your code. At the beginning I think that you miss the reset of the key pressed the "key=-1" thing that I point, but now, I don't find anything wrong.

Maybe you can get only a different library that gives you better results, but I don't know any to suggest you.

! :astonished:

For now, and I’m sorry, but ignore my previous issues (I now have new ones, they’re related but I think I’m on to something)

I think I made a discovery - I had the IR receiver plugged in to pin 7, but then I thought to switch the pin to a PWM pin, so I did. It’s now plugged in to PIN ~5, and I cleaned up the code.

Now, I can get a stable beam with “1”, but now when I press “2”, it is stable briefly, then turns off. Then I press “3” and it is stable briefly, but turns off quicker than “2”, so it’s seeing the delay! However, it’s not looping through the blink - it stays off when it’s off. (Pressing ‘0’ will turn the laser off as well).

Here’s my new code:

/*
This will use the laser, pointed at the Photoresistor.  When the laser is hitting the
photoresistor, light an LED.

Separately, you can use the IR REMOTE to light an LED

*/

#include <IRremote.h>  // Import the IR Remote library

int receiver = 5;    // pin 1 of the IR receiver to Arduino digital pin 11
IRrecv irrecv(receiver);    //create instance of 'irrecv'
decode_results results; //create instance of 'decode_results'

int remoteBtn; // set variable to determine what button on the remote was pressed

const int laserPin = 12;  // set the pin the laser is plugged in to

void setup() {
  Serial.begin(9600);
  pinMode(laserPin, OUTPUT);
  irrecv.enableIRIn();  //start the receiver
}

void loop(){
  // This part is for the IR Receiver
  if (irrecv.decode(&results)) { // have we rec'd an IR signal?
  Serial.println(results.value, HEX); //this will show RAW value of remote press
  translateIR();
  remoteEvents();
  irrecv.resume(); // receive the next value
  // remoteEvents();
  }
//  remoteEvents();
}

void translateIR() { //takes action based on IR code received
  switch(results.value)
  {
    case 0xFF629D: Serial.println(" FORWARD"); break;
    case 0xFF22DD: Serial.println(" LEFT");    break;
    case 0xFF02FD: Serial.println(" -OK-");    break;
    case 0xFFC23D: Serial.println(" RIGHT");   break;
    case 0xFFA857: Serial.println(" REVERSE"); break;
    case 0xFF6897: Serial.println(" 1");    remoteBtn = 1;     break;
    case 0xFF9867: Serial.println(" 2");    remoteBtn = 2;     break;
    case 0xFFB04F: Serial.println(" 3");    remoteBtn = 3;     break;
    case 0xFF30CF: Serial.println(" 4");    remoteBtn = 4;     break;
    case 0xFF18E7: Serial.println(" 5");    remoteBtn = 5;     break;
    case 0xFF7A85: Serial.println(" 6");    remoteBtn = 6;     break;
    case 0xFF10EF: Serial.println(" 7");    remoteBtn = 7;     break;
    case 0xFF38C7: Serial.println(" 8");    remoteBtn = 8;     break;
    case 0xFF5AA5: Serial.println(" 9");    remoteBtn = 9;     break;
    case 0xFF42BD: Serial.println(" *");    remoteBtn = 10;     break;
    case 0xFF4AB5: Serial.println(" 0");    remoteBtn = 0;     break;
    case 0xFF52AD: Serial.println(" #");    remoteBtn = 11;     break;
    case 0xFFFFFFFF: Serial.println(" REPEAT");break;  

    default:
    Serial.println(" other button     ");
    } // end case

    delay (500); //do not get immediate repeat
} //end translateIR

void remoteEvents(){
      switch (remoteBtn) {
    case 1:
      digitalWrite(laserPin, HIGH);
      break;
    case 2:
      digitalWrite(laserPin, HIGH);
      delay(250);
      digitalWrite(laserPin, LOW);
      break;
    case 3:
      digitalWrite(laserPin, HIGH);
      delay(100);
      digitalWrite(laserPin, LOW);
      break;
    case 0:
      digitalWrite(laserPin, LOW);
      break;
    default:
      Serial.println("Event not coded");
  }
  remoteBtn = -1;
  }

As you may see, I am thinking the issue is where I place the remoteEvents which isn’t allowing the loop.

Yes, that is what is suppose to do (or at least I do it to do that). What you would like it do?

Instead of turning on for a brief (and briefer) moment, I want it to blink

Pressing 2 will cause (psuedo code below)

A:
Laser = ON
delay for 300  //some arbitrary delay
Laser = OFF
delay for 300
GOTO A

Pressing 3 will do the same as above, but with a shorter delay, ie 200

Edit: Ok - I am getting closer, but am hesitant to keep this thread going - unless you would like me to re-update where I am now, I'll just troubleshoot on my own. I really really appreciate your help, and I learned about switch / case so thank you!

Piethon:
Instead of turning on for a brief (and briefer) moment, I want it to blink

Pressing 2 will cause (psuedo code below)

A:

Laser = ON
delay for 300  //some arbitrary delay
Laser = OFF
delay for 300
GOTO A




Pressing 3 will do the same as above, but with a shorter delay, ie 200

take a look at this, which calls different functions when a character is entered into the serial terminal or a button attached to pin# is pressed

it uses blink without delay to flash your on-board led...

perhaps you will find it useful for what you are trying to do.

int buttonPin = 3;
int ledPin = 13;
int state = 0;
int oldState;
unsigned long startTime;
unsigned long interval = 1000UL;
//
void setup() 
{
  Serial.begin(9600);
}
//
void loop () 
{
  if (Serial.available())
  {
    int dump = Serial.read();
    state++;
    if (state > 4) state = 0;
  }
  int buttonState = digitalRead(buttonPin);
  {
    if (buttonState == HIGH)
    {
      if (oldState == LOW)
      {
        state++;
        Serial.println(state);
        if (state > 4) state = 0;
      }
    }
  }
  oldState = buttonState;
  if (state == 0)
  {
    noFlash();
    startTime = millis();
  }
  else if (state == 1)
  {
    flashA();
  }
  else if (state == 2)
  {
    flashB();
  }
  else if (state == 3)
  {
    flashC();
  }
  else if (state == 4)
  {
    flashD();
  }
}
//
void noFlash()
{
  digitalWrite(ledPin, LOW);
}
//
void flashA() //slow flash
{
  if (millis() - startTime >= interval)
  {
    digitalWrite(ledPin, !digitalRead(ledPin));
    startTime = millis();
  }
}
//
void flashB() //med flash
{
  if (millis() - startTime >= interval/4UL)
  {
    digitalWrite(ledPin, !digitalRead(ledPin));
    startTime = millis();
  }
}
//
void flashC() //fast flash
{
  if (millis() - startTime >= interval/20UL)
  {
    digitalWrite(ledPin, !digitalRead(ledPin));
    startTime = millis();
  }
}

void flashD() //solid
{
  digitalWrite(ledPin, HIGH);
  startTime = millis();
}

blink without delay to flash your on-board led...

Yes, you need to look at this. The problem is, I think, that you press the IR remote when a delay is running and it is not received successfully.

(I'm travelling.. will try to help more Tuesday...)

Piethon:
Instead of turning on for a brief (and briefer) moment, I want it to blink

Pressing 2 will cause (psuedo code below)

A:

Laser = ON
delay for 300  //some arbitrary delay
Laser = OFF
delay for 300
GOTO A




Pressing 3 will do the same as above, but with a shorter delay, ie 200

Edit: Ok - I am getting closer, but am hesitant to keep this thread going - unless you would like me to re-update where I am now, I'll just troubleshoot on my own. I really really appreciate your help, and I learned about `switch / case` so thank you!

Well, if you want do this, I must apologise my self. I don't understand that and and I "trick" you. Your first code is fine, so you must do it that way.

void remoteEvents() {
  switch (remoteBtn) {
    case 1:
      digitalWrite(laserPin, HIGH);
      break;
    case 2:
      digitalWrite(laserPin, HIGH);
      delay(250);
      digitalWrite(laserPin, LOW);
      delay(250);
      break;
    case 3:
      digitalWrite(laserPin, HIGH);
      delay(100);
      digitalWrite(laserPin, LOW);
      delay(100);
      break;
    case 0:
      digitalWrite(laserPin, LOW);
      break;
    default:
      Serial.println("Event not coded");
  }
}

You need to keep the value of remoteBtn so, after the first time the program know what needs to do.

I'm sorry.

Oh, no worries luisilva! Thanks for all of your great help! I combined your latest reply in my final sketch :smiley: :grin: :smiley: XD

And BulldogLowell, I took the idea of creating functions to use in the switch/case and put it in my sketch.

We got it working! I don’t exactly know what happened, but I decided to start from scratch and it works now! The following code works:

#include <IRremote.h>

const int LEDpin = 4; // set up LED Pin [not used below, was earlier to confirm upload was working]
const int laserPin = 12;

int IRpin = 5;
IRrecv irrecv(IRpin); // create instance of 'irrecv'
decode_results results; //create instance of 'decode_results'

int remoteBtn = 0; // set variable to determine which button on remote was pressed

void setup() {
  Serial.begin(9600);
  pinMode(LEDpin, OUTPUT);
  pinMode(laserPin, OUTPUT);
  irrecv.enableIRIn();  // start the receiver
}

void loop() {
  if (irrecv.decode(&results)) {    // have we rec'd an IR signal?
  // Serial.println(results.value, HEX);     // this will show the RAW value of remote press
  translateIR();
  irrecv.resume();
  } // close IR signal rec'd loop
  // Serial.print("The remoteBtn is ");
  // Serial.println(remoteBtn);
  remoteEvents();
}

// ----- FUNCTIONS BELOW

void translateIR(){
  switch(results.value) {
    case 0xFF6897: Serial.println(" 1"); remoteBtn = 1; break;
    case 0xFF9867: Serial.println(" 2"); remoteBtn = 2; break;
    case 0xFFB04F: Serial.println(" 3");    remoteBtn = 3;     break;
    case 0xFF30CF: Serial.println(" 4");    remoteBtn = 4;     break;
    case 0xFF18E7: Serial.println(" 5");    remoteBtn = 5;     break;
    case 0xFF7A85: Serial.println(" 6");    remoteBtn = 6;     break;
    case 0xFF10EF: Serial.println(" 7");    remoteBtn = 7;     break;
    case 0xFF38C7: Serial.println(" 8");    remoteBtn = 8;     break;
    case 0xFF5AA5: Serial.println(" 9");    remoteBtn = 9;     break;
    case 0xFF42BD: Serial.println(" *");    remoteBtn = 10;     break;
    case 0xFF4AB5: Serial.println(" 0");    remoteBtn = 0;     break;
    case 0xFF52AD: Serial.println(" #");    remoteBtn = 11;     break;
    default: Serial.println(" other button     ");
  } // end Case
  delay(500); // do not get immediate repeat
} //end translateIR

void remoteEvents(){
  switch (remoteBtn) {
      case 1:
        steadyLaser();
        break;
      case 2:
        blinkingLaser();
        break;
      case 3:
        blinkingfasterLaser();
      case 0:
        offLaser();
      default:
        offLaser();
  }
}

void steadyLaser(){
  digitalWrite(laserPin, HIGH);
}

void blinkingLaser(){
  digitalWrite(laserPin, HIGH);
  delay(450);
  digitalWrite(laserPin, LOW);
  delay(450);
}

void blinkingfasterLaser(){
  digitalWrite(laserPin, HIGH);
  delay(250);
  digitalWrite(laserPin, LOW);
  delay(250);
}

void offLaser(){
  digitalWrite(laserPin, LOW);
}

Piethon:
(...)
I don't exactly know what happened, but I decided to start from scratch and it works now! The following code works:
(...)

What happens was this:

Piethon:
(...)
I think I made a discovery - I had the IR receiver plugged in to pin 7, but then I thought to switch the pin to a PWM pin, so I did. It's now plugged in to PIN ~5, and I cleaned up the code.
(...)

I don't know why (maybe because I don't know the IR receiver) but could be almost anything (bad connection, for example).

Is nice to hear that is working now!

Good work!