Energy Meter

Hey guys, i need help for my thesis. I want to display 2 different set of data when i press the keypad my problem is the two data must me looping. how can i do this? here is my code:

#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Password.h>

Password password = Password ("1234");
char PasswordArray[5];
int i = 0;

LiquidCrystal lcd(12, 11, 37, 35, 33, 31, 29, 27, 25, 23);

int backlight = 30;
boolean ledPin_state;
char key;

int voltagePin = 0;
int adcVolts[255];
int magV = 0;
int magCountV = 0;
double voltage;
double Vstep = 0.0048875855;
double Vscope = 0.4283461622;
double VTR = 18.00766284;

int currentPin = 1;
int adcCurrent[255];
int magI = 0;
int magCountI = 0;
double current;

double power = 0;
double powerFactor;
double phase;
int countDelta;

unsigned long last_kWhTime, kWhTime;
float kilowattHour = 0.0;

const byte ROWS = 4;
const byte COLS = 4; 

char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte colPins[COLS] = { 44, 42, 40, 38 };

byte rowPins[ROWS] = { 52, 50, 48, 46 }; 

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
    Serial.begin(9600);
    lcd.begin(20,4);
    pinMode(backlight, OUTPUT);
    digitalWrite(backlight, HIGH);
    lcd.clear();
    ledPin_state = digitalRead(backlight);   
    keypad.addEventListener(keypadEvent);
    keypad.setDebounceTime(200);
    Display1(); 
}

void loop()
{
  key = keypad.getKey();
  solving();
  
}

void keypadEvent(KeypadEvent key){
  switch (keypad.getState()){
    case PRESSED:
    if (key == 'B') {
      digitalWrite(backlight,!digitalRead(backlight));
      ledPin_state = digitalRead(backlight);
    }
    
    if (key == 'A'){
      Display2();
      delay(5000);
      Display1();
    }
    
    switch (key){
      case '*':
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Enter Pass Code:");
      lcd.setCursor(0,2);
      resetPasswordArray();
      break;
      
      case '#':
      checkPassword();
      break;
      
      default:
      if ((key != '#') && (key != '*')){
        lcd.print(key);
      }
      if (key != '#'){
       PasswordArray[i] = key; i++;
      }
  }
  break;
}
}

void solving()
{
   for (int i=0; i < 255; i++){
  adcVolts[i] = analogRead(voltagePin);
  adcCurrent[i] = analogRead(currentPin);
  }

magV = 0;
magI = 0;
magCountV = 0;
magCountI = 0;

for (int i=0; i < 255; i++){
  if (adcVolts[i] > magV){
    magV = adcVolts[i];
    magCountV = i;
  }
  if (adcCurrent[i] > magI){
    magI = adcCurrent[i];
    magCountI = i;
  }
}

  voltage = (7.2686218 * pow((magV * Vstep * Vscope), 0.98175303)) * VTR;
  current = (2.7371981 * pow((magI * Vstep * Vscope), 1.2706487));
  
  countDelta = abs(magCountI - magCountV);
  
  while (countDelta > 73){
    countDelta = countDelta - 73;
  }
  
  phase = countDelta * 360 / 73;
  powerFactor = abs(cos( phase / 180*3.1459));
  power = voltage * current * powerFactor;
  
  last_kWhTime = kWhTime;
  kWhTime = millis();
  
  kilowattHour += (power/1000)*((kWhTime - last_kWhTime)/3600000.0);
  
}

void Display1()
{
  lcd.begin(20,4);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("ECons(kWh):");
  lcd.setCursor(13,0);
  lcd.print(kilowattHour, 2);
  lcd.setCursor(0,1);
  lcd.print("ECons(PHP):");
//  lcd.setCursor(13,1);
//  lcd.print(current, 5);
  lcd.setCursor(0,2);
  lcd.print("Limit(kWh):");
//  lcd.setCursor(13,2);
//  lcd.print(power,5);
  lcd.setCursor(0,3);
  lcd.print("Date:");
  delay(1000);
}

void Display2()
{
   lcd.clear();
   lcd.begin(20,4);
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Voltage (V):");
   lcd.setCursor(13,0);
   lcd.print(voltage, 0);
   lcd.setCursor(0,1);
   lcd.print("Current (I):");
   lcd.setCursor(13,1);
   lcd.print(current, 2);
   lcd.setCursor(0,2);
   lcd.print("Power(Watt):");
   lcd.setCursor(13,2);
   lcd.print(power,2);
   lcd.setCursor(0,3);
   lcd.print("PF:");
   lcd.setCursor(4,3);
   lcd.print(powerFactor,2);
   lcd.setCursor(10,3);
   lcd.print("Rate:");
   lcd.setCursor(17,3);
//   lcd.print(rate);
}

