Offline
Newbie
Karma: 0
Posts: 5
|
 |
« on: December 08, 2012, 01:03:04 pm » |
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
God Member
Karma: 37
Posts: 974
Get Bitlash: http://bitlash.net
|
 |
« Reply #1 on: December 08, 2012, 01:07:46 pm » |
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
Offline
Edison Member
Karma: 34
Posts: 1705
|
 |
« Reply #2 on: December 08, 2012, 01:10:39 pm » |
I think you have the >5000 the wrong way round..., should be <5000 Edit... just like br said 
|
|
|
|
|
Logged
|
IT Crowd: Roy... "Have you tried turning it off and on again?" Moss.. "Have you tried forcing an unexpected reboot?"
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #3 on: December 08, 2012, 01:12:37 pm » |
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
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #4 on: December 08, 2012, 01:15:14 pm » |
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
Offline
Edison Member
Karma: 34
Posts: 1705
|
 |
« Reply #5 on: December 08, 2012, 01:16:42 pm » |
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
|
IT Crowd: Roy... "Have you tried turning it off and on again?" Moss.. "Have you tried forcing an unexpected reboot?"
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 5
|
 |
« Reply #6 on: December 08, 2012, 01:23:55 pm » |
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
Offline
Edison Member
Karma: 34
Posts: 1705
|
 |
« Reply #7 on: December 08, 2012, 01:30:27 pm » |
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.
|
|
|
|
|
Logged
|
IT Crowd: Roy... "Have you tried turning it off and on again?" Moss.. "Have you tried forcing an unexpected reboot?"
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 279
Posts: 15314
Measurement changes behavior
|
 |
« Reply #8 on: December 08, 2012, 01:46:37 pm » |
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!  Lefty
|
|
|
|
|
Logged
|
|
|
|
|
Johannesburg UTC+2
Offline
Edison Member
Karma: 34
Posts: 1705
|
 |
« Reply #9 on: December 08, 2012, 01:48:41 pm » |
.... for a Californian hippy maybe, Lefty 
|
|
|
|
|
Logged
|
IT Crowd: Roy... "Have you tried turning it off and on again?" Moss.. "Have you tried forcing an unexpected reboot?"
|
|
|
|
SE USA
Offline
Faraday Member
Karma: 33
Posts: 3623
@ssh0le
|
 |
« Reply #10 on: December 08, 2012, 01:49:38 pm » |
and for loops in basic become nightmarish if you have to embed two or 3 of them
|
|
|
|
|
Logged
|
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
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 279
Posts: 15314
Measurement changes behavior
|
 |
« Reply #11 on: December 08, 2012, 01:56:48 pm » |
.... for a Californian hippy maybe, Lefty  Nah, all the hippies left in the 70s, only .com millionaires live here now.  Lefty
|
|
|
|
|
Logged
|
|
|
|
|
|