Answers.Body: <p>I believe one possible answer can be found in the sample program for the color sorter in the Mindstorms control software. The technique that kit uses is to create a series of nested if/then loops:</p>
  
  <pre><code>if (Color == red) {
    //do red things here
  } else {
    if (Color == blue) {
      //do blue things here
    } else {
      if (Color == green) {
        //do green things here
      } else {
        if (Color == yellow) {
          //do yellow things here
        } else {
          //default actions here
        }
      }
    } 
  }
  </code></pre>
  
  <p>Using this as a template, you could use a structure like:</p>
  
  <pre><code>if (Touch Sensor 1 == Pressed) {
    // Special Action 1 (i.e. turn left)
  } else {
    if (Touch Sensor 2 == Pressed ) }
      // Special Action 2 (i.e. turn right)
    } else {
      // Default Action (i.e. drive forward)
    }
  }
  </code></pre>
  
  <p>Hope this helps - I don't have quick access to the Mindstorms environment right now or I could set up a quick screen shot. I can only refer you to the ColorSorter program in the samples.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2011-12-28T11:13:45.543Z
Answers.Text: Thanks, matthew.  This is the structure I've arrived at, and it seems to work, but I'm intrigued by @joubarc's idea of concurrency.  I'll have to try that out too.
Answers.UserDisplayName: AJ.
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2011-12-27T15:41:01.190Z
Answers.LastActivityDate: 2011-12-27T15:41:01.190Z
Answers.UserDisplayName: Matthew Bernhardt
Answers.Body: <p>It sounds like you will need to create an infinite loop block to constantly check which touch sensors are being pressed and if not, carry on with whatever instruction you wish. The loop block can be found under the flow category:</p>
  
  <p><img src="https://i.sstatic.net/MW9uB.png" alt="enter image description here"></p>
  
  <p>I recommend you read through the instructions (listed under Help > Contents and Index... in the LEGO MINDSTORMS NXT software). </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2011-12-25T22:08:13.293Z
