Programming Operator control with Limit Switches

I’m trying to programe operator control with limit switches and have run into some trouble. How do I programe the robot so that when the switch is depressed it will not allow a motor to go cw only ccw. I figure this will probably be done with an if statement but I’m not exactly sure how. Any help would be appreciated.

How about some pseudocode…

Read JoyStick or Compute PwmCmd for Motor
Read LimitSwitch // 0 = Pushed? 1 = Released?
If (LimitSwitchPushed) Then
If (PwmCmd > 127) Then // Double check if you should use “>” or “<”
PWMCmd = 127 // Instead of turning CW, do nothing.
}[/INDENT]}[/INDENT]Send PwmCmd to Motor

Make sense?

PS: When the switch is pressed and PwmCmd would cause the motor to turn CW, if you want it to turn CCW instead of doing nothing, then replace “PwmCmd = 127” with “PwmCmd = 255 - PwmCmd”. This will preserve the PwmCmd’s speed information, but will reverse the direction it tells the motor to turn.

Ok thanks, I’m guessing this would be used in mplab? We kept working on it in easyc and eventually went with when the arm came down and hit the sensor the arm moved up until it was off the sensor, but we still might use your code thanks :).

I am just illustrating the logic needed; the outline I gave above is neither EasyC nor regular C. That is why I called it pseudocode.

You can implement thiis logic in just about any version of any language’s syntax and semantics.