View unanswered posts | View active topics It is currently Fri Sep 19, 2014 12:43 pm






Reply to topic  [ 9 posts ] 
If-Else, Ultrasonic & Bumper 
Author Message
Rookie

Joined: Wed Apr 14, 2010 11:09 am
Posts: 6
Post 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;
}

}


Tue Apr 27, 2010 12:26 pm
Profile
Expert

Joined: Mon Mar 15, 2010 4:24 pm
Posts: 124
Post 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


Tue Apr 27, 2010 6:14 pm
Profile
Rookie

Joined: Wed Apr 14, 2010 11:09 am
Posts: 6
Post Re: If-Else, Ultrasonic & Bumper
I have not, but I'm not sure how to do that.


Tue Apr 27, 2010 9:13 pm
Profile
Rookie

Joined: Sat Nov 22, 2008 8:06 pm
Posts: 48
Post 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.


Wed Apr 28, 2010 12:31 am
Profile
Rookie

Joined: Wed Apr 14, 2010 11:09 am
Posts: 6
Post 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.


Wed Apr 28, 2010 12:45 pm
Profile
Expert

Joined: Mon Mar 15, 2010 4:24 pm
Posts: 124
Post 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


Wed Apr 28, 2010 4:21 pm
Profile
Rookie

Joined: Wed Jul 20, 2011 4:52 am
Posts: 1
Post a
Appreciate the info, it&rsquo;s good to know.

_________________
[u=http://www.timberlandmark.com]timberland shoes[/u]


Thu Jul 21, 2011 10:30 pm
Profile WWW
Rookie

Joined: Tue Jul 26, 2011 8:19 pm
Posts: 15
Post 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)

_________________
Member of the Sarah Heinz House VEX and FIRST teams.


Fri Sep 23, 2011 7:23 pm
Profile
Site Admin
Site Admin

Joined: Tue May 15, 2007 9:02 am
Posts: 405
Post 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

_________________
Jesse Flot
CMU Robotics Academy
ROBOTC Support


Fri Sep 23, 2011 8:32 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 9 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.