help breaking out of for loop

I have a problem with my project . In this project I should stop my program through a button in a 'for' cycle . In my particular case I have to control the switching on and off a LED with a 'for' cycle . But my problem isn't the switching a led but stop the program whenever I want with a button . In the end when I push the button the program go out to the 'for' cycle . I push the button but the 'for' cycle cycling once again before stop moving I should stopped the cycle immediatly .

void loop() {
if(y==0){
digitalWrite(homing, HIGH);
delay(1000);
digitalWrite(homing, LOW);
delay(1000);
}
if (x==0)
{
delay(5000);
}
pinMode(homingm,INPUT); //homing manuale attraverso un pulsante
pinMode(start,INPUT); // start manuale attraverso pulsante
pinMode(st,INPUT); // stop manuale attraverso un pulsante
pinMode(pausa,INPUT); // pausa manuale attraverso un pulsante
x=1;
y=1;

if(digitalRead(i)!=0){
ilmn=ilmn+1;
}
if(digitalRead(l)!=0){
ilmn=ilmn+2;
}
if(digitalRead(m)!=0){
ilmn=ilmn+4;
}
if(digitalRead(n)!=0){
ilmn=ilmn+8;
}
if(digitalRead(o)!=0){
opqr=opqr+1;
}
if(digitalRead(p)!=0){
opqr=opqr+2;
}
if(digitalRead(q)!=0){
opqr=opqr+4;
}
if(digitalRead(r)!=0){
opqr=opqr+8;
}
ncicli= ilmn && opqr;

if(digitalRead(a)!=0){
abcd=abcd+1;
}
if(digitalRead(b)!=0){
abcd=abcd+2;
}
if(digitalRead(c)!=0){
abcd=abcd+4;
}
if(digitalRead(d)!=0){
abcd=abcd+8;
}
if(digitalRead(e)!=0){
efgh=efgh+1;
}
if(digitalRead(f)!=0){
efgh=efgh+2;
}
if(digitalRead(g)!=0){
efgh=efgh+4;
}
if(digitalRead(h)!=0){
efgh=efgh+8;
}
temperatura= abcd && efgh;

if((digitalRead(homingm)==HIGH) && (digitalRead(start)==LOW))
{
digitalWrite(homing, HIGH);
delay(1000);
digitalWrite(homing, LOW);
delay(1000);

}
if(digitalRead(start)==HIGH)
{
delay(1000);

if(digitalRead(st)==LOW)
{

for (int i=0; i <=5; i++){
if(digitalRead(st)==HIGH)//stop
{
ncicli=0;
i=5;
}
else
{
digitalWrite(homing, LOW);
digitalWrite(pos, HIGH);
delay(del);
digitalWrite(pos, LOW);
delay(del);
}
while(digitalRead(pausa)==HIGH)
{
delay(1000);
}
if(digitalRead(st)==HIGH) //stop
{
ncicli=0;
i=5;
}
}
}
}
}
void loop(1) {
if (del=5000){
{
digitalWrite(homing, HIGH);
delay(1000);
}
}

Hi, can you please read this first and then post your sketch using the tags mentioned in the post.
Also can you post the entire code, not just a portion of it, we need to see if it has been setup and variables declared.
Also a copy of your circuit, a CAD or picture of a hand drawn schematic would be fine.
We need to see how you have connected the buttons and LED.

http://forum.arduino.cc/index.php/topic,148850.0.html

Hope to help.

Tom...... :slight_smile:

int homing=2,pos=3,homingi=4,posi=5,homingm=6,start=7,st=8,pausa=9,a=10,b=11,c=12,d=13,e=22,f=24,g=26,h=28,i=30,l=32,m=34,n=36,o=38,p=40,q=42,r=44,abcd,efgh,ilmn,opqr,ncicli,temperatura,x,y,del=10000;
     
        void setup() {
              Serial.begin(9600);
              pinMode(homing,OUTPUT);    //homing di output
              pinMode(pos,OUTPUT);    //posizione del pistone quando si abbassa in output
              pinMode(posi,INPUT);    //restituisce la posizione del pistone se ha raggiuto quella posizione
              pinMode(homingi,INPUT);    //restituisce homing del pistone se ha raggiuto quella posizione
              pinMode(a,INPUT);              
              pinMode(b,INPUT);
              pinMode(c,INPUT);
              pinMode(d,INPUT);
              pinMode(e,INPUT);
              pinMode(f,INPUT);
              pinMode(g,INPUT);
              pinMode(h,INPUT);
              pinMode(i,INPUT);
              pinMode(l,INPUT);
              pinMode(m,INPUT);
              pinMode(n,INPUT);
              pinMode(o,INPUT);
              pinMode(p,INPUT);
              pinMode(q,INPUT);
              pinMode(r,INPUT);
       }
       
       void loop()  {
                   if(y==0){
         digitalWrite(homing, HIGH);
         delay(1000);
         digitalWrite(homing, LOW);
         delay(1000);
         }
         if (x==0)
         {
         delay(5000);          
         }
          pinMode(homingm,INPUT);    //homing manuale attraverso un pulsante         
              pinMode(start,INPUT);    // start manuale attraverso pulsante             
              pinMode(st,INPUT);    // stop manuale attraverso un pulsante          
              pinMode(pausa,INPUT);    // pausa manuale attraverso un pulsante            
            x=1;
            y=1;
         
          if(digitalRead(i)!=0){
         ilmn=ilmn+1; 
         }
          if(digitalRead(l)!=0){
         ilmn=ilmn+2;
         }
          if(digitalRead(m)!=0){
         ilmn=ilmn+4;
         }
          if(digitalRead(n)!=0){
         ilmn=ilmn+8;
         }
          if(digitalRead(o)!=0){
         opqr=opqr+1; 
         }
          if(digitalRead(p)!=0){
         opqr=opqr+2;
         }
          if(digitalRead(q)!=0){
         opqr=opqr+4;
         }
          if(digitalRead(r)!=0){
         opqr=opqr+8;
         }
         ncicli= ilmn && opqr;
         
          if(digitalRead(a)!=0){
         abcd=abcd+1; 
         }
          if(digitalRead(b)!=0){
         abcd=abcd+2;
         }
          if(digitalRead(c)!=0){
         abcd=abcd+4;
         }
          if(digitalRead(d)!=0){
         abcd=abcd+8;
         }
          if(digitalRead(e)!=0){
         efgh=efgh+1; 
         }
          if(digitalRead(f)!=0){
         efgh=efgh+2;
         }
          if(digitalRead(g)!=0){
         efgh=efgh+4;
         }
          if(digitalRead(h)!=0){
         efgh=efgh+8;
         }
         temperatura= abcd && efgh;
         
          if((digitalRead(homingm)==HIGH) && (digitalRead(start)==LOW))
       {
        digitalWrite(homing, HIGH);
        delay(1000);
        digitalWrite(homing, LOW);
        delay(1000);
   
       }  
       if(digitalRead(start)==HIGH)
       {
         delay(1000);
           
     if(digitalRead(st)==LOW)
       {
            
              for (int i=0; i <=5; i++){   
      if(digitalRead(st)==HIGH)
       {
         ncicli=0; 
         i=5;       
       }
        else
       {
           digitalWrite(homing, LOW);
           digitalWrite(pos, HIGH);
           delay(del);  
           digitalWrite(pos, LOW);
           delay(del);
          }
          while(digitalRead(pausa)==HIGH)
          {
            delay(1000);
            }  
          if(digitalRead(st)==HIGH)
       {
         ncicli=0; 
         i=5;
       }   
       }
       }
       }
       }
           void loop(1)  {
             if (del=5000){
             {
               digitalWrite(homing, HIGH);
               delay(1000);
             }
             }

Hi, which of the two threads you have started for the same problem, are you going to use?

Tom.... :slight_smile:

this

can you let the FOR loop finish, or do you have to break out instantly ?

I am also trying to understand the FOR loop. It seems that all the instructions that I have found so far only give the generic operation but do not detail the entire loop as if it were a sketch all by itself.

I had set up an IF with the FOR

if(toggle=0){ // only true if toggle=0
FOR( statement here)
IF (switch==HIGH) toggle=1 // this prevents the FOR from executing again.
}

for (int i=0; i <=5; i++){
if(digitalRead(st)==HIGH)//stop
{
ncicli=0;
i=5;
}
else
{
digitalWrite(homing, LOW);
digitalWrite(pos, HIGH);
delay(del);
digitalWrite(pos, LOW);
delay(del);
}
while(digitalRead(pausa)==HIGH)
{
delay(1000);
}
if(digitalRead(st)==HIGH) //stop
{
ncicli=0;
i=5;
}

...'for' cycle cycling once again before stop moving I should stopped the cycle immediatly.

If i = 5 the condition i <=5 for running loop is fullfilled then it runs once again. Put there i =6; or change condition to i < 5;
or use break; statement which is more effective code in fact.

can you let the FOR loop finish, or do you have to break out instantly ?

I am also trying to understand the FOR loop. It seems that all the instructions that I have found so far only give the generic operation but do not detail the entire loop as if it were a sketch all by itself.

I had set up an IF with the FOR

if(toggle=0){ // only true if toggle=0
FOR( statement here)
IF (switch==HIGH) toggle=1 // this prevents the FOR from executing again.
}

the loop should exit immediately when you press the button and i try this mode but not found and the program isn't complete
I still have to finish it

The best answer is to restructure your code to use a non-blocking architecture. The blocking architecture you use at the moment is simple to get started, but has the limitation that it makes it very difficult to control more than one thing at a time, such as blinking LEDs and monitoring buttons.

Compare the 'blink' and 'blink without delay' examples to see the difference between a blocking and non-blocking approach.