The first thing we had to do was define our variables. We put them in as global.
The code has pretty good coments which would make it easier to follow. We then wanted to achieve a time period in between the states so that way if a person never successfully enters into the place than another person will not trip the counter as having two people going in. So the person now has about five seconds to get into the place, but if a person does not than the timer will reset the states and the timer.
The ideal logic diagram.
Better counting code logic
int count_out=0; // this is our count of people going out
int total; // keeps track of the total people walking in and out
int count_in=0; // this is our count of people going in
int k=0; // keeping track of states going in
int j=0; // keeping track of states going out
int total_in=0; // this is the total amount of people in the place
int Jcount=0; // keep track of out time count
int Kcount=0; // keep track of in time count
int limit=65535; // max limit for timer
int jcnt=0; // keep track of time for interval between out trips
int kcnt=0; // keep track of time for interval between in trips
int cntLimit=5; // keep track of amount of time its allowed to go through the
This is our actual counting code. This code was put into the for loop that normally is inserted into the program during the start up of a new FreeScale code warrior project file.
for(;;){
if(kcnt==cntLimit){ // if time takes to long going in the place, will reset states and counting time
k=0; // state count of the entrance goes to 0
Kcount=0; //in time count reset
kcnt=0; //between the trips time reset
}
if(jcnt==cntLimit) {// if time takes to long going out the place, will reset states and counting time
j=0; // state count of the exit goes to 0
Jcount=0; //out time count reset
jcnt=0; //between the trips time reset
}
if(k==0&&PTAD_PTAD3==1){ // did person go into the first trip
k=1; // in state 1 was entered
} else if(k==1&&PTAD_PTAD5==1){ // did person go into the second trip
k=2; // in state 2 was entered
} else if(k==2&&PTAD_PTAD7==1){ // did the person go in all the way
k=0; //person in reset trip in
count_in+=1; // add to the number of people who are in the place
}
if(j==0&&PTAD_PTAD7==1){ // is the person starting to go out
j=1; // out state 1 was exited
} else if(j==1&&PTAD_PTAD5==1){ // the person is almost out
j=2; // out state 2 was exited
} else if(j==2&&PTAD_PTAD3==1){ // the person had left
j=0; //person out reset
count_out+=1; // add to the number of people who had left
}
total = count_in - count_out; // now how many people are in the place
if((total)<0){ // we do not want negative numbers or do we (people sneeking in our stuff should stop that though)
total_in=0; // reset to 0 if there is a negative number
} else{total_in=total;} // what is our actual total of people in the place
if(Jcount==limit){ // does the out time count match limit
jcnt++; // keep timing
Jcount=0; // reset out time count
} else if(j!=0) { // at an out state
Jcount++; // keep timing
}
if(Kcount==limit){ // does the in time count match limit
kcnt++; // keep timing
Kcount=0; // reset in time count
}else if(k!=0){ // at an in state
Kcount++; // keep timing
}
__RESET_WATCHDOG();
}