Hey, making a Program where I can send strings via IR. The String is always 5 letters long and only contains A-I . It's sending and receiving data but not the same as it's sending.
Does someone have an idea why?
My Code:
#include <IRremote.h>
const int IR_SEND_PIN = 19;
const int IR_RECEIVE_PIN = 18;
void setup() {
Serial.begin(9600);
// Initialize the IR sender and receiver
IrSender.begin(IR_SEND_PIN, DISABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN);
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
}
void sendIRSignal(uint32_t value) {
IrSender.sendNEC(value, 32); // Send NEC code with 32-bit data
Serial.println("Sent");
}
uint32_t stringToIRCode(const String& input) {
uint32_t code = 0;
for (int i = 0; i < 5; i++) {
char c = input.charAt(i);
if (c >= 'A' && c <= 'I') {
code = (code << 4) | (c - 'A');
} else {
return 0; // Invalid character
}
}
return code;
}
String IRCodeToString(uint32_t code) {
String result = "";
for (int i = 0; i < 5; i++) {
uint8_t value = (code >> (4 * (4 - i))) & 0xF;
if (value >= 0 && value <= 8) {
result += char('A' + value);
} else {
return ""; // Invalid value
}
}
return result;
}
void loop() {
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n');
input.trim(); // Remove whitespace
if (input.length() == 5) {
uint32_t code = stringToIRCode(input);
if (code != 0) {
sendIRSignal(code);
} else {
Serial.println("Invalid input. Only letters A-I are allowed.");
}
} else {
Serial.println("Invalid input. The input must be exactly 5 letters long.");
}
}
// Receive an IR signal
if (IrReceiver.decode()) { // Check if an IR signal has been received
uint32_t receivedCode = IrReceiver.decodedIRData.decodedRawData;
Serial.print("Received signal: ");
Serial.println(receivedCode, HEX); // Output the received value
String decodedString = IRCodeToString(receivedCode);
if (!decodedString.isEmpty()) {
Serial.print("Decoded string: ");
Serial.println(decodedString);
} else {
Serial.println("Invalid IR code received.");
}
IrReceiver.resume(); // Prepare the receiver for the next signal
}
}
Some examples:
(the number after Sent is the variable "value" in void sendIRSignal)
Want to send: IABCC Output: 19:40:54.168 -> Sent (524578)
19:40:54.168 -> Received signal: 44801000
19:40:54.201 -> Decoded string: ABAAA
Want to send: AABBC Output: 19:40:54.168 -> Sent 274
19:40:54.168 -> Received signal: 44800000
19:40:54.201 -> Decoded string: AAAAA
Want to send: ABCDE Output: 19:40:54.168 -> Sent 4660
19:40:54.168 -> Received signal: 2C480000
19:40:54.201 -> Decoded string: IAAAA
Want to send: IGHAC Output: 19:40:54.168 -> Sent 550658
19:40:54.168 -> Received signal: 40E61000
19:40:54.201 -> Decoded string: GBAAA
Tryd to send data raw like this:
const uint16_t rawData[] = {9000, 4500, 4500, 4500, 4500, 4500, 4500, 4500};
but not even that worked, The receiver always gets other rawdata thats always kinda random (always around the same but quite some differences) tryd multiple receivers.
Why could it fail to even send raw data, I'm clueless
My code
#include <IRremote.h>
const int IR_SEND_PIN = 19;
const int IR_RECEIVE_PIN = 18;
// Example raw data for a short IR signal
const uint16_t rawData[] = {9000, 4500, 4500, 4500, 4500, 4500, 4500, 4500};
const int rawDataLength = sizeof(rawData) / sizeof(rawData[0]);
void setup() {
Serial.begin(9600);
// Initializes the IR sender and receiver
IrSender.begin(IR_SEND_PIN, DISABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN);
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
Serial.println("IR sender and receiver initialized");
}
void sendIRSignal() {
IrSender.sendRaw(rawData, rawDataLength, 38); // Sends raw data with 38 kHz carrier frequency
Serial.println("Sent:");
for (int i = 0; i < rawDataLength; i++) {
Serial.print(rawData[i]);
Serial.print(" ");
}
Serial.println();
}
void loop() {
// Sending an IR signal
sendIRSignal();
delay(3000); // Wait time between transmissions
// Receiving an IR signal
if (IrReceiver.decode()) { // Checks if an IR signal has been received
Serial.print("Received signal: ");
for (uint16_t i = 0; i < IrReceiver.decodedIRData.rawDataPtr->rawlen; i++) {
Serial.print(IrReceiver.decodedIRData.rawDataPtr->rawbuf[i]);
Serial.print(" ");
}
Serial.println();
IrReceiver.resume(); // Prepares the receiver for the next signal
}
}
I suggest to spend some more time with the documentation and studying the examples in the IRRemote library.
It is essential that you understand how to send raw data and the limitations of the method first. Once you understand the examples, you should have no problem getting your encoding scheme to work.