/*
MSJ Researchers World
Date - 18th NOV 2024
Mentor - Mr. Siranjeevi M
Contact - 7373771991
*/
#include <IRremote.h>
int RED_PIN = 3; // Pin connected to the red component of RGB LED
int GREEN_PIN = 5; // Pin connected to the green component
int BLUE_PIN = 6; // Pin connected to the blue component
int receiver_pin = 11; // IR receiver pin changed to 11
int btn_value = 0;
IRrecv receiver(receiver_pin);
void setup() {
// Initialize Serial communication
Serial.begin(9600);
// Initialize the IR receiver
receiver.enableIRIn();
// Set RGB LED pins as output
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
void loop() {
// Check if an IR code is received
if (receiver.decode()) {
translateIR();
receiver.resume(); // Ready to receive the next signal
}
}
void translateIR() {
btn_value = receiver.decodedIRData.command;
Serial.println(btn_value); // Print the IR code to Serial Monitor
// Control the RGB LED based on the received IR command
switch (btn_value) {
case 162: // Power button (turns off the LED)
Serial.println("Power");
setColor(0, 0, 0); // Turn off the LED (set all colors to 0)
break;
case 226: // Menu button (Show instructions)
Serial.println("Menu");
Serial.println("Press TEST to blink the LED");
Serial.println("Press POWER to turn the LED OFF");
Serial.println("Press 1 to turn LED red");
Serial.println("Press 2 to turn LED green");
Serial.println("Press 3 to turn LED blue");
break;
case 34: // Test button (blink LED)
Serial.println("Test");
blinkLED();
break;
case 48: // Button 1 (turn LED red)
Serial.println("1 - Red");
setColor(255, 0, 0); // Set LED to red
break;
case 24: // Button 2 (turn LED green)
Serial.println("2 - Green");
setColor(0, 255, 0); // Set LED to green
break;
case 122: // Button 3 (turn LED blue)
Serial.println("3 - Blue");
setColor(0, 0, 255); // Set LED to blue
break;
case 16: // Button 4 (turn LED yellow - red + green)
Serial.println("4 - Yellow");
setColor(255, 255, 0); // Set LED to yellow
break;
case 56: // Button 5 (turn LED purple - red + blue)
Serial.println("5 - Purple");
setColor(255, 0, 255); // Set LED to purple
break;
case 90: // Button 6 (turn LED cyan - green + blue)
Serial.println("6 - Cyan");
setColor(0, 255, 255); // Set LED to cyan
break;
case 66: // Button 7 (turn LED white - red + green + blue)
Serial.println("7 - White");
setColor(255, 255, 255); // Set LED to white
break;
default:
Serial.println("Unknown button pressed");
break;
}
}
// Function to set the RGB color of the LED
void setColor(int red, int green, int blue) {
analogWrite(RED_PIN, red); // Set the red component (0-255)
analogWrite(GREEN_PIN, green); // Set the green component (0-255)
analogWrite(BLUE_PIN, blue); // Set the blue component (0-255)
}
// Function to blink the LED
void blinkLED() {
setColor(255, 0, 0); // Red LED on
delay(300);
setColor(0, 255, 0); // Green LED on
delay(300);
setColor(0, 0, 255); // Blue LED on
delay(300);
setColor(0, 0, 0); // Turn off the LED
delay(300);
}