Here's a basic example of Arduino code for an IR remote control. In this example, we'll use the IRremote library, which simplifies the process of working with infrared signals from remote controls. You will need an IR receiver module and an IR remote control for this code to work. Copy code
#include <IRremote.h> // Define the IR receiver pin const int IR_RECEIVE_PIN = 11; // Create an instance of the IRremote library IRrecv irrecv(IR_RECEIVE_PIN); decode_results results; void setup() { Serial.begin(9600); // Start the IR receiver irrecv.enableIRIn(); } void loop() { if (irrecv.decode(&results)) { // Print the received IR code to the serial monitor Serial.println(results.value, HEX); // Handle different remote control button presses switch (results.value) { case 0xFFA25D: // Replace with the actual HEX code of your remote button // Code to execute when the specified button is pressed Serial.println("Button A Pressed"); break; case 0xFF629D: // Replace with the actual HEX code of another button // Code to execute when the specified button is pressed Serial.println("Button B Pressed"); break; // Add more cases for other buttons as needed default: // Code to execute for unrecognized buttons (if any) break; } irrecv.resume(); // Receive the next value } }
Here's how this code works: Include the IRremote library at the beginning of the code. Define the IR receiver pin (the pin to which your IR receiver module is connected). Create an instance of the IRrecv class from the IRremote library. In the setup() function, start the serial communication and enable the IR receiver. In the loop() function, check if there's a decoded IR signal using irrecv.decode(&results). If a signal is received, print the HEX value of the button press to the serial monitor. Use a switch statement to handle different button presses based on their HEX values. Replace the HEX values in the cases with the actual codes from your remote control. Call irrecv.resume() to prepare for receiving the next IR signal. Make sure to replace the placeholder HEX values in the switch statement with the actual HEX codes corresponding to the buttons on your IR remote control. You can capture the IR codes for your specific remote control using the "IRrecvDumpV2" example provided with the IRremote library.