Page 1 of 1

Multiple touch sensors

Posted: 30 Mar 2012, 06:36
by h-g-t
I have a task which sets a variable whenever a touch sensor (the focus button on a radio-controlled camera remote) is pressed.

The main task routinely checks this variable then pauses until the same sensor is pressed again. It then resets the variable and carries on.

The problem is that I want to add another sensor (the shutter button on the same remote). This might not be pressed, but if it is, it will be very soon after the first button.

I am using 'wait' commands to avoid reading the same sensor twice.

The problem is that the second sensor might be missed because the main thread is in a wait state.

Is what I am trying to do possible, or do I have to think again?

Code: Select all

task pause_1 () // Check for touch sensor in port 1 & set touch_1 variable
{
  touch_1 = false ;              // Make sure value initialised
  
  while (1 == 1)                 // Endless loop
   {
    until (SENSOR_1 == 1);       // Wait until touch sensor in Port 1 activated
     touch_1 = true ;            // Record sensor activated
     PlayTone (1000,1000);       // Audible confirmation
     Wait (1000) ;
     ClearSensor(IN_1);          // reset sensor
     
    until (touch_1 == false) ;   // Check to see if reset by task main

   }    // End of While loop
   
} // task pause_1 ended =========================================================

Code: Select all

task main ()

