Go Down

Topic: Mega2560 Skips over "For Loops" (Read 562 times) previous topic - next topic

Pilotguy210

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.

billroy

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

-br

JimboZA

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

Edit... just like br said  8)
Arduino ethernet server here.... http://jimboza.gotdns.com:8085/

No PMs for help please

Pilotguy210

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.

Pilotguy210

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.

JimboZA

#5
Dec 08, 2012, 07:16 pm Last Edit: Dec 08, 2012, 07:18 pm by JimboZA Reason: 1
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!
Arduino ethernet server here.... http://jimboza.gotdns.com:8085/

No PMs for help please

Pilotguy210

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

JimboZA

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

Code: [Select]
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.
Arduino ethernet server here.... http://jimboza.gotdns.com:8085/

No PMs for help please

retrolefty


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

Code: [Select]
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!  ;)


Lefty

JimboZA

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

Arduino ethernet server here.... http://jimboza.gotdns.com:8085/

No PMs for help please

Osgeld

and for loops in basic become nightmarish if you have to embed two or 3 of them
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

retrolefty


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




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

Lefty

Go Up