Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <NewPing.h>
- #include "SSD1306Ascii.h"
- #include "SSD1306AsciiWire.h"
- // 0X3C+SA0 - 0x3C or 0x3D
- #define I2C_ADDRESS 0x3C
- SSD1306AsciiWire oled;
- #define TRIGGER_PIN 9 // Arduino pin tied to trigger pin on the ultrasonic sensor.
- #define ECHO_PIN 10 // Arduino pin tied to echo pin on the ultrasonic sensor.
- #define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
- NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
- #include "OneButton.h"
- int ButtonPin = 12; //we need the OneButton library
- OneButton button(ButtonPin, true); //attach a button on pin D12 to the library
- int temperature = 0;
- long distance = 0;
- float pingtime = 0;
- float pi = 3.1415926;
- int base = 0; //distance to bottom of container (offset)
- int height = 0; //actual height of liquid.
- float diameter = 93.11; //diameter in mm of container
- float radius = diameter/2; //Area = pi * r^2
- float volume = 0; //volume of liquid. π * radius² * height
- float liters = 0;
- void setup() {
- // Start the Serial communication
- Serial.begin(115200);
- pinMode(ButtonPin, INPUT_PULLUP);
- button.attachClick(singleclick); // link the function to be called on a singleclick event.
- Wire.begin();
- Wire.setClock(400000L);
- oled.begin(&Adafruit128x32, I2C_ADDRESS);
- oled.setFont(TimesNewRoman16_italic); //(Adafruit5x7);
- oled.clear();
- oled.setCursor(0,0); //(col,row)
- //oled.set2X();
- oled.setInvertMode(2);
- oled.println(" CAL ");
- delay(2000); //delay to calibrate base
- pingtime = sonar.ping();
- pingtime = pingtime + (.6 * temperature);
- pingtime = pingtime/2;
- distance = pingtime * (0.343);
- base = distance;
- delay(1000);
- oled.setCursor(0,0); //(col,row)
- //oled.set2X();
- oled.setInvertMode(2);
- oled.println(" LITERS ");
- }
- void loop() {
- delay(250);
- pingtime = sonar.ping();
- pingtime = pingtime + (.6 * temperature);
- pingtime = pingtime/2;
- distance = pingtime * (0.343);
- height = base - distance;
- //Volume = π * radius² * height
- volume = pi * sq(radius) * height; //cubic mm
- liters = volume * 1.0E-6; //convert to liters
- // Display the distance
- oled.setFont(TimesNewRoman16); //(Adafruit5x7);
- oled.clearToEOL();
- oled.setCursor(40,10); //(col,row)
- oled.setInvertMode(0);
- oled.println(liters);
- Serial.print("$"); //send to process app
- Serial.print(liters);
- Serial.print(";");
- // Serial.println(liters);
- // Serial.println(distance);
- // Serial.println();
- }
- void singleclick(){
- base = distance; //set the base in mm Bottom of container
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement