#include "Config.h"
// #include <Preferences.h>
// Preferences preferences;
bool LastState = false;
void setup() {
pinMode(PILOT, OUTPUT);
pinMode(PUMP, OUTPUT);
digitalWrite(PUMP, HIGH);
pinMode(STCP, OUTPUT); // Latch
pinMode(SHCP, OUTPUT); // ClockPin
pinMode(DS, OUTPUT); // Data
pinMode(DSIN, INPUT);
pinMode(PSW, INPUT_PULLUP);
Serial.begin(115200);
// preferences.begin("app", false);
// int counter = preferences.getInt("counter"); // default to 1
// Serial.print("Reboot count: ");
// Serial.println(counter);
// counter++;
// preferences.putInt("counter", counter);
Serial.println("Setup sucess!");
digitalWrite(PILOT, LOW);
Serial.print("Enter Volume : ");
}
void loop() {
char key = KeyScan();
if(key != NULL && LastKey != key){
if(key != 'A' && key != 'B' && key != 'C' && key != 'D' && key != '*' && key != '#'){
Vol += key;
Serial.print(key);
}else if(!DotCheck && key == 'D'){
Vol += '.';
Serial.print(".");
DotCheck = true;
}
if(key == '#'){
Serial.println();
Serial.println(Vol);
float t = GetTime(FlowRate, Vol.toFloat());
Serial.print("Time : ");
Serial.print(t);
Serial.println(" mS");
float v = GetVolume(FlowRate, (int)t)*1000;
Serial.print("Vol : ");
Serial.print(v);
Serial.println(" mL");
TimeUse = t;
digitalWrite(PUMP, LOW);
PumpStart = true;
PumpLastTime = millis();
}
LastKey = key;
}
if(millis() - LastTime >= 500){
LastKey = ' ';
LastTime = millis();
}
delay(50); // test processing loop
if(PumpStart && millis() - PumpLastTime >= TimeUse){
PumpStart = false;
digitalWrite(PUMP, HIGH);
float t = millis() - PumpLastTime;
float v = GetVolume(FlowRate, t)*1000;
Serial.print("Time : ");
Serial.print(t);
Serial.println(" mS");
Serial.print("Vol : ");
Serial.print(v);
Serial.println(" mL");
}
}
char KeyScan(){
for(int col = 0; col < 4; col++){
digitalWrite(STCP, LOW);
// shiftOut(DS, SHCP, MSBFIRST, 0xFF);
shiftOut(DS, SHCP, MSBFIRST, ColList[col]);
digitalWrite(STCP, HIGH);
digitalWrite(STCP, LOW);
digitalWrite(STCP, HIGH);
byte data = 0;
for (int i = 7; i >= 0; i--) {
data |= (digitalRead(DSIN) == HIGH)? 1 << i : 0;
digitalWrite(SHCP, HIGH); // Shift out the next bit
digitalWrite(SHCP, LOW);
}
byte row = (data << 4) & 0xf0;
if(row != 0x00){
row = (row == 0x10)? 0 : (row == 0x20)? 1 : (row == 0x40)? 2 : 3;
return keys[row][col];
}
}
return NULL;
}
float GetTime(float FlowRate, float Volume){
// Flowrate base L/sec
float time = Volume / FlowRate;
return time*1000; // millissec
}
float GetVolume(float FlowRate, int Time){
return FlowRate * Time/1000.0;
}
/**************** Note ****************/
/*
# FlowRate unit
- Base
- L/SEC
- Input Unit
- L/HOUR
- L/MIN
- L/SEC
- Convest Function
- L/HOUR -> L/SEC
- L/MIN -> L/SEC
* Arg [Base,DestBase,Var]
* Re [VarDestBase]
*/