loops / for /while/ect

/* the pin 13 led is set to low because even if no set up is made for it, it will still light. So to make it go out I have to put it in the program and set it to LOW.

The other const int’s , ints,PinModes, are there because this is only part of a larger program that works just fine except anything doing with a loop. I modified my program and removed the semicolon from behind the for statement but that did not work.
The loop will not exit the LEDS’s continue to blink.
*/

Const int buttonPin1=2;

Const int buttonPin2=2;

Const int buttonPin3=2;

Const int buttonPin4=2;

Const int MototPIN1=8;

Const int MototPIN2=9;

Const int MototPIN3=10;

Const int MototPIN4=11;

Const int led=13;

Int buttonState1=0;

Int buttonState2=0;

Int buttonState3=0;

Int buttonState4=0;

Int X=0;

Void setup ( ) {

pinMode(MotorPin1,output);

pinMode(MotorPin2,output);

pinMode(MotorPin3,output);

pinMode(MotorPin4,output);

pinMode(buttonPin1,input);

pinMode(buttonPin2,input);

pinMode(buttonPin1,input);

pinMode(buttonPin1,input); }

void loop ()

{

Digitalwrite (led,LOW);

buttonState1=Digitalread(buttonPin1);

buttonState2=Digitalread(buttonPin2);

buttonState3=Digitalread(buttonPin3);

buttonState4=Digitalread(buttonPin4);

for (X=0;X<5:X++)

{digitalwrite (MototPin1,High); // I expect the led’s to blink 5 times and exit the loop but the continue to blink forever

digitalwrite (MototPin2,High);

delay (1000);

digitalwrite (MototPin3,LOW);

digitalwrite (MototPin4,LOW);

delay (1000);

}}

Reply, Reply All or Forward | More

And, presumably, you put that code in the loop() function which is executed over and over, forever and anon.
If you want to stop your code executing just add
while(1);
at the end.

Pete
P.S. it is etc. - not ect.

The problem is a misplaced semicolon.

This code:

for( X=0;X<5;X++);

Is equivalent to

for( X=0;X<5;X++)
{
}

The semicolon at the end of the for statement makes the for loop do NOTHING in it's body. It runs an empty loop 5 times, and then the code under it runs once. Then the code drops to the end of your loop() function and the process starts again.

Get rid of the extra semicolon:

for( X=0;X<5;X++)
{
  digitalwrite (led1,HIGH);
  delay (1000);
  digitalwrite (led1,LOW)
  delay(1000);
}

chuckrosser:
all of my simple programs work blink, buttons , delay, ect . except anything doing with a loop.
I expect this program to blink the led 5 times and exit the loop
If I write the program #

for( X=0;X<5;X++);
{
digitalwrite (led1,HIGH);
delay (1000);
digitalwrite (led1,LOW)
delay(1000);}

The leds will blink forever but will not exit the loop . I have tried while loops and do while loops the same thin happens.

el_supremo:
And, presumably, you put that code in the loop() function which is executed over and over, forever and anon.
If you want to stop your code executing just add
while(1);
at the end.

Pete
P.S. it is etc. - not ect.

???

you may want to re-think your advice here....

Indeed. Missed the obvious :slight_smile:

Pete