Subject:
|
newbie questions...
|
Newsgroups:
|
lugnet.robotics.rcx.nqc
|
Date:
|
Wed, 6 Sep 2000 06:59:22 GMT
|
Reply-To:
|
STELIAN.POP@ALCOVE.stopspamFR
|
Viewed:
|
1997 times
|
| |
| |
Hi everybody,
I recently acquired a RIS 1.5 box and started playing with nqc
(great program btw)...
Still, I have some questions...
Let's take a simple example: I have 1 touch sensor and I want
to do a SOUND_UP when the touch is pressed and a SOUND_DOWN when
the touch is released. Rather simple...
First version:
----------------------------------------
#define TOUCH SENSOR_1
task main()
{
int touch;
SetSensor(TOUCH, SENSOR_TOUCH);
touch = TOUCH;
while (true) {
int newtouch = TOUCH;
if (touch != newtouch) {
if (newtouch == 1)
// on press...
PlaySound(SOUND_UP);
else
// on release
PlaySound(SOUND_DOWN);
touch = newtouch;
}
}
}
----------------------------------------
Second version:
----------------------------------------
#define TOUCH SENSOR_1
task main()
{
SetSensor(TOUCH, SENSOR_TOUCH);
while (true) {
if (TOUCH == 0) {
while (TOUCH == 0) ;
// on press
PlaySound(SOUND_UP);
}
else {
while (TOUCH == 1) ;
// on release
PlaySound(SOUND_DOWN);
}
}
}
----------------------------------------
Are the two version equivalent ? Is it better to use the first
or the second, or does it exist a better way to deal with
touch sensors being pressed/released ?
I tried to do the same thing in RCX2, but I'm not sure to
fully understand how we get the exact event that triggered
the catch clause when monitoring several events...
Anyway, is this correct (and the Good(tm) way to do it):
----------------------------------------
#define TOUCH SENSOR_1
task main()
{
SetSensor(TOUCH, SENSOR_TOUCH);
SetEvent(0, TOUCH, EVENT_TYPE_PRESSED);
SetEvent(1, TOUCH, EVENT_TYPE_RELEASED);
while (true) {
monitor( EVENT_MASK(0) | EVENT_MASK(1) ) {
Wait(10000);
}
catch {
// **** IS THIS CORRECT ? WHY 0 ?
int x = ActiveEvents(0);
if (x & EVENT_MASK(0)) {
// on press
PlaySound(SOUND_UP);
}
else if (x & EVENT_MASK(1)) {
// on release
PlaySound(SOUND_DOWN);
}
}
}
}
Thanks to all of you for your advices.
Stelian.
--
Stelian Pop <stelian.pop@alcove.fr>
|
|
Message has 1 Reply: | | Re: newbie questions...
|
| (...) Both versions should work ok. Personally I prefer something like the following code because its less code overall and the "modality" of the program is apparent from the listing. But its really a matter of personal style. #define TOUCH SENSOR_1 (...) (24 years ago, 9-Sep-00, to lugnet.robotics.rcx.nqc)
|
5 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|