I need some help to modify my code

Hello everyone, i need some help to modify my 'ev charger project' arduino code.

Let me explain the duty of my code.

I have relay card in my circuit. When i enter the correct password, relay will be triggered, charge module will work and charge module will need to work until i enter the password again to stop it. If i enter password again it should stop charging. I mean set the relay state non triggered and hold like this until i enter password again.

Everything is working fine in my code, when i enter the password relay is triggered and charging is activated but it is not holding like this. when the enter password screen shows up it is like refreshing i mean the code behaves like not holding like i want, you can think like my code is opening door when i enter pass and closing automatically.

What i want is like that; open the door when i enter pass, stay opened, if i enter again the pass, close after i enter the password again and stay closed.

I hope i mentioned what i want exactly, I am not so familiar to explain these type of things in english.

If u want me to explain more or if u have questions please feel free to ask.

Thanks in advance.

code;

#include <Keypad.h>
#include<EEPROM.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

char password[4];
char initial_password[4],new_password[4];
int i=0;
int relay_pin = 11;
char key_pressed=0;
const byte rows = 4; 
const byte columns = 4; 

char hexaKeys[rows][columns] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte row_pins[rows]={7, 8, 9, 10};
byte column_pins[columns]={3, 4, 5, 6};
Keypad keypad_key=Keypad( makeKeymap(hexaKeys),row_pins,column_pins,rows,columns);

void setup(){
lcd.init();
lcd.backlight();
pinMode(relay_pin, OUTPUT);
digitalWrite(11, HIGH);

lcd.setCursor(5,0);
lcd.print("B & F");
lcd.setCursor(2,1);
lcd.print("ELECTRONICS");
delay(5000);
lcd.clear();

lcd.setCursor(4,0);
lcd.print("WELCOME");
delay(3000);
lcd.clear();

lcd.setCursor(0,0);
lcd.print("ELECTRIC VEHICLE");
lcd.setCursor(0,1);
lcd.print("CHARGING STATION");
delay(3000);
lcd.clear();

lcd.setCursor(1,0);
lcd.print("PLEASE CONNECT");
lcd.setCursor(1,1);
lcd.print("CHARGING CABLE");
delay(3000);
lcd.clear();

lcd.setCursor(4,0);
lcd.print("TO YOUR");
lcd.setCursor(0,1);
lcd.print("ELECTRIC VEHICLE");
delay(3000);
lcd.clear();

lcd.setCursor(0,0);
lcd.print("AFTER CONNECTION");
lcd.setCursor(0,1);
lcd.print("ENTER YOUR PIN");
delay(3000);
lcd.clear();


lcd.print(" Enter Password");
lcd.setCursor(6,1);
initialpassword();
}
void loop(){
digitalWrite(relay_pin,HIGH);

key_pressed = keypad_key.getKey();
if(key_pressed=='#')
change();
if (key_pressed)
{
password[i++]=key_pressed;
lcd.print('*');
}
if(i==4)
{
delay(200);
for(int j=0;j<4;j++)
initial_password[j]=EEPROM.read(j);
if(!(strncmp(password, initial_password,4))){
lcd.clear();

lcd.setCursor(2,0);
lcd.print("PASSWORD IS");
lcd.setCursor(4,1);
lcd.print("CORRECT");
digitalWrite(relay_pin,LOW);
delay(8000);

lcd.setCursor(0,0);
lcd.print("to set new pin");
lcd.setCursor(0,1);
lcd.print("Press >#< button");
delay(5000);
lcd.clear();
lcd.print(" Enter Password");
lcd.setCursor(0,1);
i=0;
}
else{
digitalWrite(relay_pin, HIGH);
lcd.clear();

lcd.setCursor(1,0);
lcd.print("Wrong Password");
lcd.setCursor(3,1);
lcd.print("Try Again");

delay(2000);

lcd.setCursor(0,0);
lcd.print("Press >#< button");
lcd.setCursor(0,1);
lcd.print("to set new pin");
delay(2000);
lcd.clear();
lcd.print(" Enter Password");
lcd.setCursor(0,1);
i=0;
}}}


