When i connect my IR sensor to a lcd screen and a serial monitor the IR sensor doesnt display the proper values, whatever i do the values just jump around, does anybody else have this problem and how can it be solved?
i use a mega arduino board, a 16x2 lcd-screen and an MH-sensor-series Flying-Fish.
This is the basic code i used to test the IR sensor:
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin with the arduino pin number it is connected to
const int rs = 13, e = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;
LiquidCrystal lcd(rs, e, d4, d5, d6, d7);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
//
Serial.begin(9600);
}
void loop() {
//set the position where the lcd writes
lcd.setCursor(0, 1);
//short delay so that the lcdscreen is readable
delay(500);
//clear the lcd of the previous value
lcd.clear();
//print the value pulled from the IR sensor on the lcd
lcd.print(analogRead(A0));
//print the value pulled from the IR sensor on the serial monitor
Serial.println(analogRead(A0));
};
here is an example of the serial monitor output:
15:30:26.275 -> 307
15:30:26.974 -> 435
15:30:27.396 -> 644
15:30:27.856 -> 476
15:30:28.286 -> 353
15:30:28.798 -> 567
15:30:29.316 -> 687
15:30:29.814 -> 495
15:30:30.284 -> 381
15:30:30.805 -> 642
15:30:31.313 -> 521
15:30:31.832 -> 145