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!!!
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!!!