Answers.LastActivityDate: 2011-12-25T22:08:13.293Z
Answers.UserDisplayName: Ambo100
Answers.Body: <p>You could also have 3 different concurrent tasks - one which is you main "go forward" program, and two others which listen for sensors and do things accordingly.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2011-12-26T09:33:31.197Z
Answers.Text: I'd illustrate, but I don't have NXT-G installed right now, and with the crappy connection I have, [downloading it](http://bricks.stackexchange.com/q/816) takes forever.
Answers.UserDisplayName: Joubarc
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2011-12-26T10:46:27.297Z
Answers.Text: NXT-G doesn't quite work that way. It runs through a set of instructions once, unless it's placed in a loop.
Answers.UserDisplayName: Ambo100
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2011-12-26T12:05:36.783Z
Answers.Text: Sorry, but you **can** have concurrent tasks by dragging additional beams from the center hub.
Answers.UserDisplayName: Joubarc
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2011-12-26T12:39:28.250Z
Answers.Text: That's true but you will also need to place it in a loop, otherwise  it will run through each block only once.
Answers.UserDisplayName: Ambo100
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2011-12-26T14:21:48.293Z
Answers.Text: Well yes, true enough — I didn't want to imply otherwise.
Answers.UserDisplayName: Joubarc
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2011-12-26T09:31:39.813Z
Answers.LastActivityDate: 2011-12-26T09:31:39.813Z
Answers.UserDisplayName: Joubarc
Answers.Body: <p>I had exactly this question. Based on <a href="https://bricks.stackexchange.com/users/906/matthew-bernhardt">Matthew</a>'s answer, I got this working. Here is a screenshot of the My Block I created to do this: TwoSwitch.</p>
  
  <p><img src="https://i.sstatic.net/hqJC6.png" alt="enter image description here"></p>
  
  <p>TwoSwitch sets one of two variables to true: <code>Pressed 1</code> or <code>Pressed 2</code>. You can then act on those values using tests. Here, I'm looping until button 1 is pressed. If button 2 is pressed, I play a sound and get another button press.</p>
  
  <p><img src="https://i.sstatic.net/uu7gd.png" alt="enter image description here"></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2012-10-14T23:52:14.037Z
Answers.LastActivityDate: 2012-10-14T23:52:14.037Z
Answers.LastEditDate: 2017-04-13T12:47:56.390Z
Answers.UserDisplayName: metadaddy
Body: <p>I am new to Mindstorms (but in my day job I am a programmer). </p>
  
  <p>I want to make my robot do one thing if the touch sensor on port 1 is pressed, and do something different if the touch sensor on port 2 is pressed.  </p>
  
  <p>If nothing is pressed, I want it to keep driving straight ahead.  </p>
  
  <p>I can see how to tell it to drive along until one of the sensors is triggered.  I.e. "drive along until sensor 1 is pressed", but I can't see how to say "... until either sensor 1 or sensor 2 is pressed". </p>
  
  <p>In answer, it would be great if you could:</p>
  
  <ul>
  <li>tell me the blocks to use</li>
  <li>give me a screenshot of a program</li>
  <li>link to a program which has a similar effect</li>
  </ul>
  
Tags: <nxt><mindstorms><programming>
Title: How to make Mindstorms respond differently, depending on which sensor is triggered?
UserDisplayName: AJ.
ThreadText: # How to make Mindstorms respond differently, depending on which sensor is triggered?
  
  (Asked by: username_0 on 2011-12-25)
  
  I am new to Mindstorms (but in my day job I am a programmer).
  
  I want to make my robot do one thing if the touch sensor on port 1 is pressed, and do something different if the touch sensor on port 2 is pressed.
  
  If nothing is pressed, I want it to keep driving straight ahead.
  
  I can see how to tell it to drive along until one of the sensors is triggered. I.e. "drive along until sensor 1 is pressed", but I can't see how to say "... until either sensor 1 or sensor 2 is pressed".
  
  In answer, it would be great if you could:
  
  * tell me the blocks to use
  * give me a screenshot of a program
  * link to a program which has a similar effect
  
  **Tags:** nxt, mindstorms, programming
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_1 on 2011-12-27. Score: 4 | Accepted answer
  
  I believe one possible answer can be found in the sample program for the color sorter in the Mindstorms control software. The technique that kit uses is to create a series of nested if/then loops:
  
  ```
  if (Color == red) {
    //do red things here
  } else {
    if (Color == blue) {
      //do blue things here
    } else {
      if (Color == green) {
        //do green things here
      } else {
        if (Color == yellow) {
          //do yellow things here
        } else {
          //default actions here
        }
      }
    } 
  }
  
  ```
  
  Using this as a template, you could use a structure like:
  
  ```
  if (Touch Sensor 1 == Pressed) {
    // Special Action 1 (i.e. turn left)
  } else {
    if (Touch Sensor 2 == Pressed ) }
      // Special Action 2 (i.e. turn right)
    } else {
      // Default Action (i.e. drive forward)
    }
  }
  
  ```
  
  Hope this helps - I don't have quick access to the Mindstorms environment right now or I could set up a quick screen shot. I can only refer you to the ColorSorter program in the samples.
  
  > **Comments:**
  > **username_0**: Thanks, username_1.  This is the structure I've arrived at, and it seems to work, but I'm intrigued by @username_3's idea of concurrency.  I'll have to try that out too.  
  
  ---
  
  ### Answer by username_2 on 2011-12-25. Score: 2
  
  It sounds like you will need to create an infinite loop block to constantly check which touch sensors are being pressed and if not, carry on with whatever instruction you wish. The loop block can be found under the flow category:
  
  ![enter image description here](https://i.sstatic.net/MW9uB.png)
  
  I recommend you read through the instructions (listed under Help > Contents and Index... in the LEGO MINDSTORMS NXT software).
  
  
  ---
  
  ### Answer by username_3 on 2011-12-26. Score: 1
  
  You could also have 3 different concurrent tasks - one which is you main "go forward" program, and two others which listen for sensors and do things accordingly.
  
  > **Comments:**
  > **username_3**: I'd illustrate, but I don't have NXT-G installed right now, and with the crappy connection I have, [downloading it](http://bricks.stackexchange.com/q/816) takes forever.  
  > **username_2**: NXT-G doesn't quite work that way. It runs through a set of instructions once, unless it's placed in a loop.  
  > **username_3**: Sorry, but you **can** have concurrent tasks by dragging additional beams from the center hub.  
  > **username_2**: That's true but you will also need to place it in a loop, otherwise  it will run through each block only once.  
  > **username_3**: Well yes, true enough — I didn't want to imply otherwise.  
  
  ---
  
  ### Answer by username_4 on 2012-10-14. Score: 1
  
  I had exactly this question. Based on [username_1](https://bricks.stackexchange.com/users/906/username_1-username_1)'s answer, I got this working. Here is a screenshot of the My Block I created to do this: TwoSwitch.
  
  ![enter image description here](https://i.sstatic.net/hqJC6.png)
  
  TwoSwitch sets one of two variables to true: `Pressed 1` or `Pressed 2`. You can then act on those values using tests. Here, I'm looping until button 1 is pressed. If button 2 is pressed, I play a sound and get another button press.
  
  ![enter image description here](https://i.sstatic.net/uu7gd.png)
  
  
  ---
  
  