void resetPasswordArray() { //Clear entry array
  
       for (i=0;i<5;i++){
    PasswordArray[i] = '\0'; //set all characters in the entry array to null
    }
     
    i=0; //reset counter
}

void checkPassword() { 
  if (i>3 && i<5){
    if (password.is(PasswordArray)){
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Access Granted");
      delay(2000);
    } 
    else {
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Access Denied");
      delay(2000);
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Enter Pass Code:");
      lcd.setCursor(0,2);
      
    }
  }
  else{
     lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Must Be 4 Charac!");
      delay(2000);
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Enter Pass Code:");
      lcd.setCursor(0,2);
  }
  i=0;
  resetPasswordArray();
  password.reset();
}

Thank you very much!

you must set a variable that defines the current screen to be displayed.

void loop()
{
  key = keypad.getKey();
  solving();
  if (key != NOKEYPRESSED)
  {
      currentScreen = keynumber;
  }
  if (currentScreen == 1) display1();
  else if (currentScreen == 2) display2();
  else if (currentScreen == 3) display3();
  ...
}

get the idea? (you can also use a "switch case" construct)

thanks rob, but its not clear to me coz' i'm new to arduino (sorry). Can you elaborate further?

Thank you very much...

Rob, can you help me with this,

#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Password.h>

Password password = Password ("1234");
char PasswordArray[5];
int i = 0;
const byte ledPin13 = 13;

long Delay1 = 0;
long Delay2 = 0;

LiquidCrystal lcd(12, 11, 37, 35, 33, 31, 29, 27, 25, 23);

int backlight = 30;
boolean ledPin_state;
char key;

int voltagePin = 0;
int adcVolts[255];
int magV = 0;
int magCountV = 0;
double voltage;
double Vstep = 0.0048875855;
double Vscope = 0.4283461622;
double VTR = 18.00766284;

int currentPin = 1;
int adcCurrent[255];
int magI = 0;
int magCountI = 0;
double current;

double power = 0;
double powerFactor;
double phase;
int countDelta;

unsigned long last_kWhTime, kWhTime;
float kilowattHour = 0.0;

const byte ROWS = 4;
const byte COLS = 4; 

char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte colPins[COLS] = { 44, 42, 40, 38 };

byte rowPins[ROWS] = { 52, 50, 48, 46 }; 

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
    Serial.begin(9600);
    lcd.begin(20,4);
    pinMode(backlight, OUTPUT);
    digitalWrite(backlight, HIGH);
    lcd.clear();
    ledPin_state = digitalRead(backlight);   
 //   keypad.addEventListener(keypadEvent);
    keypad.setDebounceTime(200);
    pinMode (ledPin13, OUTPUT);
}

void loop()
{
  key = keypad.getKey();
  solving();
  
  if (key == 'A')
  {
    Display1();
  }
  
 if (key == 'B')
 {
   Display2();
 }
}

void solving()
{
   for (int i=0; i < 255; i++){
  adcVolts[i] = analogRead(voltagePin);
  adcCurrent[i] = analogRead(currentPin);
  }

magV = 0;
magI = 0;
magCountV = 0;
magCountI = 0;

for (int i=0; i < 255; i++){
  if (adcVolts[i] > magV){
    magV = adcVolts[i];
    magCountV = i;
  }
  if (adcCurrent[i] > magI){
    magI = adcCurrent[i];
    magCountI = i;
  }
}

  voltage = (7.2686218 * pow((magV * Vstep * Vscope), 0.98175303)) * VTR;
  current = (3.8208634 * pow((magI * Vstep * Vscope), 1.1647062));
  
  countDelta = abs(magCountI - magCountV);
  
  while (countDelta > 73){
    countDelta = countDelta - 73;
  }
  
  phase = countDelta * 360 / 73;
  powerFactor = abs(cos( phase / 180*3.1459));
  power = voltage * current * powerFactor;
  
  last_kWhTime = kWhTime;
  kWhTime = millis();
  
  kilowattHour += (power/1000)*((kWhTime - last_kWhTime)/3600000.0);
  
}

