Problems with Storing Hex Codes

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);
  }
}

Post an annotated schematic showing exactly how you have wired it. Show all connections, power, ground and power sources. If you just post a pin list I will ignore it.What code have you tried that shows the LCD works properly?

I made a mockup on Tinker CAD, hopefully its good enough:

On the other hand, I know the LCD works because we did some testing with it prior to this project and it displayed messages just fine.

If this mockup is unclear with connections, here is the link to the page: Login - Tinkercad

That picture does not do it for me. When the LCD worked is it the same circuit? Have you tried that code with this hardware setup?

It appears you have your button improperly wired. Rather than connecting the other side to 5V, it needs to be connected to GND. Then, in setup() you need to declare these pins as INPUT_PULLUP so they will normally read HIGH unless the button is pressed and then they will read LOW (You can use the analog pins as digital pins).

The way you have it now, the input is floating when not pressed, so no guarantees what value you will read.

As for the code, if you get a timeout in our waitForIRSignal() function, you never resume receving any future signals.

1 Like

I think the buttons are okay for now, and the advice with the irrecv.resume(); did help, but the entire thing still gets stuck in the loop, and the hexCode1 value is 0. How would I fix those?

I'm noticing this only happens when I recieve with the serial monitor open too. On top of that, the HexCode value is inconsistent.

Minor issues, perhaps:

  • Make lcd.begin(); the first thing you do. Clearing it before that may or may not cause issues, but it isn't normal.
  • if that button is truly wired between an analog input and 5V, and you insist on using analogRead(), place a pull down resistor from GND to the analog input. Otherwise, expect this to bite you when you least expect it.
  • please describe "gets stuck in the loop" differently. There is no place in loop() where it can "get stuck".

famous last words..... Your ask for help, then ignore help.

plunk.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.