Subject:
|
Re: How2 'count' in NQC
|
Newsgroups:
|
lugnet.robotics
|
Date:
|
Fri, 3 Nov 2000 20:31:24 GMT
|
Original-From:
|
John Barnes <barnes@!stopspam!sensors.com>
|
Viewed:
|
655 times
|
| |
| |
I have noticed this ongoing thread about the difficulties of debouncing
and counting switch closures, and I must say that it is a very real
problem anywhere electromechanical switches are interfaced to
electronics which is orders of magnitude quicker.
Within the RCX, however, there is an inherent option whcih can makes this
easier than in some other microcontroller environments - it is multitasking.
So you can create a switch monitor loop based on;
while (true)
{
while (switch==0) ; //stick here til its pressed
count+=1
Wait(5); //wait for any bouncing of input
while (switch==1) ; //stick here as long is it stays pressed
}
This simply loops forever, incrementing the value in count each time the switch
is pressed. The wait delay is the debounce time. Having started this task, it
is easy to use it from another one;
count=0;
while (count<10) ;
This will wait for the next 10 counts.
It is not clear from the comments exactly what the application is, but this
approach might help solve whatever the problem is.
JB
> Steve, this is the third time you have helped me, and this time you
> obviously spent a lot of time working out the alternatives. First of all,
> the Wait (DELAY); in my inept code was not to flash every half second. It
> was to prevent additional counts to be registered during that half second.
> It does that. What bothers me is that I think there must be a way to
> de-bounce a switch (1 count per actuation) without 15 or 20 statements. I
> have found 'clues' throughout Dave Baum's Definitive Guide. For example, on
> pages 126 and 127 he uses a counter to add 'bumps' and get his bugbot out of
> a corner when the count exceeds a limit. I know his code works, but I'm not
> sure why. It took real research on my part to discover that the counter
> ...keeps counting... as long as the switch is held down (perhaps at
> clock-speed - delays).
>
> Thanks again, and I will be going through your *reply* with a fine-tooth comb!
>
> Jerry
>
>
> > In your code:
> >
> > > #define BUMPER SENSOR_2
> > > #define LAMP OUT_B
> > > #define FLASH_TIME 10
> > > #define DELAY 50
> > >
> > > int counter;
> > >
> > > void check_switch()
> > > {
> > > if (BUMPER == 1)
> > > {
> > > counter += 1;
> > > Wait (DELAY); //to register 1 count only
> > > }
> > > }
> >
> > You are actually counting how long the switch is held down - in units of about
> > a half second. So if you press and hold the switch for ten seconds, you'll get
> > a count of about 20 in 'counter.
|
|
Message has 1 Reply: | | Re: How2 'count' in NQC
|
| I want to thank everyone who helped with the double problem of switch de-bouncing and that loops can often count continuously while the switch is in actuation. There were good suggestions about removing delays from the code and 'counting' upon (...) (24 years ago, 4-Nov-00, to lugnet.robotics)
|
Message is in Reply To:
| | Re: How2 'count' in NQC
|
| (...) The sensor returns 1 for as long as it's held down. In your code: (...) You are actually counting how long the switch is held down - in units of about a half second. So if you press and hold the switch for ten seconds, you'll get a count of (...) (24 years ago, 3-Nov-00, to lugnet.robotics)
|
6 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
This Message and its Replies on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
Active threads in Robotics
|
|
|
|