#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
U8G2_SSD1306_64X32_1F_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
const int btn1 = 2;
const int btn2 = 3;
const int heater = 5;
const int bat = A0;
const int flow = A2;
int dutyCycle = 50;
int count = 0;
String p1,p2,p3;
void setup() {
Serial.begin(115200);
u8g2.begin();
pinMode(btn1, INPUT_PULLUP);
pinMode(btn2, INPUT_PULLUP);
pinMode(heater, OUTPUT);
pinMode(bat, INPUT);
pinMode(flow, INPUT);
}
void loop() {
// read battery voltage
int adc = analogRead(bat);
float voltage = adc * (5.0 / 1023.0);
Serial.print("Battery Voltage : ");
Serial.println(voltage);
int vbat = map(voltage * 100, 290, 420, 0, 100);
Serial.print("Battery Percentage : ");
if (vbat < 0) vbat = 0;
if (vbat > 100) vbat = 100;
Serial.println(vbat);
delay(100);
// increase duty cycle
if (!digitalRead(btn1)) {
dutyCycle += 5;
if (dutyCycle > 100) dutyCycle = 100;
delay(250);
}
// decrese duty cycle
if (!digitalRead(btn2)) {
dutyCycle -= 5;
if (dutyCycle < 0) dutyCycle = 0;
delay(250);
}
Serial.print("PWM DutyCycle : ");
Serial.println(dutyCycle);
// calculate output voltage
float vout = dutyCycle * 0.01 * voltage;
Serial.print("Output Voltage : ");
Serial.println(vout);
// read flow sensor
int pressure = analogRead(flow);
Serial.print("Flow Sensor : ");
Serial.println(pressure);
// when flow sensor triggered
if (pressure > 512) {
count += 1;
int pwm = map(dutyCycle, 0, 100, 0, 255);
analogWrite(heater, pwm);
delay(1000); // ignition delay
} else {
analogWrite(heater, 0);
}
Serial.print("Trigger Count : ");
Serial.println(count);
Serial.println();
String t1 = String(vbat) + "%";
String t2 = String(count);
String t3 = String(vout) + "V";
if(p1 != t1 || p2 != t2 || p3 != t3){
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_t0_11_tf);
u8g2.drawStr(10, 10, t1.c_str());
u8g2.drawStr(10, 20, t2.c_str());
u8g2.drawStr(10, 30, t3.c_str());
u8g2.sendBuffer();
p1 = t1;
p2 = t2;
p3 = t3;
}
}