Pages: [1]   Go Down
Author Topic: Mega2560 Skips over "For Loops"  (Read 537 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

-br
Logged

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 99
Posts: 4404
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Edit... just like br said  smiley-cool
Logged

Roy from ITCrowd: Have you tried turning it off and on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 99
Posts: 4404
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
« Last Edit: December 08, 2012, 01:18:39 pm by JimboZA » Logged

Roy from ITCrowd: Have you tried turning it off and on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 99
Posts: 4404
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Roy from ITCrowd: Have you tried turning it off and on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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!  smiley-wink


Lefty
 
Logged

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 99
Posts: 4404
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

.... for a Californian hippy maybe, Lefty  smiley-razz

Logged

Roy from ITCrowd: Have you tried turning it off and on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

.... for a Californian hippy maybe, Lefty  smiley-razz



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

Lefty
Logged

Pages: [1]   Go Up
Jump to: