Adafruit Industries, Essential service and business: NYC – Executive Order 202.6 - Read more. Accepting essential orders - here's how.
0

Broken for loop lol
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Broken for loop lol

by singhm29 on Sun Dec 05, 2010 4:15 am

Strangly enough after trying to figure out why I kept getting input after 5 runs of my loop I realized somehow the loop control variable was being reset....but I dont understand why.

void loop ()
{
int i;
DateTime now;
for (i=0; i<5; i++)
{
now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.println(i);
delay(3000);
}

RTC.adjust(DateTime(10, 12, 6, 9, 22, 0));

}

Output
2010/1/1 12:0:0
0
2010/1/1 12:0:3
1
2010/1/1 12:0:6
2
2010/1/1 12:0:9
3
2010/1/1 12:0:12
4
2010/12/6 9:22:0
0
2010/12/6 9:22:3
1
2010/12/6 9:22:6
2

Why O why is this thing resetting?
singhm29
 
Posts: 49
Joined: Thu Dec 02, 2010 2:19 am

Re: Broken for loop lol

by stinkbutt on Sun Dec 05, 2010 4:17 am

It's not resetting. You're running your FOR loop inside of the loop() subroutine, a loop that runs forever and ever.

As for why it's only going to 4, that's because it's testing i < 5, not i <= 5. Which is reasonable considering that i starts at 0.
Red M&M, Blue M&M: They all wind up the same color

stinkbutt
 
Posts: 593
Joined: Wed Feb 17, 2010 2:40 am

Re: Broken for loop lol

by singhm29 on Sun Dec 05, 2010 4:48 am

stinkbutt wrote:It's not resetting. You're running your FOR loop inside of the loop() subroutine, a loop that runs forever and ever.

As for why it's only going to 4, that's because it's testing i < 5, not i <= 5. Which is reasonable considering that i starts at 0.


Hmm I understand that loop() runs forever but why cant I say run this 5x at the start and never again? (0-4) i should not be reinitialized to zero. I can see whats hapepneing now its exectutes 5x then goes back to the top of the loop() subroutine and goes back into the for loop resetting i back to 0. What would you change to have my desired effect?
singhm29
 
Posts: 49
Joined: Thu Dec 02, 2010 2:19 am

Re: Broken for loop lol

by richms on Sun Dec 05, 2010 6:50 am

So you just want it to do that once and then what is it supposed to do? Nothing?

You could put it in setup where things happen once, or you could put another loop at the end of loop() that just repeated indefinatly.

The execution goes back to the start of the loop() when it gets to the end, hence its name. Its being reset to 0 by the first part of your for( ) which is setting it to 0.

richms
 
Posts: 558
Joined: Tue Jan 20, 2009 3:05 am
Location: New Zealand

Re: Broken for loop lol

by mtbf0 on Sun Dec 05, 2010 9:41 am

if you want something to run only once at the beginning of your sketch, put it in setup().

if you would like your sketch to stop doing anything toss in the line

Code: Select all | TOGGLE FULL SIZE
  while (1);

it'll not really be doing nothing. just very little over and over again.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: Broken for loop lol

by singhm29 on Sun Dec 05, 2010 2:29 pm

Thanks very much for the advice guys glad i picked this up before I made my code more complex.
singhm29
 
Posts: 49
Joined: Thu Dec 02, 2010 2:19 am

Re: Broken for loop lol

by stinkbutt on Sun Dec 05, 2010 3:23 pm

Yeah, the simplest solution is to just take everything you had in the loop() function and move it to the setup() function.
Red M&M, Blue M&M: They all wind up the same color

stinkbutt
 
Posts: 593
Joined: Wed Feb 17, 2010 2:40 am

Please be positive and constructive with your questions and comments.