A Little Help With My Code?

Hi everyone!
So basically I’m now working on my robot, which hopefully can fetch me things using its claw and arm. I’ve done writing several codes to achieve this purpose but sadly none of them worked (sobbing…).
Here are more details about my goal, I want to use analog signal to control all my 7 motors. The way I think to achieve it is the signal from Y-axis on the joystick is corresponding to the driving motors when no other buttons are pressed. When a button, for example Btn6U, is pressed, the signal from Y-axis is corresponding to other motors, for example Arm1.
The code following is the one I think makes most sense but when I download this to the Cortex, I get no response no matter how I operate my joystick.
Can someone help me with this? I think there’s no problem with my hardware so it should be my lousy code. Oh I’m using ROBOTC by the way.
Thanks in advance.
Lison

#pragma config(Motor,  port2,           Arm2,          tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port3,           Claw,          tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port4,           leftMotor,     tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port5,           rightMotor,    tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port6,           rightMotor,    tmotorVex269_MC29, openLoop, reversed)
#pragma config(Motor,  port7,           leftMotor,     tmotorVex269_MC29, openLoop)
#pragma config(Motor,  port8,           Arm1,          tmotorVex393_MC29, openLoop)


task main ()

{
	if (vexRT[Btn6U]==0 && vexRT[Btn5U]==0)
	{
		motor[leftMotor]  = vexRT[Ch3];   // Left Joystick Y value controls left motors
		motor[rightMotor] = vexRT[Ch2];   // Right Joystick Y value controls right motors
	}
   else if (vexRT[Btn6U]== 1)
  {
  	motor[Arm1] = vexRT[Ch3]/4;// When Btn6U is pressed, motor[Arm1] is controlled by vexRT[Ch3]
  	motor[Arm2] = vexRT[Ch2]/4;// When Btn6U is pressed, motor[Arm2] is controlled by vexRT[Ch2]

   }
    else if (vexRT[Btn5U]== 1)
  {
  	motor[Claw] = vexRT[Ch2]/4;        //When Btn5U is pressed, motor[Claw] is controlled by vexRT[Ch2]
  }


}

You need to put

while (1 == 1)
{

After the task main { then add a } at the end of the program. Then it should work.

With your program, you will not be able to drive and operate your claws/arm at the same time. This may be limiting. You may want to consider using a button. or second joystick for the claw/arm.

Thanks for your reply :smiley:
I’m new to coding, can you explain it to me why I need that while==1 line at the beginning of the code? Also I didn’t notice that I can’t have both my claw and arm moving at the same time so thanks for pointing out! However, I don’t really know what to do to fix this, any suggestion?
Thanks again : )

without the while loop, it will run through the program exactly once and finish. It did the job you programmed it to do and now it is done.

The while() is a loop. It will keep looping about whenever the thing inside the () evaluates to true.

So 1==1 is always going to be true so it will loop around forever allowing you to command it over and over again every fraction of a millisecond.

As for why you can only do one thing at a time, that has to do with the nature of tasks. For as long as you hold buttons 5U and 6U down, for example, the task main will get “stuck” in driving the left and rightMotors (which I assume are on your base). Because the first condition of the if statement evaluates to true, the rest of the code is never checked before looping back to the start.

The best fix that I could recommend is to create multiple tasks. Tasks run concurrently, and so you eliminate the problem of only being able to do one thing at a time. Essentially, you would create:


task base()
{
   if (vexRT[Btn6U]==0 && vexRT[Btn5U]==0)
	{
		motor[leftMotor]  = vexRT[Ch3];   // Left Joystick Y value controls left motors
		motor[rightMotor] = vexRT[Ch2];   // Right Joystick Y value controls right motors
	}
}

You would also need a task for arm, claw, etc., where each task has the same logic you already have in your original code.

Finally, you would need to make sure you start those tasks in your task main:


task main ()
{
startTask(base);
//etc
while()
{
EndTimeSlice(); //while loop has to keep on going; if task main ends, the whole program stops
}
}

Sorry for the weird formatting, I wrote the code directly in the forum. I hope that helps (and that I haven’t made any mistakes myself :P)!

Thanks for your help Giraffe! Good to know some basic knowledge of programming!

Hi Bob, thanks for your reply but here are some things that I can’t figure out:

In this case if I hold both 5U and 6U down, it’ll be vexRT[Btn6U] ==1 and vexRT[Btn5U] ==1 won’t it? Why does it meet the first if condition?

Secondly about the multiple tasks, were you suggesting I should to create tasks individually for my driving motors, my arm motors and the claw motor like the first code you gave me and combine them all together in the second template? Also, I tried to look up the timeslice command but found it too complicated to comprehend (I have to say I’m absolutely a newbie in coding), can you explain what it does here to me?

I know I’m asking too many questions…

Any answer would be appreciated!

My point above was that you can’t move and operate either of your arm or claw at the same time. You should be able to operate both the arm and claw at the same time if you push both buttons at the same time as you mention above.

One thing that might simplify the operation of your robot is to always have the joystick control the drivetrain motors, and then assign buttons to control the arm motors, like so:
If button A pressed, set arm motors to 127
Else is button B pressed, set arm motors to -127
Else set arm motors to 0

You wouldn’t need tasks to do this; just put the code above and have the drivetrain code immediately after (inside the while loop, outside the if statement). And you could easily extend this to also work with the claw motors by adding another group of if statements for it.

Also, with your code as-is, you may notice that the motors don’t necessarily stop if you have the joystick all the way up when you release the button. This is because the motors will keep running at the power that was last set as long as the program doesn’t end (it won’t because the while loop condition is always true) and the robot isn’t disabled (like at the end of a match). To fix this, you’d have to set the motors to 0 when the button isn’t pressed.

Oh I see, thanks for pointing out!

Thanks for your reply!
I’ll write a code using your method and see if I can get my robot moving ideally. I’m thinking if I lower the speed of the motor, I might even not need to use analog signal to control the arm and the claw motors, I just don’t want them to spin too fast after all.