void Display1()
{
  if (millis() >= Delay1){
  lcd.begin(20,4);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("ECons(kWh):");
  lcd.setCursor(13,0);
  lcd.print(kilowattHour, 2);
  lcd.setCursor(0,1);
  lcd.print("ECons(PHP):");
//  lcd.setCursor(13,1);
//  lcd.print(current, 5);
  lcd.setCursor(0,2);
  lcd.print("Limit(kWh):");
//  lcd.setCursor(13,2);
//  lcd.print(power,5);
  lcd.setCursor(0,3);
  lcd.print("Date:");
  lcd.setCursor(6,3);
  lcd.print(millis());
  
  Delay1 += 1000;
  }
}

void Display2()
{
   lcd.clear();
   lcd.begin(20,4);
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Voltage (V):");
   lcd.setCursor(13,0);
   lcd.print(voltage, 0);
   lcd.setCursor(0,1);
   lcd.print("Current (I):");
   lcd.setCursor(13,1);
   lcd.print(current, 2);
   lcd.setCursor(0,2);
   lcd.print("Power(Watt):");
   lcd.setCursor(13,2);
   lcd.print(power,2);
   lcd.setCursor(0,3);
   lcd.print("PF:");
   lcd.setCursor(4,3);
   lcd.print(powerFactor,2);
   lcd.setCursor(10,3);
   lcd.print("Rate:");
   lcd.setCursor(17,3);
//   lcd.print(rate);
}
  
void resetPasswordArray() { //Clear entry array
  
       for (i=0;i<5;i++){
    PasswordArray[i] = '\0'; //set all characters in the entry array to null
    }
     
    i=0; //reset counter
}

void checkPassword() { 
  if (i>3 && i<5){
    if (password.is(PasswordArray)){
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Access Granted");
      delay(2000);
    } 
    else {
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Access Denied");
      delay(2000);
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Enter Pass Code:");
      lcd.setCursor(0,2);
      
    }
  }
  else{
     lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Must Be 4 Charac!");
      delay(2000);
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Enter Pass Code:");
      lcd.setCursor(0,2);
  }
  i=0;
  resetPasswordArray();
  password.reset();
}

void blink (const byte LED, int DELAY){
  
  digitalWrite(LED, !digitalRead(LED));
  delay (DELAY);
  digitalWrite(LED, !digitalRead(LED));
  
}

my problem here, is when i press A the lcd display the display1 but does not get the present value of what i want to measure. Same also with B. what code can you suggest that when i press A or B the lcd will display the data but also get the values i want to measure at the same time?

This is a big help… Thank you!

added lastKey to hold the last pressed key, so pressing the A or the B just switch display mode.
give it a try

#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Password.h>

Password password = Password ("1234");
char PasswordArray[5];
int i = 0;
const byte ledPin13 = 13;

long Delay1 = 0;
long Delay2 = 0;

LiquidCrystal lcd(12, 11, 37, 35, 33, 31, 29, 27, 25, 23);

int backlight = 30;
boolean ledPin_state;
char key;

int voltagePin = 0;
int adcVolts[255];
int magV = 0;
int magCountV = 0;
double voltage;
double Vstep = 0.0048875855;
double Vscope = 0.4283461622;
double VTR = 18.00766284;

int currentPin = 1;
int adcCurrent[255];
int magI = 0;
int magCountI = 0;
double current;

double power = 0;
double powerFactor;
double phase;
int countDelta;

unsigned long last_kWhTime, kWhTime;
float kilowattHour = 0.0;

const byte ROWS = 4;
const byte COLS = 4; 

char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte colPins[COLS] = { 44, 42, 40, 38 };

byte rowPins[ROWS] = { 52, 50, 48, 46 }; 

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
    Serial.begin(9600);
    lcd.begin(20,4);
    pinMode(backlight, OUTPUT);
    digitalWrite(backlight, HIGH);
    lcd.clear();
    ledPin_state = digitalRead(backlight);   
 //   keypad.addEventListener(keypadEvent);
    keypad.setDebounceTime(200);
    pinMode (ledPin13, OUTPUT);
}


char lastKey = '\0';

void loop()
{
  key = keypad.getKey();

  solving();

  if (key != lastKey) lastKey = key;  // remember key pressed.
  switch(lastKey)
  {
    case 'A' : Display1(); break;
    case 'B' : Display2(); break;
    default:  Display1(); break; // default display
  }
}

