I can’t seem to figure out how to make wait times in Vex coding studio. I looked at the docs and it didn’t help. Could anyone tell me the reserved word I’m looking for?
EDIT: Also having trouble with “for” loops. Can anyone help with that?
I can’t seem to figure out how to make wait times in Vex coding studio. I looked at the docs and it didn’t help. Could anyone tell me the reserved word I’m looking for?
EDIT: Also having trouble with “for” loops. Can anyone help with that?
If you just want the program to wait for a certain amount of time you would use vex::task::sleep() using milliseconds in the parentheses
@TaranMayer About for loops
Example of for loop:
for (i = 1; i <= 5; ++i){
}
This would set i to 1, it would then check if i is less than or equal to 5. If it is not then the loop stops. If it is then i is increased by 1 and what ever is in the loops happens. It then goes back and checks if i is still less than or equal to 5 and it continues until i is greater than 5. This exact loop would run 5 times then stop.
it’s basically (run once, condition to check if run again, what to do if running again)
I know how to use a for loop, as I’ve used them forever in RobotC. It doesn’t seem to work in VCS c++ though
WAIT TIMES(cough,cough)
Ships in 8 weeks
Figured it out:
vex::sleepOnV5Orders(moreThan8Weeks);
that seemed to work.
I actually got it working, and I used an excess of code and a while loop to not need a “for” loop. Not pretty but works.
If you can post the actual problematic code, we can probably figure it out. Glad you’ve got a route through it, though.
vex::task:sleep(timeinmiliseconds);
Don’t change anything except timeinmiliseconds to an actual number and you’re good This acts exactly like a “Wait1Msec()” in ROBOTC.
For for loops you do
for(int k = 1; k <= 5; k++){
}
There are three parameters for a for loop, the first parameter is a declaration which declares integer k as equal to 1 (No need to declare/initialize outside of the parameter, unless you want to). The second parameter is the condition, that as long as the value is equal to or less than 5, it would continue to run. The third parameter is “k++” which, in short, adds 1 to k every time the for loop runs. So according to this, what is inside the for loop will run 5 times.
Hopefully this helps
To get an understanding, if you tell it to print the value of k, similar to this(I am writing from scratch so I could be off in the printing statement):
for(int k = 1; k <= 5; k++){
Brain.Screen.print("%d/n ", k);
}
You will have an output of
1 2 3 4 5
Not that it matters much at all for something this small, but as a general rule in C++ it’s better to use ++k as @DanDanrevolution suggested than it is to use k++. Since it’s a better general rule, I think it’s better to teach people to work this way as their default. Of course, those of us like me who are far more versed in C than in C++ (even though it existed, it wasn’t used so widely when I was taught computer science), k++ is our old default.
Okay. This is what I did that worked (FYI the variables like blinkLength are for some LEDs I’m coding, and they were declared earlier):
float i = 0;
while(i<numberOfBlinks){
stripLights.state(power, percentUnits::pct);
task::sleep(blinkLength);
stripLights.state(0, percentUnits::pct);
task::sleep(blinkLength);
i++;
}
As I was typing this response I noticed the issue with my old “for” loop - I used commas, not semicolons. Thanks for the help.
++var
and
var++
are two different things.
++var
does
var += 1
before the rest of the statement is evaluated, and
var++
does
var += 1
after the statement is evaluated. Saying “use this one and not that one” is somewhat akin to saying “use a hammer, not a screwdriver”. If all you need to do is put in a fastener, i.e.
var += 1
like a for loop typically uses, either is just as good as the other, as on their own they are both equivalent to
var += 1
. But if you have a nail or a screw, you should use the appropriate tool. Heck, using
var++
you could rewrite most for loops to
for(int i = 0; i++ < limit;)
and just omit the third statement altogether. Using
++var
would result in different behavior.
Yes, but I’m evaluating what was written, which is why I said “as @DanDanrevolution suggested” instead of everywhere in all of C++.
But this isn’t really true. As you said, evaluation before or afterward. Increasing it after the statement is evaluated means you create extra bits in memory and go through extra steps. If you write a for loop with far more iterations, you can easily see what I’m talking about; it can make an enormous difference in just a simple for loop. This is also why I said, “Not that it matters much at all for something this small.”
Using your analogy, we were talking about putting in a nail that doesn’t have to go in far, and I’m saying a hammer is a better tool than a screwdriver for putting the nail in. Sure, you can use both, and if you only have to barely nudge a nail in soft material, you can do it pretty quickly with a screwdriver, too. That doesn’t mean a screwdriver is a better tool for putting in nails, and I’m not going to teach anyone as a default rule that it’s irrelevant if your default is to use a screwdriver v. use a hammer on a nail in general.
That’s why I was asking to see the non-working code.
For the record, I had deleted the non-working code after no one replied for a while and I found a workaround. I now have a working “for” loop.
For clarity,