void change(){
  int j=0;
lcd.clear();
lcd.print("Current Password");
lcd.setCursor(0,1);
while(j<4){
char key=keypad_key.getKey();
if(key)
{
new_password[j++]=key;
lcd.print(key);
}
key=0;}
delay(500);

if((strncmp(new_password, initial_password, 4))){
lcd.clear();
lcd.print("Wrong Password");
lcd.setCursor(0,1);
lcd.print("Try Again");
delay(1000);}

else{
j=0;
lcd.clear();
lcd.print("New Password");
lcd.setCursor(0,1);

while(j<4){
char key=keypad_key.getKey();
if(key)
{
initial_password[j]=key;
lcd.print(key);
EEPROM.write(j,key);
j++;}}
lcd.print("PIN is Changed");
delay(1000);}
 
lcd.clear();
lcd.print(" Enter Password");
lcd.setCursor(0,1);
key_pressed=0;
}

void initialpassword(){
int j;
for(j=0;j<4;j++)
EEPROM.write(j,j+49);
for(j=0;j<4;j++)
initial_password[j]=EEPROM.read(j);}

Cypher locks have a "timeout" which means, after entering a passcode, the door opens, you enter, then the lock closes immediately before the door closes. To exit from the inside, you would press a button to open the door lock to exit.

The simulation is here (no relay yet... and wrong keypad... need to re-program the sim)

Posting an annotated schematic would be much better then posting pictures that are not correct and do not have the needed information to answer your question. Since there are many modules that have the same name but are different best to post links to the technical information on the modules you are using.

Every time thru the loop you make the pin HIGH.
When the password is correct you make the pin LOW.

Here is a example of what i want;

as you see in this simulation, when you enter the password (1234) lamp is on and still on until you enter password(1234) again. Thats all i want. I need to modify to work like this in my code.

Agreed.

      +-----------------------------------+
      |  +---|EXT|--------|USB|---+       |  +---------------+
      |  |    PWR          A5/SCL |-------|--| SCL  LCD 1602 |
      |  |                 A4/SDA |-------|--| SDA           |       +-----+
      |  |          UNO      AREF |    +--|--| GND           |  +----|LOCK+|
      |  |                    GND |----+  +--| VCC           |  | +--|LOCK-|
      |  | IOREF          SCK/D13 |    |  |  +---------------+  | |  +-----+
      |  | RST             DI/D12 |    |  |  +---------------+  | |  +-----+
      |  | 3V3             DO/D11~|----|--|--|SIG  RELAY  N/C|--+ +--| PS- |
      +--| +5V                D10~|    |  +--|VCC         COM|-------| PS+ |
         | GND                 D9~|    +-----|GND         N/O|       +-----+
         | GND                 D8 |          +---------------+          ||
         | Vin                 D7 |          +---------------+          ++===(mains)
         |                     D6~|          |        KEYPAD |
+--------| A0                  D5~|--------R4| 1   2   3   A |
| +------| A1                  D4 |--------R3| 4   5   6   B |
| | +----| A2             INT1/D3 |--------R2| 7   8   9   C |
| | | +--| A3             INT0/D2~|--------R1| *   0   #   D |
| | | |  | A4/DA  RS CK DI  TX>D1 |          +---------------+
| | | |  | A5/CL  GD D0 5V  RX<D0 |              C  C  C  C
| | | |  +------------------------+              1  2  3  4
| | | +------------------------------------------+  |  |  |
| | +-----------------------------------------------+  |  |
| +----------------------------------------------------+  |
+---------------------------------------------------------+

1 Like

Hello @batuhansa - You can use the Tinkercad code (click on simulate >> code). Just be sure to make your project like the Tinkercad project.

1 Like

A great CAD program is KiCad, it is free, lots of support and can take you from concept to PCB Gerber files. There are many others available for free, some are limited demos. You will not learn this in an evening or so but as you do it gets easier each time.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.