Hello, I want to start off by saying I’m in a class called Intro to Robotics in my senior year of high school, and I have no programming background at all. There are a few groups of 4-5 people in my class and each group got a Vex EDR kit and we’ve been building various types of robots to do different functions all year.
The task at hand is drag racing. We are seeing who can make the fastest robot, while also being autonomous. Luckily, my group has made the fastest robot using efficient gear ratios, but we are stuck on the autonomous part as none of us know what we’re doing with the programming part.
The goal is to press a button (one of the bumper switches) so that the robot goes maximum speed for 6.7 seconds (how fast it takes our robot to travel the distance of the race track) and then stops at the finish line.
I have attached a picture of what my current code looks like and what I’m working with. If someone could please help me by telling me where i’m going wrong, or if someone could write the code for me that would be much appreciated! If you have any questions/concerns about the robot or process please ask so I can clarify!
I’m not a programming expert, but I can give a quick reply.
is the sensor plugged in to dgtl port 11?
Do you keep the touch sensor depressed? while statement
or do you want the program to keep running even after the sensor is no longer depressed? if statement
Yes, the sensor is plugged into digital port 11. I want to be able to press the button once at the start line and have it go all the way to the finish without being held down or anything.
I just know I have it plugged into the 11th slot in the digital part. I’m pretty sure it’s an input but I could be wrong. Like I said, I don’t know what I’m doing when it comes to programming.
Click on the button at the top of your RobotC window that says something like “Motors and Sensors set up”. Different RobotC versions might look different, but in this video you can get the general idea how to set these things up. I think he talks about bumper switches around minute 6, but the video gives a general overview.
I added the switch, but i’m not sure where to go from there. People are saying to replace the while statement with an if statement, but then what?
So I changed this, but what else needs done? Don’t if statements need an else in there somewhere? And do I still need the == 1 or what? Sorry for the abundance of questions but I don’t know how this stuff works.
You do not need to have an else statement, but it is good practice to catch unexpected behavior.
== is a comparison, = is an assignment
You need something along the lines of:
If sensor == value {
//action
}
You may want to put this inside of an infinite while loop (While TRUE or While 1 == 1) so that you can repeat the behavior by pressing the button again without restarting.
You need to put your code in a while true loop. If you aren’t pressing the button when the robot turns on, the while loop evaluates to false and your program ends.
Okay thanks everyone for responding and for the tips you gave. I can’t work on my robot again for another 2 hours, but I’ll try out these things then and post an update when I have time.
As Gallium pointed out, you will need to pack the code you are working on inside another while loop known as a while (true) loop, or also while(1==1). The while (true) loop keeps looping the code inside it forever, so it forces the Cortex to keep coming back to your inner while loop to evaluate the status of your bumper switch.
These type of while(true) loops are used a lot in Vex to force the Cortex to keep looking at the status of things like joystick buttons, etc. Without packing everything inside of an infinite loop like this, the code only runs once, looks at each button status for a millisecond or so, then quits.
Okay so your on the right track. What happens currently is the value is 0 at the beginning so the program ends. You need to be constantly checking the value of the button. And a lot of the suggestions so far would look something like
This is always looking at the state of the button and any time it is pressed the robot will drive forward.
while(true){
if(button){
go
}
}
Another simplified version would be
while(button ==0){}
go
This would hold the program at the beginning inside the while loop until the button was pressed.
You need brackets after the if and ending after the second wait. Currently the if only controls whether the front motor runs. What exactly is happening as that may help with further debugging?
I noticed that right after I posted the screenshot, sorry. I put them in but it still doesn’t work. The motors spin up for the 6.7 seconds indicated in the program if I press “start” on the program debugger on my desktop, but pressing the button in digital 11 does absolutely nothing.
With this setup, everything happens as described in the post I made above this one. If I changed the 0 to a 1 literally nothing happens at all no matter what I press/click. Why wont the button plugged into slot 11 work?