Code help

So I am trying to code and make it where i hit a button on the joystick and it turns on the motor and then i can hit it again to turn it off. But i want to have it so i can have another button to turn the same motor but have the motor at a slower speed.

Are you programming in RobotC, EasyC or what?

Just speaking in general, I guess you could create a flag for your motor to keep track of whether it’s moving or not, then create a bunch of IF statements to command the motor to respond depending on what’s been commanded already. (The following is not real code, just a rough idea.)

If (button 1 is pushed AND Motor4MovingFlag == 0)
turn motor 4 to full power;
Motor4MovingFlag = 1;

If (button 1 is pushed AND Motor4MovingFlag == 1)
turn motor 4 to zero power;
Motor4MovingFlag = 0;

If (button 2 is pushed AND Motor4MovingFlag == 0)
turn motor 4 to half power;
Motor4MovingFlag = 1;

If (button 2 is pushed AND Motor4MovingFlag == 1)
turn motor 4 to zero power; (or you could have it run at half power here, too)
Motor4MovingFlag = 0;

You could create other flags if you want and/or use other values for the flags besides just 0 and 1, thereby allowing you to monitor which buttons have already been pushed, so you could get different behaviors that way.

Yes i am coding in easy c. I don’t know what you mean by flags? Like how would i make a flag?

In my example “code” above, I created a variable I called Motor4MovingFlag. This type of variable is sometimes called a flag in programming. When its value is 1, it tells me that I’ve already commanded motor number 4 to move. (I’m just using motor 4 as an example). When its value is zero, I know I’ve commanded that same motor to turn off. The flag helps the program keep track of what the motor is doing at any instant and to help the program decide what to do when you push the same button. If you push the button while motor 4 is not moving, then the code tells that motor to turn on. If you push the button while motor 4 is moving, then the code tells the motor to turn off. The “AND” is used in the IF statement logic to check both conditions concerning the button and motor 4 - for example, that the button has been pushed and the motor 4 is moving.

If I knew EasyC, I’d try to provide more code details, but maybe someone who knows EasyC can jump in here and tell you how to do this exactly. Sorry I don’t know EasyC.

There might be other, much better ways to do the same thing in EasyC, but I just tried to present a generic way to approach the problem.

here is how I would handle the problem

bool motorOn = false;
int power;
while true
{

if(button1 == 1){
if(motorOn == false){
power = 127;
motorOn = true;
}
if(motorOn == true){
power = 0;
motorOn = false
}
}

if(button2 == 1){
if(motorOn == false){
power = 50;
motorOn = true;
}
if(motorOn == true){
power = 0;
motorOn = false
}
}
motor[portx] = power
}

That’s nice and tidy, Collin. Would you happen to know how to translate that into EasyC for the kid?

Actually that helped and I got it to work, thank you for your help.