void solving()
{
   for (int i=0; i < 255; i++){
  adcVolts[i] = analogRead(voltagePin);
  adcCurrent[i] = analogRead(currentPin);
  }

magV = 0;
magI = 0;
magCountV = 0;
magCountI = 0;

for (int i=0; i < 255; i++){
  if (adcVolts[i] > magV){
    magV = adcVolts[i];
    magCountV = i;
  }
  if (adcCurrent[i] > magI){
    magI = adcCurrent[i];
    magCountI = i;
  }
}

  voltage = (7.2686218 * pow((magV * Vstep * Vscope), 0.98175303)) * VTR;
  current = (3.8208634 * pow((magI * Vstep * Vscope), 1.1647062));
  
  countDelta = abs(magCountI - magCountV);
  
  while (countDelta > 73){
    countDelta = countDelta - 73;
  }
  
  phase = countDelta * 360 / 73;
  powerFactor = abs(cos( phase / 180*3.1459));
  power = voltage * current * powerFactor;
  
  last_kWhTime = kWhTime;
  kWhTime = millis();
  
  kilowattHour += (power/1000)*((kWhTime - last_kWhTime)/3600000.0);
  
}

void Display1()
{
  if (millis() >= Delay1){
  lcd.begin(20,4);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("ECons(kWh):");
  lcd.setCursor(13,0);
  lcd.print(kilowattHour, 2);
  lcd.setCursor(0,1);
  lcd.print("ECons(PHP):");
//  lcd.setCursor(13,1);
//  lcd.print(current, 5);
  lcd.setCursor(0,2);
  lcd.print("Limit(kWh):");
//  lcd.setCursor(13,2);
//  lcd.print(power,5);
  lcd.setCursor(0,3);
  lcd.print("Date:");
  lcd.setCursor(6,3);
  lcd.print(millis());
  
  Delay1 += 1000;
  }
}

void Display2()
{
   lcd.clear();
   lcd.begin(20,4);
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Voltage (V):");
   lcd.setCursor(13,0);
   lcd.print(voltage, 0);
   lcd.setCursor(0,1);
   lcd.print("Current (I):");
   lcd.setCursor(13,1);
   lcd.print(current, 2);
   lcd.setCursor(0,2);
   lcd.print("Power(Watt):");
   lcd.setCursor(13,2);
   lcd.print(power,2);
   lcd.setCursor(0,3);
   lcd.print("PF:");
   lcd.setCursor(4,3);
   lcd.print(powerFactor,2);
   lcd.setCursor(10,3);
   lcd.print("Rate:");
   lcd.setCursor(17,3);
//   lcd.print(rate);
}
  
void resetPasswordArray() { //Clear entry array
  
       for (i=0;i<5;i++){
    PasswordArray[i] = '\0'; //set all characters in the entry array to null
    }
     
    i=0; //reset counter
}

void checkPassword() { 
  if (i>3 && i<5){
    if (password.is(PasswordArray)){
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Access Granted");
      delay(2000);
    } 
    else {
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Access Denied");
      delay(2000);
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Enter Pass Code:");
      lcd.setCursor(0,2);
      
    }
  }
  else{
     lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Must Be 4 Charac!");
      delay(2000);
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Enter Pass Code:");
      lcd.setCursor(0,2);
  }
  i=0;
  resetPasswordArray();
  password.reset();
}

void blink (const byte LED, int DELAY){
  
  digitalWrite(LED, !digitalRead(LED));
  delay (DELAY);
  digitalWrite(LED, !digitalRead(LED));
  
}

Thanks Rob, the code works but what i want is when i pressed B it will display the Display2 and wait until another key is pressed.

XD

Rob, how can i make that when i press B, it will stay on my Display2? the codes you gave me does not stay on Display2 it goes back to Display1 after few seconds?

kengwapo: Thanks Rob, the code works but what i want is when i pressed B it will display the Display2 and wait until another key is pressed.

XD

I made an error on purpose in this code but apparently you didn't notice it.

Do you want lastKey always be changed if Key changes? No, only when key == A or Key == B

your turn ;)

Define a global variable that records which display is supposed to be presented.

When you want the display to change, update the variable.

The rest of the code just needs to present whichever display is selected by the variable.

I really can't understand. huhuhu. pls. help me.

only this part need to be changed

void loop()
{
  key = keypad.getKey();

  solving();

  if (key != lastKey) lastKey = key;  // <<<<<<<<<<<<<<<<<<<<<<<< this line should change
  switch(lastKey)
  {
    case 'A' : Display1(); break;
    case 'B' : Display2(); break;
    default:  Display1(); break; // default display
  }
}

