Help with servo code

I got this code and I’m a little confused, Im trying to move 5 servos, all different degrees, at the same time when a button is pressed, if possible, I want it to stop after about 2 minutes. I dont see any degrees going on like in the “sweep” examples. Could someone explain it to me? Thanks

#include <Servo.h>

#define NUM_ENTRIES(ARRAY)      (sizeof(ARRAY) / sizeof(ARRAY[0]))

const uint8_t   pinBUTTON       = 7;

const uint8_t   BUTTON_UP       = LOW;
const uint8_t   BUTTON_DOWN     = HIGH;


const uint8_t   pinSERVO_1      = 9;
const uint8_t   pinSERVO_2      = 8;
const uint8_t   pinSERVO_3      = 4;
const uint8_t   pinSERVO_4      = 6;
const uint8_t   pinSERVO_5      = 5;

const uint8_t   pinsSERVOS[]    = { pinSERVO_1, pinSERVO_2, pinSERVO_3, pinSERVO_4, pinSERVO_5 };
const uint8_t   NUM_SERVOS      = NUM_ENTRIES(pinsSERVOS);

Servo servos[NUM_SERVOS];

void loop()
{
    int buttonState = digitalRead(pinBUTTON);

    Serial.println(buttonState);

    for ( size_t i = NUM_SERVOS; i--; )
    {
        servos[i].write(BUTTON_DOWN == buttonState ? 0 : 180);
    }
}

void setup()
{
    Serial.begin(9600);

    pinMode(pinBUTTON,INPUT);

    for ( size_t i = NUM_SERVOS; i--; )
    {
        servos[i].attach(pinsSERVOS[i]);
    }
}
servos[i].write(BUTTON_DOWN == buttonState ? 0 : 180);

That line either writes the servos to 0 or 180 degrees based on the state of the buttonState variable.

for ( size_t i = NUM_SERVOS; i--; )

That for loop is missing a condition. And size_t isn’t really the appropriate type to use here though it works because it is equivalent to int. And it seems to have an extra semicolon jammed in there.

for ( int i = NUM_SERVOS; i >= 0; i--)

Could use uint8_t there as well, but the scope is limited so int isn’t that wasteful.

I don’t know that I would use much of this person’s code.

Your version is more awkward and confusing than what I wrote which is missing nothing!

'size_t' is appropriate for all uses involving array index's, and it's not the same as 'int' as it's unsigned and provides a range great enough to span the memory of the architecture it runs within.

lloyddean:
Your version is more awkward and confusing than what I wrote which is missing nothing!

'size_t' is appropriate for all uses involving array index's, and it's not the same as 'int' as it's unsigned.

Can you explain how it is awkward or confusing? It's a normal for loop.

How would the for loop in the OP ever stop? Oh, I see, you're using the i-- as the middle condition so it will terminate when that returns 0. No, that's not confusing at all. Yeah, everyone write for loops like that. The fact that i in the for expression isn't the same value as i in the code block is totally apparent even to the raw newbie isn't it. Everyone should be able to follow that.

Given that the target audience is a newbie, shouldn't we try not to obfuscate the code like that? I mean, should the newbie really need to learn all the dark secrets of c in order to understand an example?

size_t is normally used for the size of variables or arrays in bytes. It works here, but it is a bit awkward to use in that way and provides absolutely no benefit over using int or unsigned int. Actually, it is wasting a byte compared to using a uint8_t.

We have a difference in opinion but that doesn't change the fact that what you said was incorrect.

lloyddean:
We have a difference in opinion but that doesn't change the fact that what you said was incorrect.

No problem. I get it. I didn't think the whole post increment thing all the way through.

EVERYONE HEAR THAT? I GOT ONE WRONG? LLOYDDEAN WAS RIGHT. HE CAUGHT ME. GREAT JOB DUDE. YAY FOR YOU OH GREAT ONE.

Now explain how my for loop is confusing or awkward. Especially compared to the one you wrote. Because I'm going to stick with you being absolutely incorrect on that.

Simply it's succinct, less code to write and get wrong.

'int' is signed and lessens the range of indexes available for those platforms that can use it.

And please, no need for theatrics ..., I simply disagreed with your assesment of the code.

lloyddean:
Simply it's succinct, less code to write and get wrong.

'int' is signed and lessens the range of indexes available for those platforms that can use it.

Less code to write doesn't always mean less to get wrong. It is much easier to see when the for loop with all three conditions will end than to go think about post-decrement and when will it be zero. Do you really think that the newbie will find that easier to understand and follow? Do you really think it is harder to mess up written that way? Really?

There are 5 servos in that array. Sure, if someone redoes this code for 256 servos then they'd need a larger type. But if you're going there, then why not make it unsigned long long. Even the unsigned int or size_t restricts the values to some level. Do you really think he's going to write this with more than 32,000 servos and actually need those values that the int can't get to. Really?

I write code on a larger range of platforms then the Arduino,

The theatrics were because your reply seemed to be more meant to point out that I had been wrong than to answer the question of how my for loop was "awkward" or "confusing".

Really, the fact that I got it wrong is more evidence for the version in the OP being the more confusing and awkward way to write that. I know my code, but it caught me off guard. It took a bit of thought. The normal for loop doesn't require that thought. It is pretty apparent where that one starts and stops just from looking at it.

But maybe you have a different definition of the word confusing. Maybe in your world confusing means, took a few more keystrokes. I don't think that's how anyone else defines confusing.

lloyddean:
I write code on a larger range of platforms then the Arduino,

*than

So. A for loop is a for loop on any platform written with C. The discussion at hand has very little to do with the fact that the code was written for an Arduino.

Still a matter of opinion.

Edit: Thanks for catching that for me!

I tried using the code, the servo moves when the button is not pressed and stops moving when it is. It`s kind of the opposite of what I want it to do, I tried switching the "HIGH" and "LOW" but it did the same thing. Also, I want the servos moving different degrees and then stopping at some point. So if you wouldn't mind stopping with the whole "awkward and confusing" thing, that would be great. Thanks

PS: I noticed your username "lloyddean" looked familiar so I looked back at my recent posts and realized your the one who gave me the code I put on this page, thanks for that, your a really helpful dude.