View unanswered posts | View active topics It is currently Fri Oct 31, 2014 9:28 am






Reply to topic  [ 3 posts ] 
I need help with touch sensors 
Author Message
Rookie

Joined: Thu Feb 21, 2013 6:33 pm
Posts: 2
Post 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);}

  }
}


Thu Feb 21, 2013 6:54 pm
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 597
Post 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!

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Fri Feb 22, 2013 11:11 am
Profile
Rookie

Joined: Thu Feb 21, 2013 6:33 pm
Posts: 2
Post 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.


Fri Feb 22, 2013 8:53 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: Sillyprogrammer and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.