ROBOTC.net forums
http://www.robotc.net/forums/

I need help with touch sensors
http://www.robotc.net/forums/viewtopic.php?f=52&t=5531
Page 1 of 1

Author:  Mike6149 [ Thu Feb 21, 2013 6:54 pm ]
Post subject:  I need help with touch sensors

So what I am trying to accomplish in the code is how can I stop the motors from running while the're in a while loop. I just want the two lift motors to stop responding to the controller after it touches the senors. It has been a quite troublesome thing googling and scanning the forums for the answer. Please help :-)
Code:
#pragma config(Hubs,  S1, HTMotor,  HTMotor,  HTMotor,  HTServo)
#pragma config(Sensor, S1,     ,               sensorI2CMuxController)
#pragma config(Motor,  mtr_S1_C1_1,     leftDrive,     tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S1_C1_2,     rightDrive,    tmotorTetrix, openLoop, reversed)
#pragma config(Motor,  mtr_S1_C2_1,     lift1,         tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S1_C2_2,     lift2,         tmotorTetrix, openLoop, reversed)
#pragma config(Motor,  mtr_S1_C3_1,     cup,           tmotorTetrix, openLoop, reversed)
#pragma config(Motor,  mtr_S1_C3_2,     telescope,     tmotorTetrix, openLoop)
#pragma config(Servo,  srvo_S1_C4_1,    servo1,               tServoNone)
#pragma config(Servo,  srvo_S1_C4_2,    servo2,               tServoNone)
#pragma config(Servo,  srvo_S1_C4_3,    servo3,               tServoNone)
#pragma config(Servo,  srvo_S1_C4_4,    servo4,               tServoNone)
#pragma config(Servo,  srvo_S1_C4_5,    servo5,               tServoNone)
#pragma config(Servo,  srvo_S1_C4_6,    servo6,               tServoNone)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "JoystickDriver.c"

void initializeRobot()
{
  // Place code here to sinitialize servos to starting positions.
  // Sensors are automatically configured and setup by ROBOTC. They may need a brief time to stabilize.

  return;
}

task main()
{
  initializeRobot();

  waitForStart();   // wait for start of tele-op phase

  while (true)
  {
             if(joystick.joy1_y1 > -8 && joystick.joy1_y1 < 8)
     { motor[leftDrive] = 0; }
          else
      { motor[leftDrive] = joystick.joy1_y1; }

         if(joystick.joy1_y2 > -8 && joystick.joy1_y2 < 8)
     { motor[rightDrive] = 0; }
          else
      { motor[rightDrive] = joystick.joy1_y2; }

        if(joystick.joy2_y1 > -8 && joystick.joy2_y1 < 8)
     { motor[lift1] = 0;
        motor[lift2] = 0;}
          else
      { motor[lift1] = joystick.joy2_y1/2;
         motor[lift2] = joystick.joy2_y1/2; }

        if(joystick.joy2_x2 > -8 && joystick.joy2_x2 < 8)
     { motor[cup] = 0; }
          else
      { motor[cup] = joystick.joy2_x2/7; }

      if(joy1Btn(7) == 1)
      {motor[telescope] = 30;
         wait1Msec(200);}
         
      if(joy1Btn(8) == 1)
      {motor[telescope] = -30;
         wait1Msec(200);}

  }
}

Author:  JohnWatson [ Fri Feb 22, 2013 11:11 am ]
Post subject:  Re: I need help with touch sensors

Code:
  if(joy1Btn(7) == 1)
      {motor[telescope] = 30;
         wait1Msec(200);}
         
      if(joy1Btn(8) == 1)
      {motor[telescope] = -30;
         wait1Msec(200);


You're on the right track here; pressing the 7 button will move the motor in a positive direction, and pressing 8 will move it in a negative direction. To stop the movement once the switches have been pressed, though, you will need to add a couple of things to the program. First, we will need to add an if statement to stop the motor when the switch is pressed;

Code:
if(SensorValue[switch] == 1)
{
motor[telescope]=0;
}


If we just added this in as-is to the program it wouldn't actually keep the motors from moving; instead, it would cause the motors to move in a jerky fashion when the switch is pressed and the joystick button is pressed; since we want the motor to stop moving in a given direction when the switch is pressed, we will need to try something else. What happens if we add the condition to the existing if statement?

Code:
if(joy1Btn(7) == 1 && SensorValue[switch] == 1)
      {motor[telescope] = 30;
         wait1Msec(200);}


Remember, the double ampersands (&&) mean 'AND'; the if statement will only run if BOTH of the conditions are true. In this case, it still won't work because the if statement will only run the code if the joystick button IS pressed AND the switch IS pressed; we want to make the if statement's code execute when the joystick button IS pressed AND the switch is NOT pressed (this last sentence from 'we want...' on is a great example of pseudocoding and something I recommend making good use of; for more information on pseudocode, check out this PDF).

Code:
if(joy1Btn(7) == 1 && SensorValue[switch] != 1)
      {motor[telescope] = 30;
         wait1Msec(200);}


This should work; the if statement will only execute its code when the joystick button IS pressed AND when the switch is NOT pressed; another way of saying this is 'If the value of joystick button 7 EQUALS(==) 1 AND(&&) the value of switch DOES NOT EQUAL(!=) 1, then execute the following code.

The next problem you may run into; how do you stop the motor? This one I'll let you research a little bit; we actually have a section of our Video Curriculum Trainer you will find very helpful (http://www.education.rec.ri.cmu.edu/pro ... index.html Under the Remote Control -> Buttons -> Controlling the Arm section; this is the VEX Video Trainer so you will have to modify your code accordingly, but all of the concepts you will need are in that section; definitely take a look at the three videos and the related materials underneath. The NXT/TETRIX equivalent Video Curriculum Trainer can be found here: http://www.education.rec.ri.cmu.edu/pre ... index.html).

Keep us updated on your progress!

Author:  Mike6149 [ Fri Feb 22, 2013 8:53 pm ]
Post subject:  Re: I need help with touch sensors

That actually helped me out quite a bit! Thanks a bunch, if I may have another question soon here.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/