468 views

# Wait Times

1. 4 weeks ago

### TaranMayer

Dec 19 Arizona 6142w
Edited 4 weeks ago by TaranMayer

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?

2. ### JuiceBox

Dec 20 Ohio 6741E

@TaranMayer 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

3. ### DanDanrevolution

Dec 20 Massachusetts
Edited 4 weeks ago by DanDanrevolution

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)

4. ### TaranMayer

Dec 20 Arizona 6142w

Example of for loop:

for (i = 1; i <= 5; ++i){

}
Be sure to declare i as an int earlier in your program.

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

5. ### callen

Dec 20 Braintree, MA, USA

Could you post your problematic code?

Also, @DanDanrevolution , you can declare int i right there.

6. ### 7517j

Dec 20 Pillow on my bed 7517J

WAIT TIMES(cough,cough)

Ships in 8 weeks

7. ### TaranMayer

Dec 20 Arizona 6142w

@7517j 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.

8. ### callen

Dec 20 Braintree, MA, USA

@TaranMayer 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.

9. ### [TVA]Connor

Dec 20 South Texas 1814D
Edited 4 weeks ago by [TVA]Connor
`vex::task:sleep(timeinmiliseconds);`

Don't change anything except timeinmiliseconds to an actual number and you're good :D 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 `
10. ### callen

Dec 20 Braintree, MA, USA

@[TVA Connor]The third parameter is "k++" which, in short, adds 1 to k every time the for loop runs.

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.

11. ### TaranMayer

Dec 20 Arizona 6142w

@callen If you can post the actual problematic code, we can probably figure it out. Glad you've got a route through it, though.

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.

12. ### John TYler

Dec 20 WA

@callen `++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.

13. ### callen

Dec 20 Braintree, MA, USA

@John TYler @callen `++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".

Yes, but I'm evaluating what was written, which is why I said "as @DanDanrevolution suggested" instead of everywhere in all of C++.

@John TYler 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 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.

14. ### callen

Dec 20 Braintree, MA, USA

@TaranMayer As I was typing this response I noticed the issue with my old "for" loop - I used commas, not semicolons. Thanks for the help.

That's why I was asking to see the non-working code.

15. ### TaranMayer

Dec 22 Arizona 6142w

@callen 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.

16. ### callen

Dec 22 Braintree, MA, USA

@TaranMayer 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,

1. If you want better answers, you should always post your non-working code up front. Otherwise you'll leave everyone in a position of just guessing. Essentially, you'll just make people waste time asking for you to post the code or giving you replies that you don't find helpful, which will delay the advice you want.
2. You got a lot of replies within roughly one day, and that at a time when many people are on break or in the middle of exams and so may not be able to reply readily. Even when people can reply regularly, there is a question of time zones and of when people sleep. Of course, much the replies were things you didn't need to hear or requests to see the code, as I mentioned in point 1. Regardless, people replied pretty quickly.