fbri
April 16, 2021, 8:13pm
1
Does anyone have a suggestion why this code isn’t working properly. When the program is run, the motor immediately starts without the bump being pressed.
{
(BumperF.pressed());
LeftMotor.setVelocity(50, percent);
LeftMotor.spin(forward);
wait(5, seconds);
LeftMotor.stop();
}
what exactly do you want your code to do?
(BumperF.pressed());
is not how you detect a bumper press.
try
if(BumperF.pressing()){
//motor commands here
}
Also you’re telling LeftMotor to stop instead of RightMotor, although that could be intentional.
3 Likes
fbri
April 16, 2021, 8:29pm
3
Wanting a basic code for beginners: that once the bump switch is pressed, the motor starts for 5 seconds and stops.
Try putting your motor commands inside that if statement. They will only run when the program detects that the bump switch is pressed.
3 Likes
fbri
April 19, 2021, 12:40pm
5
Good morning,
Is there a way around the “if” statement? I wasn’t ready to introduce “if” statements yet.
Sylvie
April 19, 2021, 1:20pm
6
The way around if statements involves syntax much more complicated than if statements.
5 Likes
fbri
April 19, 2021, 2:10pm
7
Here is the code with the if statement: It’s still not working.
int main()
{
if (BumperF.pressing())
{
LeftMotor.setVelocity(50, percent) ;
LeftMotor.spin(forward);
wait(5, seconds);
LeftMotor.stop();
}
}
Sylvie
April 19, 2021, 2:12pm
8
What behavior does it execute? Do any of the motors spin at all?
fbri
April 19, 2021, 2:14pm
9
It doesn’t do anything. No behavior executes. OH, the LED is on.
Your code needs to be surrounded in a while statement…
A while statement makes it so the code checks the statement frequently and continuously. Right now it only checks the if statement once.
E.G.
int main(){
while(true)
{
//if statement thing here
wait(0.1, seconds);
//This wait statement prevents the loop from going to fast
}}
Let me know if you have questions.
4 Likes
Sylvie
April 19, 2021, 2:18pm
11
Sam is correct. You need a while loop surrounding your if statement.
4 Likes
Your code needs to have a while statement and an if statement, if I’m correct! Hope this helps!
1 Like
fbri
April 19, 2021, 2:41pm
13
Thanks that worked. Thanks for the help. I was attempting to use V5 Text for PLTW POE Control Systems versus Robot C.
For RobotC, I didn’t have to use the while and if statements for a basic code, it was simply:
untilbump(bumpswitch)
startmotor(rightmotor, 63);
wait(5);
stopmotor(rightmotor);
1 Like
tfriez
April 19, 2021, 2:46pm
14
If you’re trying to create “simple” projects, why not use Blocks? It generates the same C++ code as if you were doing it in VEXcode Pro/Text - so there’s no disadvantage in terms of execution speed.
4 Likes
fbri
April 19, 2021, 3:12pm
15
I wanted to use the text, since we tend to compete in VEX competitions.
Sylvie
April 19, 2021, 3:21pm
16
Good choice. If you have any more issues or questions, feel free to ask, and we’ll be more than happy to assist.
fbri
April 19, 2021, 3:22pm
17
Great and Thank you to everyone.
1 Like
tfriez
April 19, 2021, 3:48pm
19
VEXcode’s block interface is 100% competition legal and capable. A number of teams have made it to VEX Worlds programming in block-based coding.
2 Likes
fbri:
For RobotC, I didn’t have to use the while and if statements for a basic code, it was simply:
untilbump(bumpswitch)
startmotor(rightmotor, 63);
wait(5);
stopmotor(rightmotor);
The reason for this is that the untilbump method essentially does
while(!bumpswitch.pressing()){
sleep(20);
}
If you want to maintain a similar structure to that code, then the code can be rewritten as:
int main()
{
while(!BumperF.pressing()){ //this loop effectively replaces the untilbump() method
wait(0.02, seconds);
}
LeftMotor.setVelocity(50, percent) ;
LeftMotor.spin(forward);
wait(5, seconds);
LeftMotor.stop();
}
3 Likes