Unusual programming question

so for this competition my school is entering the team has to design and build a robot that can play mini golf. what they have asked me to do is write a program that will make it so that when

button 1 is pushed … the robot putts gently
button 2 is pushed … the robot putts med. gently
button 3 is pushed … the robot putts med.
button 4 is pushed … the robot putts med. high
button 5 is pushed … the robot putts high

the controller that they use will not be a vex controller but the motor that they use will be plugged into a vex micro controller so obviously the controller will have to be modded somehow to work with vex

the other key factor is that there be a reversal button so instead of putting forward the bot putts back. this is for when the ball is trapped against a wall on the opposite side of the putter the bot can drive up to the ball in reverse and hit it.

what i have now is a huge loop with 5 smaller loops within it. in each of the smaller loops i have the Rx control thing that says button 1 controls motor port 1 and under that i have a motor module with the setting going in increments of 25 from 127 to 255.

i guess what i am asking is… is it possible to make it so that when you push one button the motor spins slowly and another makes the same motor spin fast?

also what would be the best way to create this “reversal” button? the way i have everything set up now channels 1-5 all control port 1.

i was kinda sucked into this job at first but now i am kinda excited about the challenge. as you may know i am not a great programmer but i catch on quick just a week ago i was asking about running with two controllers and now i have made about 10 different versions of a program with different formats to run with two controllers. i am using easy C. If you have any questions please ask i would be glad to clarify.



There are many ways to do this but a quick and dirty way would be

(pseudo code)

