#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0X3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int analogPin = A0;
int Pressure = 0;
#define maxPress 100
#define minPress 20
int measurePressure() {
int val = analogRead(analogPin);
if(val<=102) {
Serial.print(val);
Serial.println(": Sensor voltage low.");
return -1;
} else if (val >= 3686) {
Serial.print(val);
Serial.println(": Sensor voltage high");
return maxPress + 1;
} else {
int readPress = map(val, 103, 3685, 0, maxPress);
//Serial.print(": Mapped pressure: ");
//Serial.print(readPress);
//Serial.println("psi");
return readPress;
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Begin Program");
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSC1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(1,0);
display.println("Display initialized");
display.display();
}
void loop() {
// put your main code here, to run repeatedly:
delay(500); // this speeds up the simulation
int newPressure = measurePressure();
if (Pressure != newPressure) {
Pressure = newPressure;
printPressure(Pressure);
}
}
void printPressure(int pressVal) {
Serial.print(Pressure);
Serial.println("psi");
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10,0);
display.println("PRESSURE");
display.setCursor(20,24);
display.print(pressVal);
display.println(" psi");
int width = map(pressVal, 0, maxPress, 0, SCREEN_WIDTH-1);
int minPressStart = map(minPress, 0, maxPress, 0, SCREEN_WIDTH-1);
Serial.println(width);
display.fillRect(0, 52, width, 10, WHITE);
display.fillRect(minPressStart, 50, 2, 14, WHITE);
if (pressVal <= minPress) {
display.setTextSize(1);
display.setCursor(minPressStart+10, 54);
display.print("Low Pressure");
}
display.display();
}