{

  SetSensorTouch(S1);   // Initialise touch sensor to be used in Pause_1 task
  
..............
..............

   StartTask (pause_1) ;    // Start checking touch sensor in port 1

..............
..............

   while (shot_count < n_rows)            // Loop for all shots in column
   {

    if (touch_1 == true )                 // Check to see if should pause
    {
     ClearScreen ();
     TextOut (1,LCD_LINE1,"PAUSED");
     TextOut (3,LCD_LINE1,"PAUSED");
     TextOut (5,LCD_LINE1,"PAUSED");
     Wait (1000);
     until (SENSOR_1 == 1); // Wait until touch sensor in Port 1 re-activated
     PlayTone (3000,1000);                // Audible confirmation
     Wait (1000);
     ClearScreen ();
     touch_1 = false;                     // Reset 'sensor activated' variable
     ClearSensor(IN_1);                   // Reset touch sensor
    }
    
..............
..............

Re: Multiple touch sensors

Posted: 30 Mar 2012, 22:25
by mattallen37
So, you want it to make you hold button 1 for 1 second. You also need to know if button 2 was pressed during that time?

Do you want it to wait for button 1 to be pressed? If so, do you want a timeout? What if button 1 was pressed for only 1/2 of a second, and button 2 was pressed during that time?

Or are you saying you want a complete bump cycle (pressed then released), and then a second or more later another bump cycle that could also press button 2?

Re: Multiple touch sensors

Posted: 30 Mar 2012, 22:40
by mattallen37
You want something like this?

Code: Select all

bool Buttons(unsigned long & Time = 0){
  bool btn2_pressed = false;
  while(!btn1)Yield();       // until it's pressed
  while(btn1)Yield();        // until it's released
  unsigned long OriginalTick = CurrentTick();
  while(!btn1){              // until it's pressed
    if(btn2)btn2_pressed = true;
    Yield();
  }
  Time = CurrentTick() - OriginalTick;
  while(!btn1){              // until it's released
    if(btn2)btn2_pressed = true;
    Yield();
  }
  return btn2_pressed;
}
That waits for button 1 to get pressed and released, records the time, waits for button 1 to get pressed, subtracts the last time from the current time (to determine the length of time between release the first time, and press the second time), waits for button 1 to get released.

During the wait for button 1 to get pressed and released the second time, it checks for button 2 getting pressed.

The function returns true if button 2 was pressed, or false if it wasn't. You can also provide a time variable that will be written to the length of time between the first release and the second press.

I didn't test to be sure it works, but I think it is logical.

Re: Multiple touch sensors

Posted: 31 Mar 2012, 00:06
by h-g-t
Thanks Matt, but recording the actual delay is not necessary.

I decided to use a radio remote as a touch sensor so that I could interrupt the NXT from a distance.

Then it occurred to me that I could use the second switch on the remote to simulate a second touch sensor to carry out another action.

The difficulty is that the operating mechanism is sequential. No way round that without dismantling the transmitter and adding two separate switches in parallel. Possible, but awkward and a risk of ruining the transmitter if I get it wrong.

No problem if I just want a pause, one half-press should stop the action and a second restart it using the code I posted above.

But say I want to carry out the next action, either by just giving a single full press which activates both sensor ports almost simultaneously or by holding the first half-press then converting that into a full-press a few seconds later.

How can I code it so the NXT is guaranteed to always pick up the second press whether it happens milliseconds or minutes later?

To put it into a wider context, if you have several touch sensors attached, can you be sure the NXT will pick up every press, irrespective of what other touch sensors are are doing?

If the answer is 'no', then I will pursue a mechanical solution by adding switches.

Re: Multiple touch sensors

Posted: 31 Mar 2012, 03:24
by mattallen37
So you essentially have a switch with 3 positions; off, on 1, and on 1&2 ? The NXT could only ever see those possible combinations? You want a program to be able to sense off, on 1, and on 2? If 2 is pressed, you don't what it to count as 1 is pressed?



What about the scenario of you pressing the button fully, from not at all? You want to avoid sensing on 1, because it's only for a few ms (maybe up to 25) before on 2 (even though 1 is still technically on)?

And on the flip side, if you have it pressed half way (on 1), and then 500ms later you press it all the way, you want it to have registered the on 1, and then now the on 2?

Re: Multiple touch sensors

Posted: 31 Mar 2012, 03:52
by mattallen37
If what I said in my last post is what you want, try this:

Code: Select all

#define PressTime 1000

bool btn1, btn2;

byte btnState;
unsigned long LastTick;

task Buttons(){
  while(true){
    if(btn1){
      LastTick = CurrentTick();
      while(!btn2 && btn1){
        if(CurrentTick() > LastTick + PressTime){
          btnState = 1;
          goto NXC_NO_2;
        }
        Yield();
      }
      btnState = 2;
NXC_NO_2:
    }
    else{
      btnState = 0;
    }
    Yield();
  }
}

task main(){
  SetSensorTouch(S1);
  SetSensorTouch(S2);
  start Buttons;
  while(true){
    btn1 = Sensor(S1);
    btn2 = Sensor(S2);
    NumOut(0, LCD_LINE1, btn1);
    NumOut(75, LCD_LINE1, btn2);
    NumOut(37, LCD_LINE1, btnState);
    Yield();
  }
}
I used touch sensors on ports 1 and 2 to simulate the switches you are dealing with, and I set PressTime to 1000 (1 second) for the sake of proving it works, but you will probably want to change it to more like 25-50.

Re: Multiple touch sensors

Posted: 31 Mar 2012, 04:06
by mattallen37
Actually use this instead:

Code: Select all

#define PressTime 1000

bool btn1, btn2;

byte btnState;
unsigned long LastTick;

task Buttons(){
  while(true){
    if(btn1){
      if(btn2){
        btnState = 2;
      }
      else{
        LastTick = CurrentTick();
        while(!btn2 && btn1){
          if(CurrentTick() > LastTick + PressTime){
            btnState = 1;
          }
          Yield();
        }
      }
    }
    else{
      btnState = 0;
    }
    Yield();
  }
}

task main(){
  SetSensorTouch(S1);
  SetSensorTouch(S2);
  start Buttons;
  while(true){
    btn1 = Sensor(S1);
    btn2 = Sensor(S2);
    NumOut(0, LCD_LINE1, btn1);
    NumOut(75, LCD_LINE1, btn2);
    NumOut(37, LCD_LINE1, btnState);
    Yield();
  }
}
Should be the same logically, but it's better. For some reason I wasn't thinking clearly when I posted that last program, so I used a label and a goto when I should have used an if/else.

Re: Multiple touch sensors

Posted: 31 Mar 2012, 04:42
by h-g-t
Thanks Matt, I'll see how that works when I have the receiver - NXT connections fabricated.