Beginner question - For function / general question

Hi guys, totally new to programming, been playing around with this for a while, but there's a part I don't understand.

This is the 8-leds controlled by a shift register tutorial that comes with the arduino 2560 mega, and I understand about 99% of it, but one part I don't get.

Please correct me if I'm wrong, but this is how I'm understanding it:

We start by defining 'leds' as an 8 bit value. The 'for' function increments the variable 'i' by 1 as long as the value is less than 8 each time through the loop, and bitSet sets the relevant bit (defined by the variable i) from a zero to a 1 each time through.

So to start, leds is '00000000', all leds are off, then bitset sets bit 0 starting with the least significant bit, giving us '00000001'. Then the 'for' function increments 'i' by 1, so bitset will then set bit 1 as well, giving us '00000011'

This continues until all 8 bits are set '11111111', with all leds on.

Now the part I don't get. Once all bits are set and all LEDs are lit, the whole thing resets and starts over, and I don't see how that works

I think it's the 'for' function, but don't really understand how it works

for (int i = 0; i < 8; i++)

From what I've read, in plain english it says 'i = 0', 'if i is less than 8, then add 1'

As this function starts with i=0, I don't understand how it doesn't reset i to 0 each time through the loop, and also how it knows to start over at 0 once the number reaches 8, as the logic to me says that once it reaches 8, it should just stop incrementing, not reset to 0

If someone has the time to explain this to be I'd be very grateful...unfortunately I'm at the point where my knowledge is so basic, there are very few tutorials to cover something so simple.

Code below:

int latchPin = 5;
int clockPin = 6;
int dataPin = 4;

byte leds = 0;

void setup() 
{
 pinMode(latchPin, OUTPUT);
 pinMode(dataPin, OUTPUT);  
 pinMode(clockPin, OUTPUT);
}

void loop() 
{
 leds = 0;
 updateShiftRegister();
 delay(500);
 for (int i = 0; i < 8; i++)
 {
   bitSet(leds, i);
   updateShiftRegister();
   delay(500);
 }
}

void updateShiftRegister()
{
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, leds);
  digitalWrite(latchPin, HIGH);
}

Your for is embedded in loop, which will be called over and over.

You see the whole loop repeat, not only the for (or all leds should stay lit if the for would repeat magically)

The first time, when you enter the 'for' function, i is made 0.
The function does not return to this part of the compiled code when the looping happens.
The time after i was 0, i is incremented, then if the test is valid then code in {} executes.
When the test fails you leave the 'for' function.

.

for is not a function!

http://www.cplusplus.com/doc/tutorial/control/

Paulius2444:
I think it's the 'for' function, but don't really understand how it works

for (int i = 0; i < 8; i++)

From what I've read, in plain english it says 'i = 0', 'if i is less than 8, then add 1'

Not quite. It says:

start i with a value of zero (first expression) and
increment i every time it goes through the for loop (third expression)
until the 2nd expression is false (in your case i becomes something greater than or equal to eight).

I hope that helps.

I used word 'function' quit loosely.

But, you are right.

Maybe 'control structure' would have been better.

.

I think correct terminology matters a lot. :wink:

Okay, it's strange/confusing for beginners that for is a loop and loop is a function.

Thank you for all your answers, it makes sense now.

Unfortunately, my only experience of programming anything up until a few days ago was a bit of BASIC on my old C64 back in the 80's...so I was reading loop like a 'goto 10'

So, the i = 0 statement basically says to start at zero, not set i to zero...and when i > 8, the program exits out of the 'for', which causes it to start over next time through the loop.

Thanks again!