EasyC Programming

I am trying to get a motor to go a specific height with my potentiometer in operator control. I have been trying to figure out how to do so by pressing one of the buttons on channel 8 of the joystick and I just can’t figure it out. Can you help or give any advice?

To clearify; you are trying to make a command that when pushed your motor will spin to a certain degree?

Yes you are correct

You need to detect the button press and then run the following code. Lets say the current value of the pot (potentiometer) is 100 and you want to drive your motor until the value of the pot is 1000. Something like this would work (in psuedo code). 100mS is 1/10 of a second.

Start motor

while ( the value of the pot is less then 1000 ) {
    wait 100mS
}

Stop Motor

A better version would also include a timeout so you did not get stuck in the loop forever. A simple counter could be used, the following code would stop the motor after 3 seconds if the value for the pot had not been reached. 30 x 100mS = 3 seconds. You would need to figure out a reasonable value for the timeout based on your robot design.

Initialize a counter to 30
Start motor

while ( the value of the pot is less than 1000 AND the counter is not 0 ) {
    wait 100mS
    decrease the counter by 1
}

Stop Motor

Now if you needed to go to this position from either direction then you would check for the pot being between a certain range as hitting an exact value with a pot is difficult. for example.


if (the value of the pot is less then 1000)
  Initialize a counter to 30
  Start motor forwards
else if (the value of the pot is greater than 1100 )
  Initialize a counter to 30  
  Start the motor backwards
else
  Initialize the counter to 0

while ( the value of the pot is not between 1000 and 1100, AND the counter is not 0 ) {
    wait 100mS
    decrease the counter by 1
}

Stop Motor

Hope this helps

is this for operator controls? and thank you!

You could use this technique for either operator control, after say you had detected a button push, or autonomous. Understand this is just pseudo code, not real EasyC code, it’s just to explain the principle.

I understand and thank you very much!

There is also a Sample of “PID Interrupt Service Routine” which does exactly what you want.

Oh ok and how does that work?