Limit Switches help

Hi, I’m new at this sort of thing, and with a week left I could use some help with my forklift. I want to make it so it stops at each level. First level is the starting position, almost at the ground. 2nd level is the first level of the rack (the lowest level…24 inches i think). 3rd level in the programming will be the 2nd level on the rack (middle level). And the 4th level in the programming will be the highest level of the rack. Now, there aer a few ways I could do this I’ve heard. One is a metallic sensor which produces 1 if metal passes by it (a small piece of metal tape on the rope that we are using for the winch system) and 0 if its not. Here is the code I made for that:

void Default_Routine(void)
{
 	int count = 0;
	int now;
	int last;
	now = last;
	rc_dig_in04 = now;

blah blah blah…map joystick stuff…

/***************
                  * Sensor Code *
                  ***************/
 
 	if((pwm03 < 125) || (pwm03 > 129))
 	{
 		if((now == 1) && (last == 0)) //If sensor, which connects to Dig In/Out port 4, goes off...
		{
			count++; //Number of times tape passes through the sensor.
			Switch1_LED = 1; //Switch01 goes on when the tape passes the sensor.
		}
		else
		{
			Switch1_LED = 0;
		}
	
	
		switch(count)
		{
			case 2:
		
				Relay2_green = 1;
				Relay2_red = 0;
				pwm03 = 127; //Motor stops when it reaches 1st level.
				break;
			
			case 3:
			
				Relay2_green = 1;
				Relay2_red = 0;
				pwm03 = 127; //Motor stops when it reaches 2nd level.
				break;

			case 4:
					
				Relay2_green = 1;
				Relay2_red = 1;
				pwm03 = 127; //Motor stops when it reaches top level.
				break;

			case 5:

				Relay2_green = 0;
				Relay2_red = 1;
				//pwm03 does not equal 127 because it must pass the tape in order to go beneath the top level.
				break;

			case 6:
			
				Relay2_green = 0;
				Relay2_red = 1;
				pwm03 = 127; //Motor stops when it retreats to 2nd level.
				break;
			
			case 7:
			
				Relay2_green = 0;
				Relay2_red = 1;
				pwm03 = 127; //Motor stops when it retreats to 1st level.
				break;

			case 8:
			
				Relay2_green = 0;
				Relay2_red = 0;
				pwm03 = 127; //Motor stops when it retreats to ground level.
				count = 0;
				break;

		//if count == 1 then that just means that it passed through the first piece of tape which will be used to show that it is in full DOWN position.
		}
	}

Another way that someone told me about is using Limit Switches. Honestly I don’t even know what they are, so if somebody could explain that to me it’d help a lot. Would that be the easiest thing to do? I want to make pwm03 (the motor for the winch system) to stop at each of the four levels (1st level being ground level so it doesnt run into the floor or break the rope).

The line of code pwm = 127; that is in each of the cases I don’t think will work because pwm03 is mapped to the joystick so in the next loop (26 ms later) it will keep going the direction that the joystick was going.

So, using limit switches could help me maybe? I’ve never used them before. help would be greatly appreciated! Only 4 days left befoer we have to ship our robot!!

A limit switch is any kind of switch that closes when your mechanism reaches a “limit” of travel.

We use microswitches (some were included in the KOP), you could use anything including optical detector that you are using, bumper switch, etc.

They are easiest to use when there are only two “limits” (eg: - fully clockwise, fully counter-clockwise). When you have intermediate positions (as in your case) you have to be creative in how and where to place the switches. There would normally be two “limits” (bottom-most position, top-most position) and then a detector for intermediate position.

Hope this is clear - it’s been a long day.

Thanks. How would I program pwm03 to stop when the switch goes on? I’m using a joystick to control pwm03, so when I set it to 127 if the switch is open wont it still not work because during the next loop (26 milisec later) pwm03 is still mapped to p3_y and thers not enough time for the joystick to go back to neutral position. they conflict dont they?

Yes, but you can use the limit switch as a condition to whether or not you will actually map p3_y to pwm03, like so:



if( rc_dig_in01 == OPEN ) {
    pwm03 = p3_y;
} else { /* The limit switch is closed (tripped), force neutral */
    pwm03 = 127;
}


I am trying to get the micro switches to work as limit switches also and I am not sure if I am doing this right as it isn’t working at all and could use some help…

I have connected these switches to digital input 01 & 02 normally open with the signal and ground from each respective input and I am relying on the internal pullups on the RC inputs to give me a “1” condition if the switches are not activated.

We noticed that the joystick wheel is at a value of 43 if untouched (neutral) and in the down position is ~ 148 and in the up position is ~ 255, hence the loose values in my code below.

When connected to the dashboard viewer, the switch 1 and switch 2 leds light when I close my switches so I know that electrically these are working but I must have my logic wrong.

Here is my code:

if ((p3_wheel > 240) && (rc_dig_in01 = 1)) /* jaw close /
{
relay1_fwd = 0; /
FWD only if switch1 is not closed. /
relay1_rev = 1;
}
else
{
if ((p3_wheel < 200) && (rc_dig_in02 = 1)) /
jaw open /
{
relay1_fwd = 1; /
REV only if switch2 is not closed. */
relay1_rev = 0;
}
}

if ((p3_wheel < 50) || (rc_dig_in02 = 0) || (rc_dig_in01 = 0))
{
relay1_fwd = 0; /* don’t move jaw if p3 wheel is not pressed */
relay1_rev = 0;
}

This code works just fine if I do not include the rc_dig_in01 and rc_dig_in02 conditions in my if statements.

What am I doing wrong here?

Thanks!

= is an assignment operator.
== is the logical operator.

Ah yes… how silly of me to forget that…

been a while since I wrote anything in C…

thanks for the reminder.

we have limit switches wire to the digital in/outs. The motor turns untill a certian pattern of switches open/clsed is reached, To make an integer from 4 digital i/os do this:
word = dig_in01<<3
word+= dig_in02<<2
word+= dig_in03<<1
word+= dig_in04

Say dig_01 & dig_03 were closed, making “0”, the 4 bit word would be 0110 or 6.
Remember, inside the digital_in/outs are tied high, they read 1, you need to ground it to read 0.

Turn the motor until the number is the lift state you want. Think “state machine”.