PSoC and key fob

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hi guys,

   

Ive been playing around with my 2 button key fob and receiver, and i thought wouldnt it be great to interface it to my Psoc 1.

   

conected to my psoc are 2 ir beam breakers i made, they currently work with a simple bit of code. I was wanting to make a  simple alarm. it shoud be like this. system on> wait till armed (button 1 of key fob) > armed> do alarm stuff (already done). button 2 of the fob should disarm the alarm whether the alarm was activated or just to disable it even if it wasnt activated.

   

so do i need to declare something like:

   

buttonarm==0;

   

buttondisarm==0;

   

while buttonarm is not pressed, dont do anything, if it is arm alarm activated

   

{

   

do alarm stuff, have this done to monitor ir beam breakers

   

if the alarm goes off for any reason and buttondisarm is pressed, disable alarm output

   

}

   

im 100% sure on how to implement the keyfob

   

any help is always greatly appreciated.

   

thanks

0 Likes
44 Replies
Anonymous
Not applicable

Hi, Ive been goign through some old college projects we did and ive come across this code which used capsense, PWM, LCD etc. I cut out the LCD, capasense etc below but could i not modify the code to do something like below?

   

void main (void) {

   

int AlarmArmed=FALSE;
 int SystemOn=TRUE;
 int AlarmActive=FALSE;

   

while(1) {

   

if (AlarmActive==FALSE) {

   

if(SystemOn==TRUE) {

   

SystemOn=FALSE;

   

AlarmArmed=FALSE;

   

 }

   

Else if (SystemOn==FALSE) {

   

SystemOn=True }

   

}

   

if ((SystemOn==TRUE)&&(AlarmActive==FALSE))

   

if (AlarmActivated==FALSE)

   

{

   

AlarmArmed=TRUE;

   

}

   

else if (AlarmArmed==TRUE) {

   

AlarmArmed=FALSE;

   

}

   

}

   

if ((SystemOn==TRUE) && (AlarmActivated==TRUE))

   

{

   

AlarmActive=TRUE;

   

}

0 Likes
Anonymous
Not applicable

1. I would add a key debounce function, and name two variables armKey and disarmKey which were debounced inputs.

   

2. I would add two variable called systemState (ARMED or DISARMED) and alarmState (ALARM_ON or ALARM_NOT_ON)

   

You can try to use these to develope your program.

0 Likes
Anonymous
Not applicable

 OK, I am too slow to reponse. 😞

   

Shall check your program now.

0 Likes
Anonymous
Not applicable
1. For others to understand your code, it would be better tto state what are those variables, which are the key inputs? int AlarmArmed=FALSE; /* Key inpput? */ int SystemOn=TRUE; /* TRUE if ....? */  int AlarmActive=FALSE; /* IR broken? */ 2. what is AlarmActivated? 
   

     

0 Likes
Anonymous
Not applicable

HI,

   

Yes, you are quite right, i basically just opened the project and typed it into the reply page here. Sorry for the misunderstanding,

   

ok I see that code i sent in, is just adding to the confusion,

   

yeah basically i think it would probably be best to work from the requirements in my first post, and ignore the code sent in.

0 Likes
Anonymous
Not applicable

WE NEED AN EDIT FUNCTION !!!!!!

   

Your program is close to what i suggested. It just need to put comments in so otherS can see what you mean to do.

   

Once you add those comments in , people can see if there are errors or not.

   

But I definely would add the debounce function for the keys.

   

And do you need some kinds of debounce for your IR beam sensor?

0 Likes
Anonymous
Not applicable

hiya Lleung,

   

If it helps i could give you the project files for that code im quoting from.

   

im a little glad that my digging turned somethin up lol.

   

for the moment the IR beam breakers im using are just there as they are neat, i could put anything there, switch etc to provide a high or low signal. debounce might not be neededfor the moment.

   

thanks for replying

0 Likes
Anonymous
Not applicable

Sure,

   

You can upload your modified project here, doesn't need to be 100% correct.

   

Put your comments there first, people can make suggestion for errors/improvement.

   

Happy coding.

0 Likes
Anonymous
Not applicable

the code i was thinking of uploading has all the capsense, lcd stuff in it and is a good bit more complicated that what im trying to do. all i want to do  is have the code run when i press button a of the fob and stop on button b,

   

the code i have simply checks inputs, using our friend the shadow register and writes and output to port 1.0 which is an led, indicating an output or alarmed state. 

0 Likes
Anonymous
Not applicable

what if i tried to do it this way, could it work?

   

i have a fob input connected to port 1.7, the one i want to disable the alarm with

   

void main(void)

   

while ( PRT1 != 0x80)  // checks fob button isnt pressed, if it is dont run rest of code? effectively not arming device?

   

{

   

code for alarm inputs, simply checks to see if certain ports are high, if not sound alarm

   

}

   

 

   

could something like this be implemented?

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

An alarm system is a pretty good example for a state-machine and is often used as an example to introduce the "how to"s.

   

From your former posts I can see that you intentionally are approaching to such a design, so I suggest you to complete it:

   

Use a variable for the current state and have an enum for all possible states + an unknown state.

   

Define for each state under which circumstances (one or more signals) another state is reached.

   

Program a loop which

   

1st. Gathers all input signals and sets corresponding vars (this isolation makes the system independent from sensor-types and similar differences)

   

 

   

2nd. enter a switch () block with all possible states and decide in each state which state comes next.

   

 

   

That's it

   

 

   

Happy coding

   

Bob

0 Likes
Anonymous
Not applicable

Bob's idea is good.

   

Since this is a simple project, you can start a new project without all other bits and pieces.

   

I still think you need the debounce for your key, you can use the debouncer component with the latest creator component pack.

   

Sometime it is much easier to start fresh.

0 Likes
Anonymous
Not applicable

using switch and case,

   

ive used this with visual basic and console run programs but not using a microcontroller,

   

It kind of goes, i think:

   
int count0;
int count1;
int count2;
int count3;
int count4;



switch(input) {
case 0: ++count0; break;
case 1: ++count1; break;
case 2: ++count2; break;
case 3: ++count3; break
case 4: ++count4; break; im sure this is really obvious to anyone whos done this but im a little confused about adapting it to my code

0 Likes
Anonymous
Not applicable

here is the hint:

   

switch (systemState)

   

{

   

            case DISARMD:

   

                    check keys;

   

                    if armKey presssed

   

                            clear all outputs;

   

                            change systemState to ARMED

   

                    break; 

   

           case ARMED:

   

          /* FILLL in what it should be done here */

   

                   break;

   

         default:

   

                   /* FILLL in what it should be done here */

   

}

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I can see a lot more stages, so try something like

   

enum AlarmStates { DISARMED, ARMED, ALERTED, ALARMING, CLEARED, UNKNOWN} SystemState;

   

SystemState = DISARMED;

   

while(forever)

   

{

   

   GetAllSensors();

   

   switch(SystemState)

   

   {

   

      case DISARMED:

   

      case ARMED:

   

      case ALERTED:

   

and so on

   

      default:

   

   }

   

}

   

 

   

Happy coding

   

Bob

   

 

   

Btw here is a link to one of my favorite C-manuals: http://publications.gbdirect.co.uk/c_book/ 

0 Likes
Anonymous
Not applicable

Hi guys,

   

Thanks for your replies,

   

So basically if i have what Lleung and Bob said then it'd be something like:

   

enum AlarmStates { DISARMED, ARMED, ALERTED, ALARMING, CLEARED, UNKNOWN} SystemState;

   

SystemState = DISARMED;

   

while(forever)

   

{

   

   GetAllSensors();

   

   switch(SystemState)

   

   {

   

case DISARMED:

   

Check Keys /// would that be something like, check if  key input  port pin is high; if (PRT1DR & 0x01) for example port 1.0 with fob input. if so,clear alarm output, which for example is port 1.1,

   

PRT1DR &= ~0X02; //make alarm output low

   

SystemState= ARMED

   

break;

   

With regards to inputting and outputting data, how is SystemState, Armed, disarmed related to actual inputs from the microcontroller?

   

thanks

   

BTW.. thanks for the C manual link Bob, ive added it to bookmarks

0 Likes
Anonymous
Not applicable

any sample code using switch and case or state machines for PSoC 1?

0 Likes
kemic_264446
Level 4
Level 4
First like received

 Switch and Case are very powerful.

   

They work like this...

   

 

   

You have ONE and only ONE switch statement.

   

The Switch statement examines the value of a variable and executes a set of commands dependent on theat value.

   

Like this:

   

 

   
int state;
   
switch(state)
   
{
   

   
case 0:
   
// code here is executed if the value == 0
   
break;
   
    
case 1:
    
// code here is executed if the value == 0
    
break;
   
   
    
case 2:
    
// code here is executed if the value == 0
    
break;
   
   
default:
   
// code here is executed if the value doesn't match any of the above.
   

   
}
   

 

   

So assign the 'state' of the program to variable.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

For state-machines and C-examples mrs Google and mr. Wikipedia are a cheerful source of information.

   

In one of my first manuals for programming (by Ashton-Tate) was to read:

   

The very best thing to do when starting to write a program is to switch off the computer.

   

When reading your program thoughts it comes to my mind that you are not (yet) quite clear of what you want to do where.

   

I tried to tell you to make a list of events for each state that will make the state to leave to another state. This actually IS the definition of a state-machine. The events are your input-signals whichs states you should collect outsides the case-block and save in some variables.

   

Setting a signal by directly accessing the PRTxDR is NOT recommended because this might lead to a problem (make a keyword-search for "read-modify-write" or "Shadow-Register" to get more infos). I suggest you to use instead some LED-Components withtheir associated APIs.

   

 

   

Bob

0 Likes
kemic_264446
Level 4
Level 4
First like received

 ... continued...

   

The switch / case can also do complex thing like this...

   

 

   
case 1:
   
case 2:
   
// statements A
   
break;
   
case 3:
   
// statements B
   
break;
   

where 'statements A' will be executed whether the variable is 1 or 2

   

or like this :

   
case 1:
   
// statments 1
   
break;
   
case 2:
   
// statements 2
   
case 3:
   
// statements 2
   
break;
   

Where we have left out the 'break' after case 2, so the code can jump in at case 2 and fall through to case 3, or you can jump straight in at 3 depending on the value of the variable.

0 Likes
kemic_264446
Level 4
Level 4
First like received

 Leading to something like this fot the basic structure of your app:

   

 

   

   

 

   
#define STATE_DISARMED 0
   
#define STATE_ARMED 1
   
#define STATE_ACTIVATED 2
   
 
   
int programState = STATE_DISARMED;
   
 
   
while(1)
   
{
   
 
   
switch (programState)
   
{
   
case STATE_DISARMED:
   
do_disarmed_code();
   
if (arm_button_pushed())
   
programState = STATE_ARMED;
   
break;
   
case STATE_ARMED:
   
do_armed_code();
   
if (disarm_button_pushed())
   
programState = STATE_DISARMED;
   
   
if (activation_detected())
   
programState = STATE_ACTIVATED;
   
break;
   
case STATE_ACTIVATED:
   
sound_alarm();
   
if(disarm_button_pushed())
   
programState = STATE_DISARMED;
   
break;
   
default:
   
programState = STATE_DISARMED;
   
}
   
 
   
}
   
        
0 Likes
kemic_264446
Level 4
Level 4
First like received

 The big thing to note in this is that when you detect that a button is pushed, or activation detected you do NOT call that code, you simply change the program state, so the next iteration of the infinite do(1) loop executes the code.

0 Likes
Anonymous
Not applicable

 Hi all,

   

I think our friend Cf_corp is confused with all those state machine programming. And he is having difficulty with the idea of states.

   

My suggestion to Cf_corp is do things 1 at a time

   

my suggestion is

   

1. Build a new project from scratch with one key input and one LED output, the input uses a debouncer, so the output is debounced input ( not much software here).

   

2. Use one switch and have the button pressed to turn on and press again to trun off. ( A "LED ON" state, and "LED OFF" state)

   

3. Use 2 buttons, one to turn on (ARM) and one to turn off ( DISARM).

   

After this exercise, he should be able to understand more about states. And we can contiune the rest ot the project from that on.

0 Likes
Anonymous
Not applicable

Hi SpiderKenny, Thanks for all your helpful replies.

   

As Bob pointed out I am still slighlty confused by this, does anyone know of any sample project with switch statements in it? or something i canpretty much stick into my psoc designer page and complie and run?

   

thanks guys

0 Likes
Anonymous
Not applicable

Hi lleung,

   

I am a bit cononfused still, ive done state machines with visual basic on the console doing things like selection menus etc, but ive never done them with the microcontroller.

   

I was going to go for the simple approach like you suggested.

   

so i just started a new blank project, and ive just noticed your last post regarding doing things simply. im keen to try that approach,

   

thanks for being so patient folks!

0 Likes
Anonymous
Not applicable

You are always welcome.  Happy coding:-)

0 Likes
Anonymous
Not applicable

Hi lleung,

   

so basically ive started a new project,

   

ive designated an input on port 0.0 and an output on port 1.0

   

i take it there is some declaring to do in the code itself now,

   

so we have LED_ON, LED_OFF, ARM, DISARM

   

woudld it be a matter of doing this:

   

#define LED_ON

   

#define LED_OFF 

   

#define ARM

   

#defibe DISARM

   

int state;

   

while(1) {

   

switch(state)

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Have a look at the "enum" statement in C.

   

#define LED_ON

   

will not do something at all, it just makes LED_ON defined but will not give it a value. So you might write

   

#define LED_ON 27

   

which will give it, well not really a value but a replacement text. So when the compiler sees a "LED_ON" it will replace this with "27" doing exactly what you expect.

   

To have several names #defined at once C programmers usually use the "enum" statement which will give you the advantage of keeping the definitions together and having declared variables for the purpose the enum stands for.
Have a look a bit up in this thread at my second post where I wrote an example for the enum I'm talking about.

   

You are looking for a working example? SpiderKenny's post is one! You only have to provide the functions he called which would be marked as errors because they are unknown yed as for example arm_button_pushed() or disarm_button_pushed() which are functions

   

char arm_button_pressed(void)

   

{

   

   return PRT1DR & 0x01;

   

}

   

 

   

 

   

Bob

0 Likes
Anonymous
Not applicable

hi Bob,

   

So it would be like

   

enum { arm_button_pushed(0, disarm_button_pushed(), STATE_DISARMED, STATE_ARMED, STATE_ACTIVATED, sound_alarm} programState;

   

the other piece;: char arm_button_pressed(void) {

   

return PRT1DR & 0x01;

   

}

   

Does that set up the function arm_button_pressed  as port 1 pin 0?

   

thanks

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You are mixing states and events!

   

Bob

0 Likes
Anonymous
Not applicable

How about forget about the states and events first.

   

Start the simple 1 input , 1 output without debounce first. Then added the debounce. Finish this first.

0 Likes
Anonymous
Not applicable

Hi lleung,

   

maybe the simple approach is best,  so as i said, i have a new project with just an input and an output setup,

   

this states and events has me a bit confused,

   

would i have something like, enum {alarm_armed, alarm_disarmed, alarm_activated, unknown } states;

0 Likes
Anonymous
Not applicable

 Don't worry about the state or events now.

   

Just create the project that turn on the ourput LED when the input is on. 

   

Post it here and we go to the next step. 

0 Likes
Anonymous
Not applicable

 You do not need the states and events now.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

ok, so ive attached a sample code with an input and output, Ive used the LED api as was suggested to me so many times,  basically when the input on port 0.0 is made high, the output on port 1 is activated.

0 Likes
Anonymous
Not applicable

 Now your ouput is following the input. But

   

Normally we don't use the input directly, we need to debounce the input. This can be done in hardware or software. for hardware, you can use the one shot or the pwd from. Or you can use software to debounce.

   

Here is a good reference

   

http://www.ganssle.com/debouncing.htm

   

and

   

http://www.embedded.com/electronics-blogs/break-points/4024981/My-favorite-software-debouncers

   

See if you can use one of those to debounce your input.

0 Likes
Anonymous
Not applicable

hi lleung,

   

thanks for the replies and links, will look into that. I wanted to implement a debounce. thanks

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

So I tried using the PWD API module following the datasheet inlcuded etc.

   

Ive reattached the project with PWD api, im not 100% sure on the setup or if its correctly done

0 Likes
Anonymous
Not applicable

I'll check it later today.

   

BTW, Do you have PoC3 eval kits. It would be easier to debug with psoc3 and 5.

0 Likes