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

If-Else, Ultrasonic & Bumper
http://www.robotc.net/forums/viewtopic.php?f=11&t=2362
Page 1 of 1

Author:  emwbjr [ Tue Apr 27, 2010 12:26 pm ]
Post subject:  If-Else, Ultrasonic & Bumper

I've attempted to program my robot to have the servos move back and forth based on certain conditions. I wanted them to begin moving when the Ultrasonic sensor detected something less than 12in away. And if it detected nothing, the servos wouldn't move. The second If-Else statement was to make the servos move if the bumper is pressed. The problem may be with the "//...//" notes, not sure. The servos seem to move automatically, regardless of the stimulus, and the button doesn't seem to make anything happen yet. My code is as follows, slightly shortened:

Code:
#pragma config(Sensor, in5,    sensorSonar,         sensorSONAR, int2)
#pragma config(Sensor, in6,    Button,              sensorTouch)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main ()
{

if(SensorValue(sensorSonar) < 12) //If something is detected less than 12inches away...//
{

bMotorReflected[port3]= 1;
motor[port2]= -50; // This simply makes the left side of the jaw move //
motor[port3]= -50; // This makes the right side of the jaw move //
wait1Msec (200); // Time before the mouth to move back up //
motor[port2]= 45;
motor[port3]= 45;
wait1Msec (200); // Time before the mouth moves back down //
motor[port2]= -50;
motor[port3]= -50;
wait1Msec (200);
motor[port2]= 45;
motor[port3]= 45;
wait1Msec (200);

} //Ends true if statement for sonar//
else
{
motor[port2]= 0;
motor[port3]= 0;

} //Ends the fasle esle statement//

if(SensorValue(Button)==1) //If the bumper is pressed...//
{
  motor[port2]= -50;
  motor[port3]= -50;
  wait1Msec (200);
  motor[port2]= 45;
  motor[port3]= 45;
  wait1Msec (200);
  motor[port2]= -50;
  motor[port3]= -50;
  wait1Msec (200);
  motor[port2]= 45;
  motor[port3]= 45;
  wait1Msec (200);
}
else //If the button isn't pressed, the mouth won't move//
{
 motor[port2]= 0;
 motor[port3]= 0;
}

}

Author:  Gary Samad [ Tue Apr 27, 2010 6:14 pm ]
Post subject:  Re: If-Else, Ultrasonic & Bumper

Well, there is no problem with the comments, although you don't need the "//" at the end of the line. Have you tried just reading the sonarSensor and displaying the value?

Gary

Author:  emwbjr [ Tue Apr 27, 2010 9:13 pm ]
Post subject:  Re: If-Else, Ultrasonic & Bumper

I have not, but I'm not sure how to do that.

Author:  bluesaberist [ Wed Apr 28, 2010 12:31 am ]
Post subject:  Re: If-Else, Ultrasonic & Bumper

You can make it display sensor values using the RobotC debugger. All you need is to have the sensor set up properly.

I suggest you try replacing

if(SensorValue(Button)==1)

with this:

if(SensorValue(Button)==0)

I think the buttons may have inverted logic.

This probably isn't what is causing the problem, but you should be setting up the servos/motors in the motor and sensor setup menu.

Author:  emwbjr [ Wed Apr 28, 2010 12:45 pm ]
Post subject:  Re: If-Else, Ultrasonic & Bumper

I tried switching the bumpers, and I'm not the most proficient debugger. I brought the menu up, and I can see the sensors, but I can't tell their actual values, the change but not due to what I actually do to it.

Author:  Gary Samad [ Wed Apr 28, 2010 4:21 pm ]
Post subject:  Re: If-Else, Ultrasonic & Bumper

I'm not sure your #pragma statements look right. You might want to go back to the sensor setup window and verify that you have the appropriate sensor set up on the correct port.

Gary

Author:  huoxueren [ Thu Jul 21, 2011 10:30 pm ]
Post subject:  a

Appreciate the info, it&rsquo;s good to know.

Author:  Eddie [ Fri Sep 23, 2011 7:23 pm ]
Post subject:  Re: If-Else, Ultrasonic & Bumper

I'm pretty sure that both the button and SONAR are digital sensors. In which case, you need to make sure that you have them set up as such in RobotC and on the robot.

Also, I may have this wrong but I'm under the impression that the SONAR cable 1 should be in port 5 followed by the SONAR cable 2 in port 6.

Try using this for your #pragma and make sure that the sensors are plugged into the corresponding digital ports.

Code:
#pragma config(Sensor, dgtl5,  sensorSonar,         sensorSONAR_cm)
#pragma config(Sensor, dgtl7,  Button,              sensorTouch)

Author:  jbflot [ Fri Sep 23, 2011 8:32 pm ]
Post subject:  Re: If-Else, Ultrasonic & Bumper

If you want to have the code repeat or "go back and forth" you'll need to put all of the code in task main in a while loop: http://www.robotc.net/vex_full/reference/hp_while.pdf

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