 |
 |
View previous topic :: View next topic |
Author |
Message |
hobby_85
Joined: 17 Aug 2009 Posts: 50
|
|
Posted: Mon Oct 19, 2009 3:12 am |
|
|
Hey bungee, yeah I've tried changing that. But I found something that might help. Each of the two values has a count difference of about 2650 counts. Regardless of how far away I stand.
I think I might have to add something to the timer, I'm not sure. Still experimenting. Would you know anything about that?
thanks |
|
 |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Oct 19, 2009 4:49 am |
|
|
I don't think it will solve your problem, but variable 'tripped' is not initialized at program start up. Best is to move the resetting of tripped, tripped1 and counter from the end of the program to the beginning.
Another small improvement is to call clear_interrupt() before enabling the timer and ccp1 interrupts.
Code: | for (ctr=0;ctr<64000;ctr++) { //Wait for PINC1 to go high until timeout
if(input(PIN_C1)) {
set_timer1(0); //set timer value to 0, start counting
enable_interrupts(INT_TIMER1); //feature to know if timer1 overflows
ctr=1; //PINC1 went high, set ctr=0, break
break;
}
}
if (ctr==1){ //at this point, C1 went high, start timer and wait for CCP/C5 to go high
| You are using the ctr variable both as a counter and as a flag. This is bad programming practice as it makes for more difficult to understand code.
Try to use meaningful variable names. What is the difference between 'trigger' and 'trigger1'? Examples of better names are 'object_detected', 'out_of_range' or whatever other purpose the trigger is really meant to say. |
|
 |
Guest
|
|
Posted: Mon Oct 19, 2009 6:21 am |
|
|
ckielstra wrote: | I don't think it will solve your problem, but variable 'tripped' is not initialized at program start up. Best is to move the resetting of tripped, tripped1 and counter from the end of the program to the beginning.
Another small improvement is to call clear_interrupt() before enabling the timer and ccp1 interrupts.
Code: | for (ctr=0;ctr<64000;ctr++) { //Wait for PINC1 to go high until timeout
if(input(PIN_C1)) {
set_timer1(0); //set timer value to 0, start counting
enable_interrupts(INT_TIMER1); //feature to know if timer1 overflows
ctr=1; //PINC1 went high, set ctr=0, break
break;
}
}
if (ctr==1){ //at this point, C1 went high, start timer and wait for CCP/C5 to go high
| You are using the ctr variable both as a counter and as a flag. This is bad programming practice as it makes for more difficult to understand code.
Try to use meaningful variable names. What is the difference between 'trigger' and 'trigger1'? Examples of better names are 'object_detected', 'out_of_range' or whatever other purpose the trigger is really meant to say.
|
Thanks for that, I'll sort out the code and see what happens. It seems like the code is working to an extent. Notice the following:
If transmitter is 1 m away, I consistently get
1) 116514 equaling to 324.73meters OR
2) 119190 equaling to 332.20 meters
If transmitter is 2m away, I consistently get
1)116891 , 325.79 meters or
2)119553, 333.22 meters
Notice that the receiver is picking up the 1 meter difference in distance very accurately. However, its giving me two different results. If there was just one, I could add an adjustment to the code.
Thanks for the help, I'll work on improving the code. If you could think of any reasons why I keep getting 2 different reasons for each one, that would be great.
Thanks again. |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|