View previous topic :: View next topic |
Author |
Message |
fsven
Joined: 18 Sep 2008 Posts: 3
|
Prevent sensor from continuously sense |
Posted: Thu Feb 26, 2009 8:37 pm |
|
|
I'm developing a sensor system that will detect the position of a human. Below is my program:
Code: |
#include <16f877a.h>
#use delay (clock = 40000000)
#fuses hs,noprotect,nowdt,nolvp
#BYTE PORTA=5
#BYTE PORTB=6
#BYTE PORTC=7
#include <flex_lcd.c>
#include <kbd.c>
void main()
{
char k;
set_tris_a (0x0f);
set_tris_b (0);
set_tris_c (1);
lcd_init();
kbd_init();
lcd_putc("\fReady...\n");
delay_ms(1000);
do
{
if(portc == 0b00000001)
{
lcd_putc("Room 1");
delay_ms(100);
}
else if(portc == 0b00000011)
{
lcd_putc("Kitchen");
delay_ms(100);
}
}while(true);
}
|
The problem is when sensor continuously operates, the LCD will repeat the display, e.g. kitchen. How to make the sensor detect once even if the sensor still continuously on?
TQ |
|
|
arunb
Joined: 08 Sep 2003 Posts: 492 Location: India
|
re |
Posted: Thu Feb 26, 2009 10:43 pm |
|
|
you could compare the current position with the previous position, if there is change in the position you could display it, otherwise just ignore.
when the program starts first, the previous position value is zero, read the position once and display it. now read and compare with this position, display the position, only when a change is detected.
hope this was helpful
thanks
aa |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Fri Feb 27, 2009 3:05 am |
|
|
use a copy of portc and only check it if it has changed
I would also use a switch statement especially if this will be extended, just looks nicer.
Code: |
int oldportc = portc;
int newportc = oldportc;
do
{
newportc = portc;
if (newportc != oldportc ) // Only do something if it has changed
{
oldportc = newportc;
switch(newportc)
{
case 0b00000001:
lcd_putc("Room 1");
break;
case 0b00000001:
lcd_putc("Kitchen");
break;
}
// delay_ms(100); // With the current code you may as well put this here, Don't think it is needed anyway!
}
}while(true);
|
The reason I use newport=portc is because everytime you read the port it potentially could have changed so it is best to only read it once. I don't see it being a problem with your code though so you could just leave it the way it is.
If the code changes you may want to move the delay back.
Not sure why the delay is in there anyway, should not be required unless the port changes faster tham 100ms and you need the delay! |
|
|
f_sven Guest
|
|
Posted: Fri Feb 27, 2009 4:36 am |
|
|
Code: |
int oldportc = portc;
int newportc = oldportc;
do
{
newportc = portc;
if (newportc != oldportc ) // Only do something if it has changed
{
oldportc = newportc;
switch(newportc)
{
case 0b00000001:
lcd_putc("Room 1");
break;
case 0b00000010:
lcd_putc("Kitchen");
break;
}
// delay_ms(100); // With the current code you may as well put this here, Don't think it is needed anyway!
}
}while(true);
|
Thanks for reply. I try running this code but lots of error appear. Actually the basic idea is to read the sensor pulse in short time. Means the pic read the pulse once even the sensor have long pulse. |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Fri Feb 27, 2009 5:55 am |
|
|
A few mods to make it work
Code: |
#include <16f877a.h>
#use delay (clock = 40000000)
#fuses hs,noprotect,nowdt,nolvp
#BYTE PORTA=5
#BYTE PORTB=6
#BYTE PORTC=7
//#include <flex_lcd.c>
#include <kbd.c>
void main()
{
char k;
int oldportc;
int newportc;
set_tris_a (0x0f);
set_tris_b (0);
set_tris_c (1);
//lcd_init();
kbd_init();
//lcd_putc("\fReady...\n");
delay_ms(1000);
do
{
newportc = portc;
if (newportc != oldportc ) // Only do something if it has changed
{
oldportc = newportc;
switch(newportc)
{
case 0b00000001:
// lcd_putc("Room 1");
break;
case 0b00000011:
// lcd_putc("Kitchen");
break;
}
// delay_ms(100); // With the current code you may as well put this here, Don't think it is needed anyway!
}
}while(true);
}
|
I commented all the lcd routines out because I could not find flex_lcd.c, is this one of your files or a CCS driver ?
I did not look to extensively |
|
|
Guest
|
|
Posted: Fri Feb 27, 2009 6:22 am |
|
|
Thanks..its works!! |
|
|
|