Hey, im am trying to build a distance measure module, i use a IR sensor module.
This is beginning of the code i use now:
#include <LiquidCrystal.h>
int soundpin = 5;
int led = 6;
int led2 = 4;
int buttonState = 0;
int buttonPin = 2;
int buttonState2 = 0;
int buttonPin2 = 3;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(6, OUTPUT);
pinMode(4, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
int sensorValue = analogRead(A0);
Serial.println(buttonState2, DEC);
if (buttonState2 == HIGH){
digitalWrite(led2, HIGH);
if (buttonState == HIGH) {
digitalWrite(led, HIGH);
tone(5, 1500,20);
lcd.setCursor(0, 0);
if(sensorValue >= 547){
lcd.print("18cm");
}
else {
lcd.clear();
}
lcd.setCursor(0, 0);
if(sensorValue >= 542){
lcd.print(" 19cm");
}
else {
lcd.clear();
}
lcd.setCursor(0, 0);
if(sensorValue >= 533){
lcd.print(" 20cm");
} else {
lcd.clear();
}
delay(100);
}
else {
digitalWrite(led, LOW);
delay(10);
}
}
else {
digitalWrite(led2, LOW);
lcd.clear();
}
}
But is there a better way to calibrate the sensor ?
If i use if(sensorValue >= 547){ it will be a LOT of these ! One for each cm. So it will be 100 of these.
lcd.print("18cm"); }
Sorry for my bad english but i hope you guys understand my probmlem.
-Jonas