Hello there, I tried to rewrite the loop iteration example code on arduino according to my understanding. Please correct
int timer = 100;
int thisPin = 2;
void setup() {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
for (thisPin < 8; thisPin++) {
digitalWrite(thisPin, HIGH);
delay(timer);
digitalWrite(thisPin, LOW);
}
for thisPin < = 7; thisPin--) {
digitalWrite(thisPin, HIGH);
delay(timer);
digitalWrite(thisPin, LOW);
}
}
Please use [code][/code] tags and indent your code properly. You'll soon see some obvious mistakes if you do that.
Couple of observations.
int thisPin = 2;
...
for (thisPin < 8; thisPin++) {
You initialise thisPin on the first line. Then in the for loop, you have left out the the usual initialise part in the brackets; you have just the test and increment parts: "thisPin < 8" and "thisPin++".
According to for - Arduino Reference, I believe this may be OK, but you may need to include an extra semicolon, like this:
for (; thisPin < 8; thisPin++) {
In the second for loop, you have:
for thisPin < = 7; thisPin--) {
Probably needs an opening bracket and maybe that semicolon. But I'm also wondering about when the loop will stop looping. thisPin starts at 8 after the previous loop? You then subtract one each time round this loop. And you keep going so long as "thisPin <= 7".
If thisPin starts out at 8, and the test is done before subtracting one from thisPin, the second loop will never run, because thisPin is already not <= 7.
If it starts at less than 8, the loop will carry on a long time. thisPin is declared as int, which can go negative, down to -32,768 I think. That will confuse the digitalWrite.
What values were you expecting thisPin to take? And was there a particular reason for not including all three parts in each for loop brackets, the initialise, test and increment / decrement?
All the best
Ray
The for:loops as written seem intended to vary thisPin from 0 to 7 while turning them on & off.
Only D2 is declared as an output tho, so the digitalWrite to 0,1,3,4,5,6,7 will just turn the internal pullup resistors off & on.