Hi, I make a little circuit with arduino uno and an LCD. The circuit is the same of this image http://arduino.cc/en/uploads/Tutorial/LCD_bb.png and I connected an IR receiver at pin 6. After all the afternoon of try I can't solve my problem:
When the LCD is connected to arduino I receive wrong codes from my remote control, but when i disconnect the LCD the codes are right! For example if I push the number 1 on my remote control I don't receive the right code but a different code every time.
I'm available for further information. Help me! =(
Here is the code:
#include <LiquidCrystal.h>
#include <IRremote.h>
int RECV_PIN = 6;
IRrecv irrecv(RECV_PIN);
decode_results results;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
lcd.print("hello, world!");
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(LED_PIN, OUTPUT);
}
void loop() {
lcd.setCursor(0, 1);
digitalWrite(LED_PIN, LOW);
if (irrecv.decode(&results)) {
if (results.value==0xE13DDA28)
Serial.println("----------------OK");
Serial.println(results.value, HEX);
lcd.print(results.value, HEX);
irrecv.resume(); // Receive the next value
}
}
It is not clear what your problem is. I have loaded you code into my Nano and have tried it. I made a few changes because of some hardware differences. For example. I moved the IR sensor to pin 3 because I was already using pin 6 for my LCD. I am also using a 20X4 instead of a 16X2.
You say that "I receive wrong codes" but I don't know what that means. However, I will make some observation and see if it hits upon anything
Observations:
I did not see where LED_PIN was being used.
You write your results to "lcd.setCursor(0, 1);" without clearing the existing data. There for if you have "111111" and then write "222" you will have "222111" on your display.
You might want to move your "lcd.setCursor(0, 1);" to only execute if you have results instead of every loop.
My code looks like:
// The LCD is usually interfaced via 16 pins which are labelled as shown below:
//Connections to Arduino
// LCD Connection
// 1. GND - Ground GND
// 2. VDD - 3 - 5V 5V
// 3. VO - Contrast (Tap off a 5K - 10K pot across VCC and Ground)
#define LCD_RS 8 // 4. RS - Register Select - 0=Command / 1=Character Arduino Pin as defined
// 5. RW - Read/Write - 0=Write or 1=Read GND
#define LCD_ENABLE 9 // 6. E - Enable - Enable data transmit Arduino Pin as defined
// 7. DB0 - Data Bit 0 N/A
// 8. DB1 - Data Bit 1 N/A
// 9. DB2 - Data Bit 2 N/A
// 10. DB3 - Data Bit 3 N/A
#define LCD_DB4 4 // 11. DB4 - Data Bit 4 - used in 4 bit operation Arduino Pin as defined
#define LCD_DB5 5 // 12. DB5 - Data Bit 5 - used in 4 bit operation Arduino Pin as defined
#define LCD_DB6 6 // 13. DB6 - Data Bit 6 - used in 4 bit operation Arduino Pin as defined
#define LCD_DB7 7 // 14. DB7 - Data Bit 7 - used in 4 bit operation Arduino Pin as defined
#define LCD_Backlight 10 // 15. BL1 - Backlight + Emitter of 2N3904, Collector to VCC, Base to D9 via 10K resistor
// 16. BL2 - Backlight - GND
#include <LiquidCrystal.h>
#include <IRremote.h>
int RECV_PIN = 3;
IRrecv irrecv(RECV_PIN);
decode_results results;
LiquidCrystal lcd(LCD_RS, LCD_ENABLE, LCD_DB4, LCD_DB5, LCD_DB6, LCD_DB7);
void setup() {
lcd.begin(20, 4);
lcd.clear();
pinMode(LCD_Backlight, OUTPUT); analogWrite(LCD_Backlight, 128); // Set the brightness of the backlight
lcd.print("hello, world!");
Serial.begin(9600);
irrecv.enableIRIn();
// pinMode(LED_PIN, OUTPUT);
}
void loop() {
// digitalWrite(LED_PIN, LOW);
if (irrecv.decode(&results)) {
if (results.value==0xE13DDA28)
Serial.println("----------------OK");
Serial.println(results.value, HEX);
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(results.value, HEX);
irrecv.resume(); // Receive the next value
}
}
You did not indicate the type of remote, so I tested with a Sony TV remote. It seems to work fine.
Thanks for your reply RandaIIR :). For "I receive wrong codes" I mean that I receive different codes by pressing the same button on my remote. For my tests I used a samsung tv remote.
Now I try to explain the problem better. If I make a circuit with only the IR receiver and using the IRrecvDemo sketch in the IRremote library, by pressing the button number 1 on my remote I obtain some codes like this:
where I think that E13DDA28 is the correct code for the buttom 1.
If I make the circuit and use the sketch like the first post with the LCD and I press the buttom 1 I never obtain E13DDA28 but if I disconnect the LCD sometimes I obtain the code E13DDA28 in the results.
Is it normal by pressing the same button on the remote I obtain different codes?
I don't know what to tell you. I am not a fan of the 'IRremote' library I and do not know the inner workings. I have not look inside the LCD library in a few years but I seem to recall there were some interrupt sensitive sections.
Maybe it has something to do with interrupts.
Maybe the backlight from the LCD is interfering with the receiver sensitivity.
I have seen behavior like this earlier.
Some remotes send very long codes (often repeated twice - and the second can be reversed)
In addition many uses a toggle bit to detect to repeated presses (unlike press/hold)
Try masking away to a single byte : your code & 0xff // 255=B11111111
then :
switch (code)
{
case n1:
//do something when code=n1
break;
case n2:
//do something when var equals 2
break;
....
correct masking.
In out project we had just same behaviour as the one you see.
I guess you will find one code that is unik for a spesific key - use that one.
the 'unknown' result will be discarded anyway