Я читаю ту же переменную «расстояние» от ультразвукового датчика, и печатаю его как на серийный, так и на ЖК-дисплей, но серийный номер показывает правильное число, ЖК-дисплей показывает случайное число
все работают отдельно, датчик и дисплей, ноне сенсор а жк дисплей, когда я добавил lcd.print
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myServo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
lcd.setCursor(12,0);
lcd.print(distance);
lcd.setCursor(12,1);
lcd.print(pos);
distance = calculateDistance();// Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree
Serial.print(pos); // Sends the current degree into the Serial Port
Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
Serial.print(distance); // Sends the distance value into the Serial Port
Serial.print("." ); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
}
//lcd.setCursor(12,0);
//lcd.print(distance);
// Repeats the previous lines from 165 to 15 degrees
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myServo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
lcd.setCursor(12,0);
lcd.print(distance);
lcd.setCursor(12,1);
lcd.print(pos);
distance = calculateDistance();
Serial.print(pos);
Serial.print(",");
Serial.print(distance);
Serial.print(".");
}
}
// Function for calculating the distance measured by the Ultrasonic sensor
int calculateDistance(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
distance= duration*0.034/2;
return distance;
}