I gave almost the code in the previous post.

I cant figure it out.

kengwapo: I cant figure it out.

Pick a simpler project, and come back to this one once you have a better grasp of the basics. The goal here is to help you to do the project yourself, not to do it for you.

kengwapo:
I cant figure it out.

i need help for my thesis.

What is your major?

Sorry i can't change this project, i am an ECE student and the project that was approved by our panel was challenging because it is not only about electronics but also electrical and i can't change that because it was already approved. We are only given 2 months to work this out (we have other subjects too, so we cant give all of our time to this), and im on the last week and sorry for yesterday i just feel exhausted and pressured my hardware is already finished and my problem is on the programming side because i dont have a strong background on this. All i need is a little help from you guys, i started the codes and i believe i can finish it, with your help also. Hope you'll help me, if i don't know please teach me. Thank you very much, God bless you.

Please be honest and give credit for the code help to the Arduino community in both your final code and reports.

void loop()
{
  key = keypad.getKey();
  solving();

  if ( (key == 'A') || (key =='B') ) lastKey = key; // see, it is not that difficult.
  switch(lastKey)
  {
    case 'A' : Display1(); break;
    case 'B' : Display2(); break;
    default:  Display1(); break; // default display
  }
}

Yes sir, i will.. Thank you very much, you don't know how helpful this is.. i want to learn, its just i am time pressured.. i will try your code.. :)

Rob it works! Thank you so much! Now i will work for a password.. XD

Yes sir, i will.. Thank you very much, you don't know how helpful this is.. i want to learn, its just i am time pressured.. i will try your code..

Learning takes time, life == making choices. .... choosing where we spend our time on. we are all time pressured in life (OK we may deny it but in the end time is always running short)

And yes, I do know how helpful this is.

Thank you Rob, and sorry…

Rob, can you give me ideas on how can i make my ‘’ and ‘#’ to have another function. In my codes below i used '’ to trigger Pass code Entry and ‘#’ to check the pass code if correct. The next thing i want if the pass code is correct is the user can input some data and store it in the EEPROM, ‘*’ to clear the input data and ‘#’ to store it in the EEPROM. Here is my code:

#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Password.h>

Password password = Password ("1234");
char PasswordArray[5];
int i = 0;
const byte ledPin13 = 13;

long Delay1 = 0;
long Delay2 = 0;

LiquidCrystal lcd(12, 11, 37, 35, 33, 31, 29, 27, 25, 23);

int backlight = 30;
boolean ledPin_state;
char key;

int voltagePin = 0;
int adcVolts[255];
int magV = 0;
int magCountV = 0;
double voltage;
double Vstep = 0.0048875855;
double Vscope = 0.4283461622;
double VTR = 18.00766284;

int currentPin = 1;
int adcCurrent[255];
int magI = 0;
int magCountI = 0;
double current;

double power = 0;
double powerFactor;
double phase;
int countDelta;

unsigned long last_kWhTime, kWhTime;
float kilowattHour = 0.0;

const byte ROWS = 4;
const byte COLS = 4; 

char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte colPins[COLS] = { 44, 42, 40, 38 };

byte rowPins[ROWS] = { 52, 50, 48, 46 }; 

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
    Serial.begin(9600);
    lcd.begin(20,4);
    pinMode(backlight, OUTPUT);
    digitalWrite(backlight, HIGH);
    lcd.clear();
    ledPin_state = digitalRead(backlight);   
    keypad.addEventListener(keypadEvent);
    keypad.setDebounceTime(200);
    pinMode (ledPin13, OUTPUT);
}


char lastKey = '\0';

void loop()
{
  solving();
  
  key = keypad.getKey();

   if ( (key == 'A') || (key =='B') || (key == 'C' ) || (key == '*') || (key == '#') ) lastKey = key;
  switch(lastKey)
  {
    case 'A' : Display1(); break;
    case 'B' : Display2(); break;
    case 'C' :
      if (key == 'C'){
        digitalWrite(backlight,!digitalRead(backlight));
        ledPin_state = digitalRead(backlight);
      }
      break;
    case '*': break;
    case '#': break;
 //   default:  break; // default display
  }
  
  if ((key == '*') || (key == '#') || (key == '1') || (key == '2') || (key == '3') || (key == '4') || (key == '5') || (key == '6') || (key == '7') || (key == '8') || (key == '9') || (key == '0') ){
    switch (key){
      case '*':
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Enter Pass Code:");
      lcd.setCursor(0,2);
      resetPasswordArray();
      break;
      
      case '#':
      checkPassword();
      break;
      
      default: 
      if ((key != '#') && (key != '*')){
        lcd.print(key);
      }
      if (key != '#'){
       PasswordArray[i] = key; i++;
      }
    }
      
  }
}

