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!