# Coding With Sensors

Hi,

My team was having difficulty coding a potentiometer such that it computes the rotation of a motor during driving control and once it has reached a certain value, stops the motor. The use of this is for our cap flipper, we want the motor driving the cap flipper to only spin until it has reached 90 degrees in either direction and thus a potentiometer is the way to go. (We’re using v4 hardware). Attached to this conversation is an image of the code. The motor driving the cap flipper is named “Cap” and the potentiometer is named “CapSensor”. Button 5U turns the motor in one direction while Button 5D turns it in the opposite direction. What we’re struggling with is embedding a second “if” statement that limits the rotation to a certain value (threshold). Note there is a typo in the second half of the code where “claw” is used instead of “Cap”. Can someone give a suggestion/revise the code such that once the potentiometer reaches a certain value (the value of the CapSensor once the cap flipper has reached 90 degrees) the Cap motor stops and do it for both buttons?

Regards,
Michael

I found a few things wrong with ur code. I recommend first going on YouTube and watching RobotC tutorials. Practice coding a singular motor while watching the vids then move onto sensors. Once u do that u should be pretty familiar. It can be done in less than a day if u put the effort into learning it.

So you could do this in a couple ways. One way is just to add another argument to your if statements with the && operator.

``````
if(button && potentiometer < threshold)
{
run motors
} else if(other button && potentiometer > other threshold)
{
run motors backwards
} else
{
stop motors
}

``````

If you are looking to perform a certain function while a certain parameter is true, you could also use a while loop.

``````
while(whatever)
{
do something
}

``````

On a more basic level, this way to stop your motors won’t be the most accurate due to the momentum of the flipper turning the cap even after you stop supplying power to the motor. You could attempt to remedy this by running the motor in the opposite direction for a couple hundred milliseconds, but you may have mixed results. Achieving consistent and accurate cap flipping just with code requires more complex control loops such as PID. The same result can be achieved much more simply just by using a physical limiter on your claw, so that it can only turn so far before it hits a piece of metal or something.

I’m advising my sister teams with cap bots to use servos. They are typically more accurate than potentiometers. If you have any, I’d recommend one.

This was by far the most useful advice, I’m new to coding and this was relatively comprehensible. I am confused however with the PID, I did some basic research about it and didn’t quite grasp the concept. What I will try to do instead is code the flipper so that it stops a few degrees less than 90 in either direction which will, in turn, make up for the over spinning due to momentum. My cap flipper is designed in a way that a physical limiter intervenes with the efficiency which is why I’m resulting to code. I have a similar design to 1814D’s bot (aka Delta) which can give you some insight into what exactly I’m trying to do. If all else fails I will have to result in learning the PID loop, but I will be switching to v5 very soon so I am not sure how that would work with v5. Anyways, if I have any questions after I update the code and test it in a few days (when I next have a robotics team meeting), I will let you know. Thank you for the help!