Description
Me and my friend are working on an IR project with an arduino. Our goal is to be able to use it as a remote, with rebindable buttons. So far, we are using an lcd and Serial Monitor to track stuff, but we are facing an issue when it comes to the waitForIRSignal command. We want the lcd to display the new hex code when it is recieved, and then to store that hex code to apply later in the loop (we haven't gotten here yet we are still debugging). Instead of this, the LCD either skips printing the code recieved message or clears everything and the Serial Monitor starts freaking out, and both stop working when I try to bind a new hex code by using our remote. Can somebody help me troubleshoot? Here is our code:
#include <IRremote.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 4, 5, 6, 7);
// Create IR transmitter object
unsigned long hexCode1;
int buttonPin1 = A0;
int buttonValue1 = 0;
int buttonPin2 = A1;
int buttonValue2 = 0;
const int IR_LED_PIN = 3;
IRsend irSender;
IRsend irsend(IR_LED_PIN);
int IR_RECV_PIN = 2;
IRrecv irrecv(IR_RECV_PIN);
decode_results results;
void setup()
{
pinMode(3, OUTPUT);
lcd.clear();
Serial.begin(9600);
lcd.begin(16, 2);
digitalWrite(3, LOW);
irSender.begin(3);
irrecv.enableIRIn();
}
void waitForIRSignal() {
unsigned long startMillis = millis(); // Start a timer to track time
bool receivedSignal = false; // To track if the signal is received
while (!irrecv.decode(&results)) {
if (millis() - startMillis > 5000) { // Timeout after 5 seconds
Serial.println("Timeout: No IR signal received.");
lcd.clear();
lcd.print("Timeout");
delay(1000); // Display timeout message for a moment
lcd.clear(); // Clear the screen after timeout message
return; // Exit the function if no signal is received within timeout
}
delay(10); // Short delay to avoid overwhelming the processor
}
// Once a valid signal is received
Serial.print("IR code received: ");
Serial.println(results.value, HEX); // Print the decoded IR code in HEX format
hexCode1 = results.value; // Store the received IR code
lcd.clear();
lcd.print("Code Bound");
delay(1000); // Display the "Code Bound" message for 1 second
lcd.clear(); // Clear the screen after the message
irrecv.resume(); // Prepare for the next signal
}
void loop()
{
buttonValue2 = analogRead(buttonPin2);
if(buttonValue2 > 1000){
lcd.clear();
lcd.print("Bind New Code: 1");
waitForIRSignal();
}
buttonValue1 = analogRead(buttonPin1);
if(buttonValue1 > 1000){
Serial.println("Sending Code");
lcd.clear();
lcd.print("Sending...");
irsend.sendNECRaw(0xED12BF40);
delay(1000); // Wait 1 seconds before sending again
}else{
lcd.clear();
lcd.print("Not Pressed");
delay(100);
}
}