loop not stopping

I am making a for loop and it compiles correctly but does not stop looping , I am following an online tutorial but some of what they show compiles incorrectly , also my “for” in the loop does not change to orange is that normal ?

if I put a open curly bracket at the end of the for loop before loop code as in the tutorial it compiles incorrectly if I remove it as I show here it compiles correctly but does not run .

// put your setup code here, to run once:
const int forwards = 7;
const int backwards = 6; //assign relay INx pin to arduino pin
int number=2; // number of cuts before stop

void setup() {

pinMode(forwards, OUTPUT); //set relay as an output
pinMode(backwards, OUTPUT); //set relay as an output

}

void loop() {

for (int j=1; j<=number; j=j+1)

digitalWrite(forwards, LOW);
digitalWrite(backwards, HIGH); //Activate the relay one direction, they must be different to move the motor
delay(1000); // wait 2 seconds

digitalWrite(forwards, HIGH);
digitalWrite(backwards, HIGH); //Deactivate both relays to brake the motor
delay(500);// wait 2 seconds

digitalWrite(forwards, HIGH);
digitalWrite(backwards, LOW); //Activate the relay the other direction, they must be different to move the motor
delay(1000);// wait 2 seconds

digitalWrite(forwards, HIGH);
digitalWrite(backwards, HIGH); //Deactivate both relays to brake the motor
delay(1000);// wait 2 seconds

}

Put the code to be executed within the for loop in a pair of curly brackets otherwise only the first line after the for loop initial statement will be executed as many times as set for the for loop.

i tried that , like this… assuming this is what you mean and it does not compile

// put your setup code here, to run once:
const int forwards = 7;
const int backwards = 6; //assign relay INx pin to arduino pin
int number=2; // number of cuts before stop

void setup() {

pinMode(forwards, OUTPUT); //set relay as an output
pinMode(backwards, OUTPUT); //set relay as an output

}

void loop() {

for (int j=1; j<=number; j=j+1) { // added curly bracket here as per your instruction or is this the wrong place ?

digitalWrite(forwards, LOW);
digitalWrite(backwards, HIGH); //Activate the relay one direction, they must be different to move the motor
delay(1000); // wait 2 seconds

digitalWrite(forwards, HIGH);
digitalWrite(backwards, HIGH); //Deactivate both relays to brake the motor
delay(500);// wait 2 seconds

digitalWrite(forwards, HIGH);
digitalWrite(backwards, LOW); //Activate the relay the other direction, they must be different to move the motor
delay(1000);// wait 2 seconds

digitalWrite(forwards, HIGH);
digitalWrite(backwards, HIGH); //Deactivate both relays to brake the motor
delay(1000);// wait 2 seconds

}

The curly braces must enclose everything you want done in the for() loop. An example of the required structure.

why does the example show a double line of curly brackets at the end , is this needed on a for loop ?

if i open and close with curly brackets and add a second curly to the last line it compiles but it still forever loops

You do realize that loop() loops, right?

Yes it forever loops. That’s what the loop() function does. If you DON’T want it to loop forever, there are a few ways to accomplish this:

  1. Put your code in the setup() function instead. The setup() function only gets executed once.

  2. Put the code you don’t want to loop forever into a conditional (if) block, and after it gets done executing the number of times you want, let that condition fail.

void loop() {
  static int loopCount = 0;  // static means this variable will keep its value when loop() returns

  if (loopCount < 10) { // only do this stuff 10 times
    loopCount = loopCount + 1;
    // Any code here will only execute 10 times
  }
}
  1. Put an infinite forever loop that does nothing where you want the code to stop.
void loop() {
// do stuff here only once
  for(;;);  // This forever loop will "hang" the program.
}

thanks I will try that out tomorrow. i was follow a sketch that used "loop" and an "if" statments inside it and only looped the stated amount . i used their exact sketch and added my loop statement. not sure why mine does not work, but theirs did .

Your code does what you’ve asked it to do. It loops round the for loop twice then it goes back to the top of loop() and loops round the for loop twice again and again and again and…

If you really want it to power on, do something twice and then never do anything ever again it’s best to put the something in setup() and leave loop() empty.

Steve