I hope you're doing well. I'm currently working on a school project involving an IR remote model and need some assistance.
The project aims to create a model IR remote control with four buttons, each programmed to send a different IR code when pressed.
I have successfully set up the hardware and written the code for the project. However, I am facing an issue with the transmission of IR codes. Despite my efforts, I have been unable to resolve this issue.
➜ do yourself a favour and please read How to get the best out of this forum and post accordingly (including your code with code tags and necessary documentation for your ask like your exact circuit and power supply, links to components etc).
Is the assignment to make your own IR transmitter? Why not just use an old TV remote or something where the transmission codes (marks and spaces) are already defined in the library?
//#include<IRremote.hpp>
#include <IRremote.hpp>
const int buttonUP = 4; // Digital pin for button 1
const int buttonDOWN = 5; // Digital pin for button 2
const int buttonLEFT = 6; // Digital pin for button 3
const int buttonRIGHT = 7; // Digital pin for button 4
const int buttonSTOP = 8;// Digital pin for button 5
const int ledPin = 3; // Digital pin for LED
IRsend irsend;
// Declare results globally
void setup() {
pinMode(buttonUP, INPUT_PULLUP);
pinMode(buttonDOWN, INPUT_PULLUP);
pinMode(buttonLEFT, INPUT_PULLUP);
pinMode(buttonRIGHT, INPUT_PULLUP);
pinMode(buttonSTOP, INPUT_PULLUP); // Corrected the pin mode for buttonSTOP
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Initialize serial communication
// Enable IR receiver
}
void loop() {
// Read the state of each button
int buttonUPState = digitalRead(buttonUP);
int buttonDOWNState = digitalRead(buttonDOWN);
int buttonLEFTState = digitalRead(buttonLEFT);
int buttonRIGHTState = digitalRead(buttonRIGHT);
int buttonSTOPState = digitalRead(buttonSTOP);
// Send different hexadecimal codes for each button press
if (buttonUPState == LOW) {
sendHexCode(0xE0E020DF);
}
if (buttonDOWNState == LOW) {
sendHexCode(0xE0E0A05F);
}
if (buttonLEFTState == LOW) {
sendHexCode(0xE0E0609F);
}
if (buttonRIGHTState == LOW) {
sendHexCode(0xE0E010EF);
}
if (buttonSTOPState == LOW) { // Corrected the condition for buttonSTOP
sendHexCode(0xE0E0906F);
}
// Turn on the LED if any button is pressed
if (buttonUPState == LOW || buttonDOWNState == LOW || buttonLEFTState == LOW || buttonRIGHTState == LOW || buttonSTOPState == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
void sendHexCode(int code) {
// Print the hexadecimal code
Serial.print("Sending code: 0x");
Serial.println(code, HEX);
//Send the hexadecimal code over IR
irsend.sendNEC(code, 32); // Assuming NEC protocol with 32 bits
}