View Single Post
  #5   Spotlight this post!  
Unread 18-12-2007, 00:44
slavik262's Avatar
slavik262 slavik262 is offline
We do what we must because we can.
AKA: Matt Kline
FRC #0537 (Charger Robotics)
Team Role: Alumni
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Sussex, WI
Posts: 310
slavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to behold
Send a message via AIM to slavik262
Re: Using a limit switch to limit motion

It's possible it's a gravity thing, but if not:

Code:
If (input > 127 && limit == 1)
motor = 127;

else
motor = input;
This code simply stops the motor from going forward once it hits its limit.

replace "input" with your input, "limit" with your limit switch and "motor" with the pwm to the motor

If you want to take it to the next step, you could use a potentiometer to keep track, with the use of a variable, what the location of arm is and automatically have it slow down when it reaches a certain point, and stop when it reaches tht limit. Assuming the pot had a range of 0 to 255, the code would look something like this:

Code:
#define Upper_Value 225 //This will slow the motor once the arm reaches within 30 of its max
#define Lower_Value 30  //This will slow the motor once the arm reaches within 30 of its minimum
#define Slow_Speed //The speed in either direction which you want the motor to slow to once it reaches either value
unsigned short Pot_Value; //Declare a variable to keep track of your potentiometer value
Put these #defines at the top of your code. Wherever the name is used in the code, the value will be stubstituted in. Also declare a variable called Pot_Value to keep track of your potentiometer value

Code:
Pot_Value = pot_input; //Update the potentiometer value

if (input > 127 && Toplimit == 1) // If the top limit switch is pressed and the joystick is being pressed forward
	motor = 127;  //Stop the motor, regardless of input

else if (input < 127 && Bottomlimit == 1) // If the bottom limit switch is pressed and the joystick is being pressed forward
	motor = 127;  //Stop the motor, regardless of input

else if (Pot_Value => Upper_Value && input > 127) //If the arm is at or past the upper value and joystick is pressing upward
	motor = 127 + Slow_Speed; //Set the arm to forward slow speed

else if (Pot_Value =< Lower_Value && input < 127) //If the arm is at or below the lower value and joystick is pressing down
	motor = 127 - Slow_Speed; //Slow the arm to reverse slow speed

else //If the arm is between the limits
	motor = input; //assign the motor directly to your input
Again, replace "input" with your input, "motor" with the pwm to the motor, Toplimit with the top limit switch, Bottomlimit with the bottom limit switch and "pot_input" with your potentiometer input

Feel free to PM me if you have any questions.
__________________