Hello, we are trying to use a potentiometer for our mobile goal, but we are encountering some problems with our code. Every time we press the button, the mobile goal repeatedly goes up and down.
This is our code:
-pot is the name for potentiometer
-1400 is the value we want the mobile goal to stop at
-motor[FrontF] is the name for the mobile goal motor
-mogoStack is the boolean that detects whether button 7U is pressed
-We included the section for reversing the mobile goal to counter momentum
I haven’t looked at your code in any detail, but be aware that it’s extremely unlikely that the robot is going to ever land on exactly a pot value of 1400. You should probably give it an acceptable range, like between 1380 and 1420 or something like that.
The momentum of the mechanism, among other factors, will make it extremely improbable that your robot will stop at a potentiometer value of exactly 1400.
Your code looks to be unconventional in its logic, and, as such, I currently do not have the time to tell you exactly how to implement the range though. Perhaps someone else can help you there.
As has already been mentioned, if your machine has too much inertia when it tries to enter the acceptable range, then it will overshoot and go out of range even though the motor power has been removed. You might have to lower your motor values so you don’t overshoot, or you might need to widen your acceptable range.
There are other methods using PID, but I’m guessing that is beyond your abilities right now.
Even disregarding inertia, the potentiometer will rarely go to an exact measure even if moved precisely by hand. Instead of saying “if the potentiometer value is 1400,” say “if the potentiometer measure is greater than 1380 and less than 1420,” essentially.