#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int tankheight = 50; //50cm
int fillheight = 45 //in cm
int trig = 2;
int echo = 3;
void testdrawstyles(int a){
display.clearDisplay();
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0,0); // Start at top-left corner
display.print(F("level="));
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(WHITE);
display.print(a);
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(WHITE);
display.print(F("%"));
display.display();
delay(500);
}
void setup() {
Serial.begin(9600);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
// display.display();
//delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
// Draw a single pixel in white
display.drawPixel(10, 10, WHITE);
display.display();
delay(1000);
//testdrawstyles(); // Draw 'stylized' characters
}
void loop() {
long time = 0, height = 0, heightpercent = 0;
// Transmitting pulse
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
// Waiting for pulse
time = pulseIn(echo, HIGH);
// Calculating distance
height = time / (29 *2);
// height = height - 6 ; // offset correction
// height = 20 - h; // water height, 0 - 100 cm
heightpercent = 5 * height; // distance in %, 0-100 %
// Sending to computer
Serial.print(height);
// Serial.print(" cm\n");
Serial.print("\n");
delay(1000);
// testdrawstyles(heightpercent);
testdrawstyles(height);
}