Hi guys and gals. I know this should be easy but what am I missing? All i want to do is shut off my DC motor after a certain amount of loops. I am trying to use the for loop but if I put the code in the void loop it never stops and if I put it into the setup loop it never starts? Can someone help please? Thank you all.
int enA = 9;
int in1 = 2;
int in2 = 3;
void setup() {
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
analogWrite(enA, 255);
}
void loop() {
for (int i = 0; i > 3; i++) {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
delay(500);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
delay(100);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
delay(500);
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
delay(100);
}
This for-loop does nothing. Literally nothing. It does not matter where you put it or if you delete it. Why? Because i starts at zero. Zero is not greater than 3. So the for-loop does not execute.
Perhaps you meant to put "< 3"? Then the for-loop would execute. In the first iteration, it would write LOW to both pins. But what would be the point of repeating that? They would already be LOW, so writing LOW to them a second or third time would have no effect at all.
That cannot be true. It cannot make any difference where you put that for-loop.
What do you mean by "setup loop". The setup() function only gets called once, each time the Arduino is reset, or powered up, or you upload your sketch.