My code has problems can someone revise it to see what i could fix

The arm on my robot only goes up and i’m not quite sure what is wrong with the touch sensors could someone look at the code and fix it.

Here is the code

#pragma config(Sensor, dgtl1, au, sensorTouch)
#pragma config(Sensor, dgtl2, ad, sensorTouch)
#pragma config(Motor, port1, lift, tmotorVex393_HBridge, openLoop)
#pragma config(Motor, port2, a1, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port3, a2, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port4, a3, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port5, a4, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port6, d1, tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor, port7, d2, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port8, d3, tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor, port9, d4, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port10, hd, tmotorVex393_HBridge, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

#pragma platform(VEX)

//Competition Control and Duration Settings
#pragma competitionControl(Competition)
#pragma autonomousDuration(20)
#pragma userControlDuration(120)

#include “Vex_Competition_Includes.c” //Main competition background code…do not modify!

// You may want to perform some actions before the competition starts. Do them in the
// following function.

void pre_auton()
{
// Set bStopTasksBetweenModes to false if you want to keep user created tasks running between
// Autonomous and Tele-Op modes. You will need to manage all user created tasks if set to false.
bStopTasksBetweenModes = true;

// All activities that occur before the competition starts
// Example: clearing encoders, setting servo positions, ...

}

task autonomous()
{
// …
// Insert user code here.
// …

AutonomousCodePlaceholderForTesting();  // Remove this function call once you have "real" code.

}

task usercontrol()
{
while (true)
{
motor[d1] = vexRT[Ch2];
motor[d3] = vexRT[Ch2];

motor[d2] = vexRT[Ch3];
motor[d4]  = vexRT[Ch3];

}
if(vexRT[Btn5U] == 1)
{
motor[hd] = 127;
}
if(vexRT[Btn5D] == 1)
{
motor[hd] = -127;
}
if(vexRT[Btn5U] == 0)
{
motor[hd] = 0;
}
if(vexRT[Btn5D] == 0)
{
motor[hd] = 0;
}

  if(vexRT[Btn6D] == 1)

{
motor[a1] = 127;
motor[a2] = 127;
motor[a3] = 127;
motor[a4] = 127;
}
if(vexRT[Btn6D] == 0)
{
motor[a1] = 0;
motor[a2] = 0;
motor[a3] = 0;
motor[a4] = 0;
}

if(vexRT[Btn8L] == 1)

{
motor[a1] = -127;
motor[a2] = -127;
motor[a3] = -127;
motor[a4] = -127;
}

//safety button
if(vexRT[Btn7L] == 1)
{
motor[a1] = 0;
motor[a2] = 0;
motor[a3] = 0;
motor[a4] = 0;
motor[d1] = 0;
motor[d2] = 0;
motor[d3] = 0;
motor[d4] = 0;
motor[hd] = 0;
motor[lift] = 0;
}

while(SensorValue(ad) == 1)		// Loop while robot's lower sensor is pressed in

{
motor[a1] = 0;
motor[a2] = 0;
motor[a3] = 0;
motor[a4] = 0;
}

while(SensorValue(au) == 1) // Loop while robot’s upper sensor is pressed in(add in pneumatic pistons when we get some)
{
motor[a1] = -127;
motor[a2] = -127;
motor[a3] = -127;
motor[a4] = -127;
}

  UserControlCodePlaceholderForTesting(); // Remove this function call once you have "real" code.
}

All user control code needs to be inside a single while loop, Otherwise it will not continue to execute.

Try using this code:

#pragma config(Sensor, dgtl1, au, sensorTouch)
#pragma config(Sensor, dgtl2, ad, sensorTouch)
#pragma config(Motor, port1, lift, tmotorVex393_HBridge, openLoop)
#pragma config(Motor, port2, a1, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port3, a2, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port4, a3, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port5, a4, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port6, d1, tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor, port7, d2, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port8, d3, tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor, port9, d4, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port10, hd, tmotorVex393_HBridge, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

#pragma platform(VEX)

//Competition Control and Duration Settings
#pragma competitionControl(Competition)
#pragma autonomousDuration(20)
#pragma userControlDuration(120)

#include "Vex_Competition_Includes.c" //Main competition background code...do  not modify!

// You may want to perform some actions before the competition starts. Do them in the
// following function.

void pre_auton()
{
  // Set bStopTasksBetweenModes to false if you want to keep user created tasks running between
  // Autonomous and Tele-Op modes. You will need to manage all user created tasks if set to false.
  bStopTasksBetweenModes = true;

  // All activities that occur before the competition starts
  // Example: clearing encoders, setting servo positions, ...
}

task autonomous()
{
  // .....................................................................................
  // Insert user code here.
  // .....................................................................................

  AutonomousCodePlaceholderForTesting(); // Remove this function call once you have "real" code.
}

task usercontrol()
{
  while (true)
  {
    motor[d1] = vexRT[Ch2];
    motor[d3] = vexRT[Ch2];

    motor[d2] = vexRT[Ch3];
    motor[d4] = vexRT[Ch3];

    if(vexRT[Btn5U] == 1)
    {
      motor[hd] = 127;
    }
    
    if(vexRT[Btn5D] == 1)
    {
      motor[hd] = -127;
    }
    
    if(vexRT[Btn5U] == 0)
    {
      motor[hd] = 0;
    }
    
    if(vexRT[Btn5D] == 0)
    {
      motor[hd] = 0;
    }

    if(vexRT[Btn6D] == 1)
    {
      motor[a1] = 127;
      motor[a2] = 127;
      motor[a3] = 127;
      motor[a4] = 127;
    }
    
    if(vexRT[Btn6D] == 0)
    {
      motor[a1] = 0;
      motor[a2] = 0;
      motor[a3] = 0;
      motor[a4] = 0;
    }

    if(vexRT[Btn8L] == 1)
    {
      motor[a1] = -127;
      motor[a2] = -127;
      motor[a3] = -127;
      motor[a4] = -127;
    }

    //safety button
    if(vexRT[Btn7L] == 1)
    {
      motor[a1] = 0;
      motor[a2] = 0;
      motor[a3] = 0;
      motor[a4] = 0;
      motor[d1] = 0;
      motor[d2] = 0;
      motor[d3] = 0;
      motor[d4] = 0;
      motor[hd] = 0;
      motor[lift] = 0;
    }

    while(SensorValue(ad) == 1)	// Loop while robot's lower sensor is pressed in
    {
      motor[a1] = 0;
      motor[a2] = 0;
      motor[a3] = 0;
      motor[a4] = 0;
    }

    while(SensorValue(au) == 1) // Loop while robot's upper sensor is pressed in(add in pneumatic pistons when we get some)
    {
      motor[a1] = -127;
      motor[a2] = -127;
      motor[a3] = -127;
      motor[a4] = -127;
    }

  }

}

Also, I recommend to follow the instructions in the comment left behind from the template in this line:


UserControlCodePlaceholderForTesting(); // Remove this function call once you have "real" code.

Let me know if it works

Also, if the above code doesn’t work you can try and switch out those first two “if” statements with “whiles.” (So you would have “while (vexRt [Btn…] == 1)” You could also check the joystick buttons to make sure that they work.

if(vexRT[Btn6D] == 1)
{
motor[a1] = 127;
motor[a2] = 127;
motor[a3] = 127;
motor[a4] = 127;
}
if(vexRT[Btn6D] == 0)
{
motor[a1] = 0;
motor[a2] = 0;
motor[a3] = 0;
motor[a4] = 0;
}

if(vexRT[Btn8L] == 1)
{
motor[a1] = -127;
motor[a2] = -127;
motor[a3] = -127;
motor[a4] = -127;
}

//safety button
if(vexRT[Btn7L] == 1)
{
motor[a1] = 0;
motor[a2] = 0;
motor[a3] = 0;
motor[a4] = 0;
motor[d1] = 0;
motor[d2] = 0;
motor[d3] = 0;
motor[d4] = 0;
motor[hd] = 0;
motor[lift] = 0;
}
These if statements could potentially interfere with each other. You need to account for every possible configuration of these buttons. See table below:

6D 8L 7L
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

In your above logic steps, if 6D is zero and 8L is one - you are telling the motors to both be on and off at the same time. Your logic needs to account for all 8 states listed above. So you may want to do compound if statements. Think it through. Good luck!

It is important to know that all of the buttons are sending 0’s all of the time they are not pressed. Any test for a button being 0 is true nearly all of the time, not just in the moment that it is released. As such, your code (and the code posted in reply) is sending 0 and 127 at the same time to the same motor in many places.

The following code will set motor hd to 127, 0 or -127 (and never more than one of those at the same time):


motor[hd] = (vexRT[Btn5U] - vexRT[Btn5D]) * 127;

Wow! Very elegant!!

Very not readable, but neat.


void set(int a, int value) {
    motor[a] = value;
}

void set(int a, int b, int value) {
    motor** = motor[a] = value;
}

void set(int a, int b, int c, int value) {
    motor[c] = motor** = motor[a] = value;
}

void set(int a, int b, int c, int d, int value) {
    motor[d] = motor[c] = motor** = motor[a] = value;
}

void setCond(int a, int value, int cond) {
    if(cond) motor[a] = value;
}

void setCond(int a, int b, int value, int cond) {
    if(cond) motor** = motor[a] = value;
}

void setCond(int a, int b, int c, int value, int cond) {
    if(cond) motor[c] = motor** = motor[a] = value;
}

void setCond(int a, int b, int c, int d, int value, int cond) {
    if(cond) motor[d] = motor[c] = motor** = motor[a] = value;
}

task usercontrol() {

  while(true) {

    set(d1, d3, vexRT[Ch2]);
    set(d2, d4, vexRT[Ch3]);

    if(vexRT[Btn5U]) {
      motor[hd] = 127;
    } else if(vexRT[Btn5D]) {
      motor[hd] = -127;
    } else {
      motor[hd] = 0;
    }

    setCond(a1, a2, a3, a4, 127, vexRT[Btn6D]);
    setCond(a1, a2, a3, a4, 0, vexRT[Btn6D]);
    setCond(a1, a2, a3, a4, -127, vexRT[Btn8L]);

    // Safety button
    setCond(a1, a2, a3, a4, 0, vexRT[Btn7L]);
    setCond(a1, a2, a3, a4, 0, vexRT[Btn7L]);

  }

}

The following code will stall driver controls…

// Loop while robot's lower sensor is pressed in
    while(SensorValue(ad)) {
      motor[a1] = 0;
      motor[a2] = 0;
      motor[a3] = 0;
      motor[a4] = 0;
    }

    // Loop while robot's upper sensor is pressed in (add in pneumatic pistons when we get some)
    while(SensorValue(au)) {
      motor[a1] = -127;
      motor[a2] = -127;
      motor[a3] = -127;
      motor[a4] = -127;
    }

I suggest rewriting it as…


    // Loop while robot's lower sensor is pressed in
    setCond(a1, a2, a3, a4, 0, SensorValue(ad));
    // Loop while robot's upper sensor is pressed in (add in pneumatic pistons when we get some)
    setCond(a1, a2, a3, a4, 0, SensorValue(au));

In general settings and then resetting a motor value in the same loop is a bad idea. I would slightly rework this to either not do that or to store the motor values in a temporary variable and perform one set per motor at the end.


News to me, I don’t really use ROBOTC.

I didn’t use variadic functions because I wasn’t sure if ROBOTC supported them. It doesn’t.

Also, “oh nooo I redefined four trivial functions built into a library somewhere else” give me a break.

void setCond(int a, int value, int cond) {
    if(cond) setMultipleMotors(value, a);
}

void setCond(int a, int b, int value, int cond) {
    if(cond) setMultipleMotors(value, a, b);
}

void setCond(int a, int b, int c, int value, int cond) {
    if(cond) setMultipleMotors(value, a, b, c);
}

void setCond(int a, int b, int c, int d, int value, int cond) {
    if(cond) setMultipleMotors(value, a, b, c, d);
}

task usercontrol() {

  while(true) {

    setMultipleMotors(vexRT[Ch2], d1, d3);
    setMultipleMotors(vexRT[Ch3], d2, d4);

    if(vexRT[Btn5U]) {
      motor[hd] = 127;
    } else if(vexRT[Btn5D]) {
      motor[hd] = -127;
    } else {
      motor[hd] = 0;
    }

    setCond(a1, a2, a3, a4, 127, vexRT[Btn6D]);
    setCond(a1, a2, a3, a4, 0, vexRT[Btn6D]);
    setCond(a1, a2, a3, a4, -127, vexRT[Btn8L]);

    // Safety button
    setCond(a1, a2, a3, a4, 0, vexRT[Btn7L]);
    setCond(a1, a2, a3, a4, 0, vexRT[Btn7L]);

  }

}

The following code will stall driver controls…

// Loop while robot's lower sensor is pressed in
    while(SensorValue(ad)) {
      motor[a1] = 0;
      motor[a2] = 0;
      motor[a3] = 0;
      motor[a4] = 0;
    }

    // Loop while robot's upper sensor is pressed in (add in pneumatic pistons when we get some)
    while(SensorValue(au)) {
      motor[a1] = -127;
      motor[a2] = -127;
      motor[a3] = -127;
      motor[a4] = -127;
    }

I suggest rewriting it as…


    // Loop while robot's lower sensor is pressed in
    setCond(a1, a2, a3, a4, 0, SensorValue(ad));
    // Loop while robot's upper sensor is pressed in (add in pneumatic pistons when we get some)
    setCond(a1, a2, a3, a4, 0, SensorValue(au));