void keypadEvent(KeypadEvent key){
/*  switch (keypad.getState()){
    case PRESSED:
    switch (key){
      case '*':
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Enter Pass Code:");
      lcd.setCursor(0,2);
      resetPasswordArray();
      break;
      
      case '#':
      checkPassword();
      break;
      
      default:
      if ((key != '#') && (key != '*')){
        lcd.print(key);
      }
      if (key != '#'){
       PasswordArray[i] = key; i++;
      }
  }
  break;
}*/
}

void solving()
{
   for (int i=0; i < 255; i++){
  adcVolts[i] = analogRead(voltagePin);
  adcCurrent[i] = analogRead(currentPin);
  }

magV = 0;
magI = 0;
magCountV = 0;
magCountI = 0;

for (int i=0; i < 255; i++){
  if (adcVolts[i] > magV){
    magV = adcVolts[i];
    magCountV = i;
  }
  if (adcCurrent[i] > magI){
    magI = adcCurrent[i];
    magCountI = i;
  }
}

  voltage = (7.2686218 * pow((magV * Vstep * Vscope), 0.98175303)) * VTR;
  current = (3.8208634 * pow((magI * Vstep * Vscope), 1.1647062));
  
  countDelta = abs(magCountI - magCountV);
  
  while (countDelta > 73){
    countDelta = countDelta - 73;
  }
  
  phase = countDelta * 360 / 73;
  powerFactor = abs(cos( phase / 180*3.1459));
  power = voltage * current * powerFactor;
  
  last_kWhTime = kWhTime;
  kWhTime = millis();
  
  kilowattHour += (power/1000)*((kWhTime - last_kWhTime)/3600000.0);
  
}

void Display1()
{
  if (millis() >= Delay1){
  lcd.begin(20,4);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("ECons(kWh):");
  lcd.setCursor(13,0);
  lcd.print(kilowattHour, 2);
  lcd.setCursor(0,1);
  lcd.print("ECons(PHP):");
//  lcd.setCursor(13,1);
//  lcd.print(current, 5);
  lcd.setCursor(0,2);
  lcd.print("Limit(kWh):");
//  lcd.setCursor(13,2);
//  lcd.print(power,5);
  lcd.setCursor(0,3);
  lcd.print("Date:");
  lcd.setCursor(6,3);
  
  Delay1 += 1000;
  }
}

void Display2()
{
  if (millis() >= Delay1){
    lcd.begin(20,4);
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Voltage (V):");
   lcd.setCursor(13,0);
   lcd.print(voltage, 0);
   lcd.setCursor(0,1);
   lcd.print("Current (I):");
   lcd.setCursor(13,1);
   lcd.print(current, 2);
   lcd.setCursor(0,2);
   lcd.print("Power(Watt):");
   lcd.setCursor(13,2);
   lcd.print(power,2);
   lcd.setCursor(0,3);
   lcd.print("PF:");
   lcd.setCursor(4,3);
   lcd.print(powerFactor,2);
   lcd.setCursor(10,3);
   lcd.print("Rate:");
   lcd.setCursor(17,3);
//   lcd.print(rate);
    Delay1 += 1000;
  }
}
  
void resetPasswordArray() { //Clear entry array
  
       for (i=0;i<5;i++){
    PasswordArray[i] = '\0'; //set all characters in the entry array to null
    }
     
    i=0; //reset counter
}

void checkPassword() { 
  if (i>3 && i<5){
    if (password.is(PasswordArray)){
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Access Granted");
      delay(2000);
        
    } 
    else {
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Access Denied");
      delay(2000);
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Enter Pass Code:");
      lcd.setCursor(0,2);
      
    }
  }
  else{
     lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Must Be 4 Charac!");
      delay(2000);
      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Enter Pass Code:");
      lcd.setCursor(0,2);
  }
  i=0;
  resetPasswordArray();
  password.reset();
}

void blink (const byte LED, int DELAY){
  
  digitalWrite(LED, !digitalRead(LED));
  delay (DELAY);
  digitalWrite(LED, !digitalRead(LED));
  
}