#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int pressurePin = A1;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();
lcd.backlight();
}
void loop() {
unsigned long startTime = micros(); // Start time of the loop
for (int i = 0; i < 100; i++) {
int rawValue = analogRead(pressurePin);
float millibars = map(rawValue, 0, 1023, 0, 2000) / 10.0;
// Display the value on the LCD
lcd.setCursor(0, 0);
lcd.print("Pr: ");
lcd.print(millibars);
lcd.println(" mbar ");
// Display the value in the serial monitor
Serial.print("Pr: ");
Serial.print(millibars);
Serial.println(" mbar ");
// Delay to control the sampling rate
delayMicroseconds(10000); // 10,000 microseconds delay for approximately 100 samples per second
}
unsigned long endTime = micros(); // End time of the loop
// Calculate the time taken by the loop
unsigned long loopTime = endTime - startTime;
// Subtract the loop time from the desired delay time to maintain the desired sampling rate
unsigned long delayTime = 10000 - loopTime;
if (delayTime > 0) {
delayMicroseconds(delayTime);
}
}