# If/else statement broken,and for-loop that will not stop after the set cycle #

This little motor and light are supposed to go off, the motor at a certain speed for a certain time. The machine is supposed to do this four times, with an interval in between each repetition.The for loop would not stop after the fourth repetition and just kept going. So I added an if/else statement in order to make it behave. but i get the error “else without a previous if.” So I have two broken loops and it’s thrown me for a loop. What did I do incorrectly?

`````` int motorPin = 10;
int ledPin = 3;
int repetitions;
int iterations = 0;

void setup()

{
int ledPin;
int motorPin;
pinMode (motorPin, OUTPUT);
pinMode(ledPin, OUTPUT);
int repetitions;
}
void loop()
{
if(iterations > 4);
{
analogWrite(motorPin, LOW);
analogWrite(ledPin, 0);
}
else
{

for(int repetitions=0;repetitions<5;repetitions=repetitions++)
{
delay(2000);
analogWrite(ledPin, 1000);
delay(2000);
analogWrite(motorPin, 70);
delay (3000);
delay(200);
analogWrite(ledPin, 0);
analogWrite(motorPin, LOW);
}
}
}
``````

A few think I see (although I don’t know if it will solve your problem…

analogWrite(ledPin, 1000);

First… the analog write function will only take a value between 0-255… So 1000 is out of the range.

for(int repetitions=0;repetitions<5;repetitions=repetitions++)

Second… I don’t know if it is correct (might be) but try changing repetitions=repetitions++ to just … repetitions++

int iterations = 0;

if(iterations > 4);

Third… Your set iterations to 0 then have an if statement looking for it to be > 4. Iterations is never incremented so it will never be > 4. Also the if statement shouldn’t have a semi-colon at the end.

Just going by the example on the website of a for-loop, yes, I should just write repetitions++, but I am supposed to set it = 0 because that gives it the number we start counting from. I could set it to = 2, and then I would only expect the machine to run twice to reach 4. And will fix the semicolon. Otherwise, It ought to cease and desist, but it keeps looping for some reason.

How about the if statement? What could be going wrong with that when I compile it?

What if I were to add this to the code: ?

``````Iterations++
``````

right in there with the LED and the motor at the very bottom of that section of code? Would the iterations start counting up?

Here is some modified code (removed the motor portions and used the LED on board(pin 13)) which does work appropriately

``````int ledPin = 13;
int iterations = 0;

void setup()
{
int ledPin;
pinMode(ledPin, OUTPUT);
int repetitions;
}
void loop()
{
if(iterations > 4)
{
analogWrite(ledPin, 0);
delay(10000);
iterations = 0;
}
else
{
for(int repetitions=0;repetitions<5;repetitions++)
{
delay(500);
analogWrite(ledPin, 100);
delay (500);
analogWrite(ledPin, 0);
}
iterations++;
}
}
``````

Things to remember… The for loop flashes the LED 5 times and this same for loop is in an if/else conditional statement which cycles 4 times before the if condition

if(iterations > 4)

is FALSE. This means it will flash the LED 20 times before shutting it off for 10 seconds.

The error you were getting about the “else” without and “if” was due to the ; after the if condition.

I could set it to = 2, and then I would only expect the machine to run twice to reach 4.

Actually... it would run 3 times.... 2, 3, 4.

Ok, so if I set repetitions to 4 and iterations to 1, that will give me the correct count?

I just ran my machine and I think it went like six times. let me see.....

** correction, nine times it runs now, and then it stops. Definitely an improvement.

How many cycles do you want?

Set repetitions = 0 in the for loop to start. Then have it run as many times as you need. Remember 0 counts. So…
for(repetitions = 0; repetitions < 100; repetitions++) will do whatever you want 100 times. 0,1,2,3,4…97,98,99. Once repetitions =100 the for loop stops because 100 is not < 100… it is equal to 100.

Why do you have that pointless declaration of repetitions in setup( ) ??

Touche.. Missed deleting that from his original post. XD

Because I want to cover all my bases and make dang sure it has all of its veriables, terms, definitions, and # counts initialized.

Ok, good news: It runs four times now like it should. Thanks a bunch.

The weird news: Iterations is set at 2. Don’t know why that works the way it does. But hey,

Here is the code:

``````int motorPin = 10;
int ledPin = 3;
int repetitions = 0;
int iterations = 0;

void setup()

{
int ledPin;
int motorPin;
pinMode (motorPin, OUTPUT);
pinMode(ledPin, OUTPUT);
int repetitions;
int iterations;
}
void loop()
{
if(iterations > 2)
{
analogWrite(motorPin, LOW);
analogWrite(ledPin, 0);
}
else
{

for(int repetitions=0;repetitions<4;repetitions++)
{
delay(2000);
analogWrite(ledPin, 1000);
delay(2000);
analogWrite(motorPin, 70);
delay (3000);
delay(200);
analogWrite(ledPin, 0);
analogWrite(motorPin, LOW);
iterations++;
}
}
}
``````

Thanks all. Much obliged.

** For those of posterity, it needed to be repetitions++ in the for statement, there was an unnecessary semicolon in front if the if statement, separating it from the else statement, and I may have needed to define and state the variables iterations and repetitions all the way up the program. Or something. Good luck.

Just so that you might understand....

The for loop increments "iterations" every time it cycles... Such is why you only have the 4 LED flashes. After the for loop finishes, Iterations = 4.

If you only need it to happen 4 times, just have the for loop and get rid of the if-else statement.

Overall, you need to do some research to understand the basics of loops and conditional statements better.

Good luck!

``````void setup()

{
int ledPin;
int motorPin;
pinMode (motorPin, OUTPUT);
pinMode(ledPin, OUTPUT);
int repetitions;
int iterations;
}
``````

Exactly which pins are you setting as OUTPUT? NOT pins 10 and 3. Local variables are not initialized, to ledPin and motorPin have random leftover crap in them. Rather useless code here.