To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.robotics.rcx.nqcOpen lugnet.robotics.rcx.nqc in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / RCX / NQC / 754
753  |  755
Subject: 
Re: newbie questions...
Newsgroups: 
lugnet.robotics.rcx.nqc
Date: 
Sat, 9 Sep 2000 05:30:24 GMT
Viewed: 
4506 times
  
In article <slrn8rbqq8.k62.stelian@ontario.alcove-int>,
stelian.pop@alcove.fr wrote:

> 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 ?


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

task main()
{
  SetSensor(TOUCH, SENSOR_TOUCH);
  while(true)
  {
    until(TOUCH);
    PlaySound(SOUND_UP);
    until(!TOUCH);
    PlaySound(SOUND_DOWN);
  }
}

> 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);
>                                 }
>  }
>  }
>  }


The above looks good, but I haven't tried it so there could be a subtle
problem.  Using ActiveEvents() is pretty awkward at present because you
need to know the task number of the running task (task main is 0).  With
the latest firmware (firm0328.lgo) you can use ActiveEvents(10) to
always get active events for the current task.  I'll probably add more
sophisticated catch code to NQC in the future that takes advantage of
ActiveEvents(10).

You may want to replace "Wait(10000);" with "until(false);"  (or if you
don't like "until", you could do "while(true);".

Dave

--
reply to: dbaum at enteract dot com



Message has 1 Reply:
  Re: newbie questions...
 
(...) Ok, so the 'if ( ActiveEvents(10) & EVENT_MASK(0) )' is the correct syntax to check if event 0 was triggered ? (...) Or a function that returns the current task number ? Thanks for your advices and keep up the good work ! Stelian. (26 years ago, 11-Sep-00, to lugnet.robotics.rcx.nqc)

Message is in Reply To:
  newbie questions...
 
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 (...) (26 years ago, 6-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

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR