sbk
February 20, 2024, 7:38pm
1
My code should work on paper, but on robot. Pulling joystick back doesnt make car go back and X button doesn’t work
#include "vex.h"
#include "math.h"
int direction = -1;
#define directionleft = directionType::fwd;
#define directionright = directionType::rew;
void function(void){
direction = direction * -1;
}
controller console = controller();
motor leftMotorA(PORT20, ratio18_1, false);
motor leftMotorB(PORT3, ratio18_1, false);
motor_group LeftDriveSmart(leftMotorA, leftMotorB);
motor rightMotorA(PORT4, ratio18_1, false);
motor rightMotorB(PORT5, ratio18_1, false);
motor_group RightDriveSmart( rightMotorB, rightMotorA);
int main() {
while(1)
{
if (console.Axis3.value() != 0 or console.Axis2.value() != 0)
{
if (direction == -1)
{
LeftDriveSmart.spin(vex::directionType::rev, console.Axis3.value(), vex::velocityUnits::pct);
RightDriveSmart.spin(vex::directionType::fwd, console.Axis2.value(), vex::velocityUnits::pct);
}
else
{
LeftDriveSmart.spin(vex::directionType::fwd, console.Axis3.value(), vex::velocityUnits::pct);
RightDriveSmart.spin(vex::directionType::rev, console.Axis2.value(), vex::velocityUnits::pct);
}
}
else
{
LeftDriveSmart.stop();
RightDriveSmart.stop();
}
}
}
Welcome to the forums!
Can I assume that this isn’t the code that you’re actually trying to compile? There are multiple things here that should generate errors and prevent the code from actually compiling and loading to the robot.
For instance, the or
text between your if
conditions should be ||
to be compliant with C/C++.
1 Like
or is equivalent to || in modern C++.
3 Likes
Apparently this has been a thing in C++ since 1998: https://en.m.wikipedia.org/wiki/C_alternative_tokens , so not really “modern C++”. Didn’t know that until I saw this code.
2 Likes
This isn’t true, using “or” as an alternative to || is perfectly supported in c++. There may be other issues, but that isn’t one of them.
1 Like
yep,
#ifndef __ISO646_H
#define __ISO646_H
#ifndef __cplusplus
#define and &&
#define and_eq &=
#define bitand &
#define bitor |
#define compl ~
#define not !
#define not_eq !=
#define or ||
#define or_eq |=
#define xor ^
#define xor_eq ^=
#endif
#endif /* __ISO646_H */
But I’ve never, ever used them. Most projects have a mixture of C and C++ code and it’s far easier to always just use the originals.
3 Likes
sbk
February 21, 2024, 6:16am
7
I don’t know, somehow half of the code works
reuben
February 21, 2024, 3:08pm
8
check the ports on the brain, sometimes it helps to switch them. Often the software is correct ant the error is in the hardware.
Joeger
February 21, 2024, 3:08pm
9
The reason your X button doesn’t work is because you haven’t programmed it. There is no code saying what should happen when X is pressed.
sbk
February 21, 2024, 3:11pm
10
I dont see a reason why it doesnt detect joystick being pulled back
reuben
February 21, 2024, 3:11pm
11
are you using vex pro, if you are that could be your problem as they discontinued the service in favor of vs code. the platform still works but it is full of bugs and has not been updated since worlds last year.
reuben
February 21, 2024, 3:15pm
12
you could try testing your controller, some of our older ones have malfunctioning joysticks.
sbk
February 21, 2024, 5:52pm
13
Which alternative do you suggest?
sbk
February 21, 2024, 5:52pm
14
It works if I only write the joystick code, but it malfunctions when I add more button functions
reuben
February 21, 2024, 7:22pm
15
I use vs code with git hub as a version control. This is the suggested alternative, and works well when you get accustomed to the new system.
1 Like