Mega2560 Skips over "For Loops"

I have been having issues with a much larger project that I've been trying to diagnose with strange outputs. I finally have narrowed it down and I have realized that my Arduino is not recognizing "for loops." It just skips right through them and performs no action. So, to test my theory, I got rid of the entire program, and made this simple sketch....

int x;

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

void loop()
{
for (x=0; x > 5000; x++)
{
Serial.println(x);
delay(200);
}

}

The output of this sketch is absolutely nothing. It skips over the for loop and does nothing. So, I added a little line at the end of the sketch just to make sure there is serial communications.

Serial.Println("testing");

That is the last line before the end of the loop. The output is a continuous stream of "testing" but it completely has skipped over the for loop. I then thought I'd outsmart the software and do what I want to do inside of a do/while loop.

So my new test sketch looked like...

int x;

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

void loop()
{
do
{
Serial.println(x);
x++;
delay(200);
}while x<5000
Serial.println("testing");

}

The output from that sketch looks like this...

0
testing
1
testing
2
testing
3
testing....

That makes no sense. It should stay in the do/while loop until it reaches 5000. I have other parts of my program that use the do/while loop and they work normally. Am I doing something wrong here? This a very simple task and I'm even embarrassed to post this here because it is a very basic programming concept. By the way, I have now tried this on two separate MEGA boards with the exact same results. I upgraded my software tot he 1.0.2 with the same results. I cannot complete my project without some sort of a loop, and this makes no sense to me. Any insight would be much appreciated.

The test in the for loop is backwards. The do-while loop specimen should have its conditional test in (parens).

-br

I think you have the >5000 the wrong way round..., should be <5000

Edit... just like br said 8)

Ooops...

The lack of parentheses in the while statement was a simple typo. I was hurrying when I typed this. My sketch actually had it correctly.

Ugh...

I see my mistake in the for loop now. Thanks for pointing that out. I'm embarrassed that I have made such a simple mistake. Stupid stupid stupid. Excuse my retardation.

But did you change the direction of the < vs > in the for?- remember that the block is only executed while the condition is true, so when you make it 0, and test to see if it's >5000, that's false so it leaps out straightaway.

Edit... we posted at the same time. Glad to help. The simplest mistakes are the hardest to spot!

No kidding... I'm embarrassed that I was stumped by such a simple thing. I was thinking backwards the whole time and no matter how many times I looked at it, I kept telling myself... "Nope, that's right." such a simple deal... ugh...

Can't beat Basic for this more "English" code:

FOR i = 1 TO 10
REM do stuff
NEXT

I've never been a C fan; that comes from having started on Fortran I suppose.

JimboZA:
Can't beat Basic for this more "English" code:

FOR i = 1 TO 10

REM do stuff
NEXT




I've never been a C fan; that comes from having started on Fortran I suppose.

But English is a terrible language to program computers with, just too vague and non-logical! :wink:

Lefty

.... for a Californian hippy maybe, Lefty :stuck_out_tongue:

and for loops in basic become nightmarish if you have to embed two or 3 of them

JimboZA:
.... for a Californian hippy maybe, Lefty :stuck_out_tongue:

Nah, all the hippies left in the 70s, only .com millionaires live here now. :wink:

Lefty