• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Electronic Door Lock project

Status
Not open for further replies.

Soapchips

New Member
Hello all,

I hope you can help me. I am using a PIC16F627 and a simple spst switch on 4 inputs RB0-RB3 to activate a relay to open one of two actuators (pin 11=open, PIN 12 = closed). My code is not right and i am having probs with the code. I am a novice C programmer and just cant get it.

Here is the code i have so far. Please take a look and see if you have any ideas to make it simpler/better. I do not want you to do my project, just show me the door please.

********************************************code begin

Program
#include <pic.h>
#include <delay.h>
#include <delay.c>
#define LED RB4
#define RELAY1 RB5
#define RELAY2 RB6
#define led1 RA1
#define led2 RA0
#define led3 RA7
#define led4 RA6

int count1, sum;
main()
{
TRISB = 0x0F; //RB0-RB3 inputs
TRISA = 0x00;
for(;;)
{led1=0; led2=0; led3=0; led4=0;
sum=0; RELAY1=0; RELAY2=0;
for(count1 = 0; count1 < 5; count1++) //no. 1 input
{DelayMs(250);
if(RB0 == 1 && RB1 == 0 && RB2 == 0 && RB3 == 0)
{led1=1;
if(sum==0)
{sum +=1;
}
}
}
for(count1 = 0; count1 < 5; count1++) //no. 2 input
{DelayMs(250);
if(RB0 == 0 && RB1 == 1 && RB2 == 0 && RB3 == 0)
{if(sum ==1)
{sum +=10;
}
}
}
for(count1 = 0; count1 < 5; count1++) //no. 3 input
{DelayMs(250);
if(RB0 == 0 && RB1 == 0 && RB2 == 1 && RB3 == 0)
{if (sum ==11)
{sum += 100;
}
}
}
for(count1 = 0; count1 < 5; count1++) //no. 4 input
{DelayMs(250);
if(RB0 == 0 && RB1 == 0 && RB2 == 0 && RB3 == 1)
{if (sum == 111)
{sum += 1000;
}
}
}
if (sum == 1111)
{ if(LED == 1)
{RELAY1 = 0; //to unlock
RELAY2 = 1;
{for(count1 = 0; count1 < 5; count1++)
{DelayMs(250);
}
}
LED = 0;
RELAY1 = 0; RELAY2 = 0;
break;
}
else
{ RELAY1 = 1; //to lock
{for(count1 = 0; count1 < 5; count1++)
{DelayMs(250);
}
}
RELAY2 = 0;
LED = 1;
RELAY1 = 0; RELAY2 = 0;
break;
}
}
break;
}
}
*****************************code end

I am using a simple else/if with braeks. Is this secure? Is it too much?

Please help, i grad in oct and i am not a programmer, i am an electronice tech!

Mahalo!!!
 

tdb

New Member
If you could explain the project and problem in detail a little more, I might be able to help a bit. Is everything working already, and you are just looking for better code? Or, does it even compile? If not, what compiler are you using?

From the code, it looks like you are detecting all 4 switches. When all 4 are ON, you turn on the relay for 1 second. If ANY of the 4 switches are OFF, you turn the relay off. Is this accurate? If so, I would just read all four switches at one time (this way, you eliminate 3 whole seconds!). Here's some sample code (I'm just using this text editor, so it may not compile correctly):

//include your pre-processor statements here as well

#byte PortB = 0x06

Main()
{

while(1)
{
TRISA = 0x00 //set all 8 bits to outputs
TRISB = 0x0F //set b0-b3 to inputs

int count1
byte InputB

// read all inputs w/ 1 second debounce
for(count1=0;count1<5;count1++)
{
Delay_ms(250)
inputB = PortB //read the entire port (you're compiler may do this
//differently
inputB = 0xF0 && InputB //mask first 4 bits of InputB
}

switch(inputB)
{
case 0xFF:
//all inputs are ON
//enter ON condition code here
break;

default:
//one or more inputs were OFF
//enter OFF condition code here
break;
}
}
}
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top