// // LCD1602 to Arduino Uno connection example
// #include <LiquidCrystal.h>
// LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
// // LCD1602 to Arduino Uno connection example
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
#define IR_SENSOR 2 //<--- change this to D7
#define IR_ACTIVE_STATE LOW
#define SWITCH 4 //<--- change this to D4
#define SWITCH_STATE LOW
#define AMP_SENSOR 5// <<-- change this to A5
#define SENSOR_INTERVAL 100
float current = 0.00;
double sumCurrent = 0.00;
int lcdMode = 0;
byte customChar[] = {
B11111,
B10000,
B01000,
B00100,
B00100,
B01000,
B11111,
B00000
};
void setup() {
lcd.begin(16,2);
lcd.createChar(0, customChar);
lcd.setCursor(0, 0);
lcd.print("Generator-Ampere");
lcd.setCursor(3, 1);
lcd.print("Calculator");
delay(1000);
lcd.clear();
// put your setup code here, to run once:
pinMode(IR_SENSOR , INPUT_PULLUP);
pinMode(AMP_SENSOR , INPUT);
pinMode(SWITCH , INPUT_PULLUP);
// digitalWrite(IR_SENSOR , !IR_ACTIVE_STATE);
Serial.begin(9600);
}
volatile unsigned long startTime = 0;
volatile unsigned long stopTime = 0;
double resultTimer = 0.00;
double resultCurrent = 0.00;
bool calcState = false;
bool antiBounce = false;
bool stateBounce = false;
bool sumState = false;
bool clearBounce = false;
char byteSum ;
void loop() {
if (Serial.available() > 0) {
char byteSum = Serial.read();
if (byteSum == 'c') {
digitalWrite(IR_SENSOR , LOW);
Serial.println(digitalRead(IR_SENSOR));
calcState = false;
// byteSum = 0;
}
if (byteSum == 's') {
digitalWrite(IR_SENSOR , HIGH);
Serial.println(digitalRead(IR_SENSOR));
// byteSum = 0;
}
if (byteSum == 'x') {
clearAllState();
}
}
currentSensor_Timer();
SumOfAmpere();
lcdStatus(lcdMode);
// put your main code here, to run repeatedly:
}
unsigned long currentSensor_premillis = 10;
void currentSensor_Timer() {
if (millis() - currentSensor_premillis >= SENSOR_INTERVAL) {
currentSensor_premillis = millis();
current = 12;
}
}
int previousLcd = 0;
void lcdStatus(int lcdModes) {
if (lcdModes != previousLcd) {
lcd.clear();
Serial.println("lcd cleared");
}
previousLcd = lcdModes;
switch (lcdModes) {
case 0:
lcd.setCursor(0, 0);
lcd.print("STATUS : Waiting");
break;
case 1:
lcd.setCursor(0, 0);
lcd.print("TIME : ");
lcd.print(String((micros() - startTime) / 1000000.0));
lcd.print(" Sec ");
lcd.setCursor(0, 1);
lcd.print("AMP : ");
lcd.print("0.10");
lcd.print(" Amp " );
break;
case 2:
lcd.setCursor(0, 0);
lcd.setCursor(0, 0);
lcd.print("Result: ");
lcd.print(String(resultTimer));
lcd.print("s ");
lcd.setCursor(0, 1);
lcd.write((byte)0);
lcd.print("AMP : ");
lcd.print(String(resultCurrent));
lcd.print("A " );
break;
}
}
void SumOfAmpere() {
if (digitalRead(SWITCH) == SWITCH_STATE) {
if (clearBounce == false) {
clearAllState();
lcdMode = 0;
Serial.println("Cleared all memories variable !");
clearBounce = true;
}
}
else {
clearBounce = false;
}
// Starting record time after pass A Black Line
// While driving in white starting sum current values
// Stopping record time and calculate time of a->b
if (calcState == false) {
if (digitalRead(IR_SENSOR) == IR_ACTIVE_STATE && antiBounce == false) {
lcdMode = 0;
if (antiBounce == false) {
Serial.println("Waiting for start");
antiBounce = true;
}
}
else if (!digitalRead(IR_SENSOR) == IR_ACTIVE_STATE && antiBounce == true) {
lcdMode = 1;
if (stateBounce == false) {
startTime = micros();
Serial.println("StartTime :" + String(startTime));
stateBounce = true;
}
sumCurrent += current;
sumState = true;
Serial.println("Sum : " + String(sumCurrent));
}
else if (digitalRead(IR_SENSOR) == IR_ACTIVE_STATE && antiBounce && sumState) {
stopTime = micros();
Serial.println("StopTime: " + String(stopTime));
antiBounce = false;
stateBounce = false;
calcState = true;
}
}
else if (calcState == true) {
lcdMode = 2;
//clear all variable state
resultTimer = ((stopTime - startTime) / 1000000.0);
resultCurrent = sumCurrent / 1000.0;
if( sumState){
Serial.print(stopTime);
Serial.print("-");
Serial.println(startTime);
Serial.println(String("Result Timer : ") + String(resultTimer) + String(" Sec"));
Serial.println("Result Current : " + String(resultCurrent) + String(" A") );
sumState = false;
}
}
}
void clearAllState() {
startTime = 0;
stopTime = 0;
resultTimer = 0.00;
resultCurrent = 0.00;
calcState = false;
antiBounce = false;
stateBounce = false;
sumState = false;
}