Please help me with this sensor

Hi everyone, I’m using a metal sensor to track the height at which my forklift is at. There will be 4 pieces of metallic tape on a rope (using a winch system), one for each level of the rack and one for the ground. So, when the rope passes through the sensor and the sensor passes a piece of tape, lights will go on on the OI and the motor will stop. If I want to go to the next level I want to just keep holding the joystick for that winch motor. If I want it to stop at the level, the pwm03 value will equal 127 and hopefully if I let go of the joystick really fast, the motor will actually turn off. How do I make it so there is a small pause, enough time for me to let go of the motor before pwm03 value is remapped to the joystick in the next 26ms loop so the motor will stop? I want to see these (relay2) lights to go on when the forklift is going up, and I want to stop the motor from lifting the forklift at each level. I don’t think what I have in my code will work though, what can I do to pause at pwm03 = 127 so that it wont continue moving?

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

blah blah blah…map joysticks to pwm…


                  /***************
                  * 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.
		}
	}

Any suggestions as to how I can stop the motor at each location?

I’m the only programmer and electrician on my team and my team is made up of 5 people, so any help would be fantastic!

Try using one of the joystick buttons. If you depress the button while moving the joystick, have the motor stop at the level you want. Otherwise the motors continue to the next level.

We’re using limit switches on the lift. The motor runs untill the lift raises and the upper switch is closed. Going down, there is a lower limit switch that closes, letting the program know it’s down. These switches plug into digital in/outs. One thumb button starts it up, the other starts it down.

How would I program either of those two scenarios?

As you operate the lift, copy the joystick value to the Victor. But when the metal strip is detected by the sensor, copy 127 to the Victor instead, ignoring the joystick.

Now you want to wait awhile for the motor to come to a stop. Create a counter variable. Set it to 3 (if you want to ignore the next 3 times the user_routines function is called). Each time the master processor calls your function, if the counter is not zero, decrement the counter. Only read the joystick after the counter value reaches zero.

Otherwise you could just use the limit switch idea — make the metal tape long enough that the motor is able to come to a stop before the tape runs out :slight_smile:

Lots simpler than using the counter.

As you operate the lift, copy the joystick value to the Victor. But when the metal strip is detected by the sensor, copy 127 to the Victor instead, ignoring the joystick.

what do you mean?

Hi, I’ve posted some of this a few times, but I think I might be able to get it to work now. I’m using a winch system for my forklift and I’ll be using a metal sensor and 4 pieces of metallic tape on the rope for the winch to let the program know what level the forklift is on. 1st level being the ground level, and 4th level being the highest level on the rack. I was having a problem with turning the motor off at each level, but now I think i might have solved it. Any help would be appreciated ENORMOUSLY!

Code:
void Default_Routine(void)
{
int count = 0;
int now; //for mapping rc_dig_in04 to ON or OFF (sensor)
int last; //for mapping rc_dig_in04 to ON or OFF (sensor)
int now2; //for mapping p3_sw_trig to ON or OFF (reset p3_y)
int last2; //for mapping p3_sw_trig to ON or OFF (reset p3_y)
int now3; //for mapping p3_sw_aux1 to ON or OFF (reset counter)
int last3; //for mapping p3_sw_aux1 to ON or OFF (reset counter)

now = last;
rc_dig_in04 = now;

last2 = now2;
now2 = p3_sw_trig;

last3 = now3;
now3 = p3_sw_aux1;then some joysticks are mapped (pwms..)

then

Code:
/***************
* Sensor Code *
***************/

if((now3 == 1) && (last3 == 0)) //p3_sw_aux1 sets counter to 0 in case forklift needs to be reset.
{
	count = 0;
}


if((now2 == 1) && (last2 == 0)) //p3_sw_trig sets joystick 3 y-axis to forklift motor.
{
	pwm03 = p3_y;
}


if(( pwm03 &lt; 125) || (pwm03 &gt; 129)) //if py_3 is being used
{
	
	if((now == 1) && (last == 0)) //If sensor, which connects to Dig In/Out port 4, sends a signal...
	{
		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 1:			//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.

			Relay2_green = 1;
			Relay2_red = 0;
			break;

		case 2:
	
			Relay2_green = 1;
			Relay2_red = 0;
			pwm03 != p3_y;
			pwm03 = 127; //Motor stops when it reaches 1st level of rack.
			break;
		
		case 3:
		
			Relay2_green = 1;
			Relay2_red = 0;
			pwm03 != p3_y;
			pwm03 = 127; //Motor stops when it reaches 2nd level.
			break;

		case 4:
				
			Relay2_green = 1;
			Relay2_red = 1;
			pwm03 != p3_y;
			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 != p3_y;
			pwm03 = 127; //Motor stops when it retreats to 2nd level.
			break;
		
		case 7:
		
			Relay2_green = 0;
			Relay2_red = 1;
			pwm03 != p3_y;
			pwm03 = 127; //Motor stops when it retreats to 1st level.
			break;

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


	}
}You think it will work? If the sensor senses the metal, the motor will stop because it no longer is mapped to the joystick. Then if I want to keep going, I press the p3trigger button and pwm03 is remapped to the joystick because of an if statement. By the way, the relay lights just mean that if its going up, the green light is on. if its going down the red light is on. if its at the top, they are both on. Also, there are some safety measures with this, as in, since the counter counts each time the tape passes through the sensor, it must go up the whole way and down the whole way, it cannot go up to one level, then down a level, then up two...the counter will get all messy cabooy, and i haven't had the time to figure out the code for that (i would have to include the direction of the y-axis and it'd get complicated..im new at this so imma keep this as simple as possible). So, if the driver accidentally goes down when he was supposed to go up, he would go all the way down to the bottom and then press p3_sw_aux1 which would reset the counter. Tell me if you understand any of this and let me know if you think any of this would work. I could really use the confidence.