STOP {for loop} AND back to { main loop }

hello everyone
I have a very simple project, but I am unable to do something in code .. I want to stop the { for loop } if the condition is not met and go back to the { main loop }.. Can anyone help me with that?

int value=0;
byte Sensor=A1;
byte LED1=3;
byte LED2=5;
byte LED3=6;


void setup() {
Serial.begin(9600);
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(LED3, OUTPUT);
}


void loop() 
{
  
  value=analogRead(Sensor);
  Serial.println("Sensor value is :");
  Serial.println(value);
  
  if ((value)>120 && (value)<150)
  {
   for(int BL=0;BL<10;BL++)
   {
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,HIGH);
    digitalWrite(LED3,HIGH);
    delay(500);
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,LOW);
    digitalWrite(LED3,HIGH);
    delay(500);
    //From this point, if the value is still between 120 to 150, complete the code.. If it is not, go back to the main loop.
      if ((value)>120 && (value)<150) 
      {
      for(int BL=0;BL<30;BL++)
      {
     digitalWrite(LED2,LOW);
     digitalWrite(LED3,LOW);
      for(int ledVal = 0; ledVal <= 100; ledVal +=1) {// Fade in
     analogWrite(LED1, ledVal);
     delay(15);
      }     
       for(int ledVal = 100; ledVal >= 0; ledVal -=1) { // Fade out
     analogWrite(LED1, ledVal);
     delay(30);
     } 
     delay(750); // Pause
    }
   }
  }
 }
}

You can check whether condition holds at the start of every iteration as follows.

for (int BL = 0; BL < 10 and condition; BL++)

If you want to exit the for-loop in the middle of an iteration, you can use break.

if (not condition) {
  break;
}

For nested for-loops, you could consider using a separate function and return whenever condition is not met.

I will apply this now..thanks

I seem unable to implement it.. I need help writing the code

There are a lot of online tutorials that will help you. Start with if, then else stufff.

You typed that, so the problem can't be that you can't type. What was your actual problem?

Stop confusing yourself with litter. You don't need to bracket individual variables.

litter !!! :face_with_raised_eyebrow:

I'm trying to keep it polite. :slight_smile:

You seem to have a lot of experience in the programming world to the point that you could see my bracket variables.. how about you help me solve the problem in my code :smiling_face:

The solution was already given in reply #2. You didn't reply to my direct question in reply #6, so I don't know what problem to apply my expertise to.

My question is that I don't want this {for loop} to work in all cases unless the value is still between 120 and 150..

That is, if the value is between 120 to 150, the first {for loop} is played directly for 10 seconds, and then if the value is still between 120 to 150, the second {for loop} is played, and if the value is not between 120 and 150, return to the main loop

I need the solution as soon as possible please :smiling_face_with_tear:

So the condition is value > 120 and value < 150, when the combine this with the first suggestion from post #2, we get:

for (int BL = 0; BL < 30 and value > 120 and value < 150; BL++)

I did that before.. The problem is that the second {for loop} works even if the value is different after the first.. because it depends on the value of the first..

I will give an example

if value = 0 to 120 or value = > 150
nothing happens .

if value = between 120 and 150

do this

for(int BL=0;BL<10;BL++)

It will take 10 seconds

Then
if value still = 120 to 150

do this

for(int BL=0;BL<30;BL++)

If the value changes and becomes not equal between 120 to 150 don't complete.
That's all i want.

I solved it.. I merged the while loop with for loop and it doing great with the addition of a new variable

int value=0;
byte Sensor=A1;
byte LED1=3;
byte LED2=5;
byte LED3=6;


void setup() {
Serial.begin(9600);
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(LED3, OUTPUT);
}


void loop() 
{
  
  value=analogRead(Sensor);
  Serial.println("Sensor value is :");
  Serial.println(value);
  
  while (value>120 && value<150){
  byte XY=10;
  for (int BL = 0; BL < 10; BL++)
  {
  digitalWrite(LED1,LOW);
  digitalWrite(LED2,40);
  digitalWrite(LED3,HIGH);
  delay(500);
  digitalWrite(LED1,LOW);
  digitalWrite(LED2,LOW);
  digitalWrite(LED3,HIGH);
  delay(500);
  XY++;
   if (XY >= 20) {
   value=analogRead(Sensor);
   if (value > 120 && value < 150 )
  {  
     for (int BL = 0; BL < 30; BL++) 
     {
      digitalWrite(LED2,LOW);
      digitalWrite(LED3,LOW);
      for(int ledVal = 0; ledVal <= 100; ledVal +=1) {// Fade in
       analogWrite(LED1, ledVal);
       delay(15);
      }     
       for(int ledVal = 100; ledVal >= 0; ledVal -=1) { // Fade out
       analogWrite(LED1, ledVal);
       delay(30);
       } 
       delay(750); // Pause
     }
    }
   }
  }
 }
}

I don't think so. Your code does not match the description of what you wanted to do.

Looks like you messed up the parenthesis.
In particular, you got a 30 cycles loop inside a loop of 10 cycles - that is, in fact, you got not 10 + 30 cycle passes, but 10 * 30 = 300 passes

The code Works perfect.. Cycle 30 will not work unless the condition XY>= 20 is met with the condition that the value is between 120 and 150.. And this is what I was trying to achieve..

XY = 10
after while loop
*XY=20
*if value between 120 and 150
Both conditions have been fulfilled
do Cycle 30
if value not between 120 and 150
evrything will stop
Because the condition for a while loop also requires the value 120 to 150

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.