if (ButtonOne == pressed){
MotorSpeed = VerySlow;
if (ButtonTwo == pressed){
MotorSpeed = Slow;
if (ButtonThree == pressed){
… 'and so on
MotorSpeed = stoped; //this stops the motor when no buttons are pressed

I will leave you to fill in the code for BottonOne, pressed, VerySlow etc. I hope you get the idea. For backwards I suspect you just need negative speeds, so VerySlow could become someting like ‘direction * 20’ where direction is +1 for forwards and -1 for backwards.

Hope that helps

i see what you are saying but how do i do that in easy C

could somebody please verify that a single motor could be run at different speeds by using different channels on the controller.

Sure - using EasyC you can set the motor speed based on which channel is active. There are a variety of ways you could code this, depending on exactly how you want the controls to work.

  • Dean

well right now i am thinking of using channels 5 and 6 on both controllers to make a total of 4 putting speeds and the bottom buttons would act as the reverse of each speed. how do you suggest i vary the motor speed when a different channel is used?

Well, the button channels come through as three possible values: 0, 127 (nothing pressed), or 255. When I use them, I usually fetch the value into a variable and then use a few if() statements to determine what is going on:

if ( rx1ch5 < 64 )
    // one of the buttons is pressed
else if ( rx1ch5 > 192 )
    // the other button is pressed
    // neither button is pressed

Setting the motor speed is pretty easy. Lets say you’ve got a constant defined for your fastest putt speed called “PUTT_FASTEST” that is some value between 0 (slow) to 127 (fast). You would set the motor speed to “( 128 + PUTT_FASTEST )” whenever you wanted to putt at that speed, or “( 127 - PUTT_FASTEST )” whenever you wanted to reverse at that speed.

Basically, you are going to set up an infinite loop “while(1) { … }” that contains all your code. Inside that loop, you are going to read all four channels you are tracking into variables. You are going to have a series of four if-else blocks like the one shown above that set the motor speed based on each channel is active and in which direction.

Lastly, you should think about what happens if more than one of the channels is active at the same time. Hint: the motor keeps running at the last speed it was told to run at, so if two if() statements both fire, the later one “wins”.

There are lots of different coding styles and variations, of course, but this should get you there. The main thing is that (1) it works, and (2) you understand why it works.

[EDIT] I should add that you can do the exact same thing using the joysticks. For instance, you could use the four joystick channels on one remote instead of the four button channels on two remotes. If you did that, then you might have the Right joystick map like this:
[/INDENT]And the Left joystick map like this:
The only thing that you’d need to change is what channel numbers you read - the logic is otherwise identical. Just a thought if you want to keep this on one remote.

  • Dean

the way i have it now there is one big infinite loop within that loop there are 4 smaller loops. within the first sub-loop i have dragged in an Rx control block and set it so that RX1 ch. 5 controls port 1. still in sub-loop 1 i dragged in a motor module and set it with a user value of 255. this would act as the fastest putt. Under the next sub-loop i again dragged in an Rx control block and set it so Rx1 channel 6 controls the same port 1 but under this i dragged in a motor module but the user value on this is set to 210. and so on

my theory here is that when i hit channel 5 on Rx1 the motor will spin at it’s fastest. and when i hit channel 6 the motor will spin slightly slower is this correct?

in the top line of your code you have Rx1 ch.5 <64 what does this mean?

Rx1Ch5 is the back left button on the transmitter. When neither up or down is pressed, it returns a value of 127. When one button is pressed, it returns 0. When the other is pressed, it returns 255. Saying

“if(rx1ch5 < 64)”

is the same as
if(rx1ch5 = 0) – or – if(rx1ch5 < 127) - it means the code inside the if statement is only run when rx1ch5 is 0.

rx1ch5 is a variable. I named it this to indicate it holds the value of channel 5 from receiver 1. Of course, you have to use a function block to retrieve the value of the receiver channel into this variable, but that is just one function block in EasyC.

The if() statement is checking the value held by the variable rx1ch5 to see if it is below 64. If it is, then the code inside the following statement " { … } " runs. If it isn’t below 64, then the code inside the following statement is ignored.

I picked 64 because it is half-way between 0 and 127. In the later if() statement, I picked 192 because it is half-way between 127 and 255. There is nothing magic about those numbers, but they work well for me.


  • Dean

You could potentially even do this with just one set of buttons.

If you used a timer instead, you could do something like…

quick press is a weak put, hold down for 2 seconds is a bit stronger, 4 seconds is a bit more, 6 seconds is strongest.

Heck, you could do this and make it even more variable, adding a little bit of power for every second. With values between 1-127, you could have that many steps (every half second adds +1 or +2?).

The opposite button could simply function the same way but be a reverse putt.

Is there any reason you wanted to have your putt setup the way you were talking about?

Another option would be to use the analog stick. If you move the stick slowly, it puts slowly. Move it faster, it putts faster.

If you need the finely controlled, 4 sets of puts you could use a timer but make the spacing further apart. Quick press is weak, 5 seconds is a bit more, 10 is a bit more, 15 is max.

This little golf application seems ideal for a small mod I made to my transmitter. It allows you to move a stick to a given position, remove your hand and it stays there. The idea being the farther forward of center you place the stick(Channel 3 for example) the faster your motor will move once you release the ‘trigger’(Channel 5 for example). Likewise if placed rearward of center. The stick moves with definite detents, clicks as it moves through its range. The friction from which holds the stick where you leave it like a tractor throttle control. Or throttle on a R/C helicopter or airplane. Axis 1-4 can each be modded individually or in combination.

Open up the transmitter case.
Remove the centering spring from the channel you want to affect.
This will mean that it will no longer automatically return to center.
If you want to maintain the automatic recentering but just add the clicky feel you can leave the spring in place, however the added friction of the next step may prevent the stick from reliably returning fully to the exact center position.
Now find yourself a small piece of sheet metal approximately 1" x 1/2". This can be almost any scrap. I used the nickel battery tabs from a dead tv remote. Poke a hole in 1 end of the strip with a nail. Bend and trim as necessary and affix with a small screw to the post provided inside. One end of the tab fits a slot in that post. The other end engages a movable quadrant attached to the stick providing friction and detents. It looks as if it was designed for this option.
Good luck. And please be aware that this may void your warranty, ruin your transmitter if your screwdriver slips or cause it to emit spurious radiation causing you to be picked up by the FCC or those guys that monitor the tags on your pillows.:eek:
But probably everything will be just fine.:rolleyes: