View unanswered posts | View active topics It is currently Fri Nov 28, 2014 12:01 pm






Reply to topic  [ 2 posts ] 
Touch Sensor not working? 
Author Message
Rookie

Joined: Fri Feb 15, 2013 5:01 pm
Posts: 1
Post Touch Sensor not working?
Using the touch sensor as run switch to execute code after download
VEX PIC
Please review code
thanks


Attachments:
File comment: Review basic code compile just will not respond to logic?
Moving Forward-wait-jc.c [2.76 KiB]
Downloaded 220 times
Fri Feb 15, 2013 5:47 pm
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 610
Post Re: Touch Sensor not working?
For reference;

This particular issue was caused by the second while loop not having any braces to encapsulate which code it should control.

Code:
   while(SensorValue(touchSensor) == 0)      // Instructs robot to do nothing while the touch sensor isn't pressed
   {
   }
   wait1Msec(250);                         // Wait just a bit before moving on (to help touch sensor remain consistant

   while(SensorValue(touchSensor) == 1)      // Loop while robot's bumper/touch sensor IS pressed in

   motor[frontRightMotor] = 127;        // Motor on port2 is run at full (127) power forward
   motor[backRightMotor]  = 127;        // Motor on port3 is run at full (127) power forward
   


If there aren't any braces following a loop or if statement's condition, the loop or if statement will look for the next single line of code to 'control', in this case the 'motor[frontRightMotor] = 127;' command. So, instead of doing nothing while the touch sensor was pressed (which was the intention of the program, to have a 'wait until bump' command to start the program) the program would turn on the frontRightMotor at a power level of 127.

The correct code is:

Code:
   while(SensorValue(touchSensor) == 0)      // Instructs robot to do nothing while the touch sensor isn't pressed
   {
wait1Msec(10);                         // This is for debouncing; it can be lowered down to 5~10ms without any side effects
   }
   

   while(SensorValue(touchSensor) == 1)      // Loop while robot's bumper/touch sensor IS pressed in
        {
wait1Msec(250); // This is for debouncing; it can be lowered down to 5~10ms without any side effects
   }
    
   
        motor[frontRightMotor] = 127;        // Motor on port2 is run at full (127) power forward
   motor[backRightMotor]  = 127;        // Motor on port3 is run at full (127) power forward
   

_________________
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


Mon Feb 18, 2013 12:14 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


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.