Exiting 'if' and 'for' loop on analogRead

Hello programmers! greetings from Kolkata, India.

Please take two minutes to read the situation.

The purpose here is to take input from 4 simple switches and a potentiometer (5 inputs in total) to light up 10 LEDs (similar to Bar Graph example). Each switch will make LED’s attain different height; such as switch 1 will glow 10 LEDs, switch 2 will glow 7 LEDs, switch 3 and 4 will glow 6 LEDs. The potentiometer will work like the Bar Graph example for upto 7 LEDs.

The program sketch works correctly, but i am stuck at a point:

My question is, how to exit the ‘if’ loop for one switch, on immediate pressing of another switch, also i want to stop the potentiometer sense after few seconds, and operate on further change of its value, which can also be interrupted by pressing any of the four switches.

I hope i could clear my situation.
Thank you for reading, too many thanks for willing to help me)))

//sketch

int S0=A0;
int S1=A1;
int S2=A2;
int S3=A3;
int S4=A4;
int del=60;
int del1=1500;
long prevtime=0;

const int ledCount = 10;
int ledPins[] = {
 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};

void setup() {
 // put your setup code here, to run once:

pinMode(S0, INPUT);
pinMode(S1, INPUT);
pinMode(S2, INPUT);
pinMode(S3, INPUT);
pinMode(S4, INPUT);

for (int thisLed = 0; thisLed < ledCount; thisLed++) {
   pinMode(ledPins[thisLed], OUTPUT);
 }

}

void loop() {  
 
int state0=digitalRead(S0);
int state1=digitalRead(S1);
int state2=digitalRead(S2);
int state3=digitalRead(S3);



  int sensorReading = analogRead(S4);
  int ledLevel = map(sensorReading, 0, 1023, 0, 7);
int ledLevelmem=0;  //unused, in a try to setup the quit statement

 // loop over the LED array:
 for (int thisLed = 0; thisLed <= 7; thisLed++) {
   // if the array element's index is less than ledLevel,
   // turn the pin for this element on:
   
   
   if (thisLed < ledLevel) {
     digitalWrite(ledPins[thisLed], HIGH);
   }
  else  {
   digitalWrite(ledPins[thisLed], LOW);

     }
 }
 
if (state0==1)  {
 
 for(int ht0=0; ht0<=9; ht0++)  {

   digitalWrite(ledPins[ht0],HIGH);
delay(del);

 if (state1==1)      //why this statement does not work?
 break;              //why this statement does not work?

  if (ht0==9){
    delay(del1); 
    for (ht0=9; ht0>=0; ht0--)  {
   digitalWrite(ledPins[ht0],LOW);
     
     
      }
      break;
  }
}
}

if (state1==1)  {
 
 for(int ht0=0; ht0<=6; ht0++)  {

   digitalWrite(ledPins[ht0],HIGH);
delay(del);
 
  if (ht0==6){
    delay(del1); 
    for (ht0=9; ht0>=0; ht0--)  {
   digitalWrite(ledPins[ht0],LOW);
     }
     
   break;   }
  }
}

if (state2==1)  {
 
 for(int ht0=0; ht0<=5; ht0++)  {

   digitalWrite(ledPins[ht0],HIGH);
delay(del);
 
  if (ht0==5){
    delay(del1); 
    for (ht0=9; ht0>=0; ht0--)  {
   digitalWrite(ledPins[ht0],LOW);
     }
     
   break;   }
  }
}

if (state3==1)  {
 
 for(int ht0=0; ht0<=5; ht0++)  {

   digitalWrite(ledPins[ht0],HIGH);
delay(del);
 
  if (ht0==5){
    delay(del1); 
    for (ht0=9; ht0>=0; ht0--)  {
  digitalWrite(ledPins[ht0],LOW);
     }
     
   break;   }
  }
}

}

ardprog.txt (2.28 KB)

how to exit the 'if' loop for one switch

There's no such thing.
Next question.

If you want to break out of a for loop, use "break;"

create functions that do a specific task.

Then it will be easier to read what you are doing, and also return; becomes an option.

If you just want to hack your if code, use a goto.
always use { } for if statement, this is not python, and you'll get burned.

I suspect the problem lies in your use of delay() - it appears in lots of places. During a delay() the Arduino can do nothing.

If you want a responsive program you must use millis() to manage timing. The technique is illustrated in several things at a time.

…R

AWOL:
There's no such thing.
Next question.

If you want to break out of a for loop, use "break;"

Is breaking out of a for loop acceptable practice? I am not a programmer although I did learn programming many many years ago and I was taught if you wanted to get out of a for loop before it finished you were using the wrong loop.

It can be OK in some circumstances. Say you have 100 things you have to check for a particular one. For example, a phone book, where you are searching for a name. Once you find it you are done, so "breaking" is acceptable.

      if (state1==1)      //why this statement does not work?

break;              //why this statement does not work?

How do you know it doesn't work? You are not re-acquiring state1 inside that loop.

so much thanks to all of you))

and extremely sorry for not being able to come online.

but hey, i made it work to a great extent, and all of your views and ideas helped me rethink the program.

but i’m still stuck to one point, how to stop the sense from analogRead (here a 220KOhms potentiometer) after a few seconds, and make it work again only after a change of its value.

here is the program:

int S0=A0;
int S1=A1;
int S2=A2;
int S3=A3;
int S4=A4;
int del=60;
int del1=1500;
long prevtime=0;

const int ledCount = 10;
int ledPins[] = {
  2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};

void setup() {
  // put your setup code here, to run once:

void alloff(void);
void alloffanalog (void);

pinMode(S0, INPUT);
pinMode(S1, INPUT);
pinMode(S2, INPUT);
pinMode(S3, INPUT);
pinMode(S4, INPUT);

for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT);
  }

}

void loop() {  
  

   int sensorReading = analogRead(S4);
   int ledLevel = map(sensorReading, 200, 800, 0, 6);
int ledLevelmem=7; 
unsigned long time;
unsigned long uptime;

  for (int thisLed = 0; thisLed <= 6; thisLed++) {
       
    if (thisLed <=ledLevel) {
      digitalWrite(ledPins[thisLed], HIGH);
      if (digitalRead(S0)==HIGH){     
 alloff();
 goto sw0;}
      if (digitalRead(S1)==HIGH){     
 alloff();  
goto sw1;}
  if(digitalRead(S2)==HIGH){
  alloff();  
  goto sw2;}
    if(digitalRead(S3)==HIGH){
     alloff();  
    goto sw3;}
    }
    
   else  {
    digitalWrite(ledPins[thisLed], LOW);
    if (digitalRead(S0)==HIGH){     
 alloff();
 goto sw0;}
    if (digitalRead(S1)==HIGH){     
 alloff();  
goto sw1;}
  if(digitalRead(S2)==HIGH){
   alloff();  
  goto sw2;}
    if(digitalRead(S3)==HIGH){
     alloff();  
    goto sw3;}

      }
      //time=millis();
      //uptime=time;
      if(time> (uptime+del1*5))  {
        uptime=time;
        alloffanalog();
        goto sw1;
        break;
      }
  }
  
  
sw0:{ 
 int ht0=0; 
if (digitalRead(S0)==HIGH)  {
  for(int ht0=0; ht0<=9; ht0++)  {
if (digitalRead(S1)==HIGH){     
 alloff();  
goto sw1;}
  if(digitalRead(S2)==HIGH){
   alloff();  
  goto sw2;}
    if(digitalRead(S3)==HIGH){
     alloff();  
    goto sw3;}
    
    digitalWrite(ledPins[ht0],HIGH);
delay(del);
 
   if (ht0==9){
     for(int i=0; i<=del1;i=i+10)
     {
     if (digitalRead(S1)==HIGH){
  alloff();     
  goto sw1;}
  if(digitalRead(S2)==HIGH){
  alloff();  
  goto sw2;}
    if(digitalRead(S3)==HIGH){
    alloff();  
    goto sw3;}
     delay(10); }
     alloff();
       //break;
   } 
  }
  }
}
sw1:{
 int ht1=0;
if (digitalRead(S1)==HIGH)  {
    for(int ht1=0; ht1<=6; ht1++)  {
    if (digitalRead(S0)==HIGH){
   alloff();
    goto sw0;}
  if(digitalRead(S2)==HIGH){
   alloff();  
  goto sw2;}
    if(digitalRead(S3)==HIGH){
     alloff();  
    goto sw3;}

    digitalWrite(ledPins[ht1],HIGH);
delay(del);
  
   if (ht1==6){
     for(int i=10; i<=del1; i=i+10){
       if (digitalRead(S0)==HIGH){
    alloff();     
  goto sw0;}
  if(digitalRead(S2)==HIGH){
  alloff();  
  goto sw2;}
    if(digitalRead(S3)==HIGH){
    alloff();  
    goto sw3;}
     delay(10);} 
     alloff();
      
   // break;  
  }
   }
  }
}

sw2:{
  int ht2=0;
if (digitalRead(S2)==HIGH)  {
  for(int ht2=0; ht2<=5; ht2++)  {
    if (digitalRead(S1)==HIGH){     
   alloff();
    goto sw1;}
  if(digitalRead(S0)==HIGH){
   alloff();  
  goto sw0;}
    if(digitalRead(S3)==HIGH){
     alloff();  
    goto sw3;}

    digitalWrite(ledPins[ht2],HIGH);
delay(del);
  
   if (ht2==5){
     for(int i=0;i<=del1;i=i+10){
     if (digitalRead(S1)==HIGH){     
   alloff();
     goto sw1;}
  if(digitalRead(S0)==HIGH){
   alloff();  
  goto sw0;}
    if(digitalRead(S3)==HIGH){
     alloff();  
    goto sw3;}
     delay(10); }
     alloff();
      
    //break;
   }
}
}
}
sw3:{
int  ht3=0;
if (digitalRead(S3)==HIGH){  
  for(int ht3=0; ht3<=5; ht3++)  {
    if (digitalRead(S1)==HIGH){     
   alloff();
    goto sw1;}
  if(digitalRead(S2)==HIGH){
   alloff();  
  goto sw2;}
    if(digitalRead(S0)==HIGH){
     alloff();  
    goto sw0;}

    digitalWrite(ledPins[ht3],HIGH);
delay(del);
  
   if (ht3==5){
     for(int i=0;i<=del1;i=i+10){
        if (digitalRead(S1)==HIGH){     
   alloff();
        goto sw1;}
  if(digitalRead(S2)==HIGH){
 alloff();
    goto sw2;}
    if(digitalRead(S0)==HIGH){
     alloff();  
    goto sw0;}
     delay(10); }
    alloff();
      
    //break;   
     }
    }
   }
  }
  
  
}

void alloff(void)
{
  digitalWrite(ledPins[0],LOW);
  digitalWrite(ledPins[1],LOW);
  digitalWrite(ledPins[2],LOW);
  digitalWrite(ledPins[3],LOW);
  digitalWrite(ledPins[4],LOW);
  digitalWrite(ledPins[5],LOW);
  digitalWrite(ledPins[6],LOW);
  digitalWrite(ledPins[7],LOW);
  digitalWrite(ledPins[8],LOW);
  digitalWrite(ledPins[9],LOW);

}

void alloffanalog (void)
{
  analogWrite (ledPins[0], 0);
  analogWrite (ledPins[1], 0);
  analogWrite (ledPins[2], 0);
  analogWrite (ledPins[3], 0);
  analogWrite (ledPins[4], 0);
  analogWrite (ledPins[5], 0);
  analogWrite (ledPins[6], 0);
  analogWrite (ledPins[7], 0);
  analogWrite (ledPins[8], 0);
  analogWrite (ledPins[9], 0);
}
//void ledon(void)
//{
  //for(int count=0; count<=limit;count++){
  //digitalWrite(ledPins[count],HIGH);
  //delay (del);
  //}
//}

Not sure what you are saying.
Try a 10K pot.

goto
Never use this as it results in spaghetti code.

thanks LarryD for viewing the post.

the value of potentiometer is insignificant here as its reading(input voltage) has "map" function, hence whatever the potentiometer i use, there should not be any change in the operation of the arduino board.

goto works perfectly well in this program, as i wanted it to.

never came across "spaghetti code" term, please be specific what you want to mean.

A potentiometer of 250K ohms may cause problems.
The analog input works best at a resistance <= 10K

Spaghetti code jumps all over the place and is very hard to follow and debug.
Serious programmers do not use it.
1 month after you get things to work you will forget how the program flows.
It makes it impossible for others to follow and comment on what you are doing.

I strongly suggest you train yourself out of using goto. You will thank us for it later.

psychespring:
never came across "spaghetti code" term, please be specific what you want to mean.

Thankfully, Wikipedia has an article about it: Spaghetti code

It is named such because program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled.

how to stop the sense from analogRead (here a 220KOhms potentiometer) after a few seconds, and make it work again only after a change of its value.

Save the previous reading to a variable and compare it with the current reading. If it has not changed (see below ***) do nothing. If you want to introduce a time element then before each reading you need to check whether the value of millis() has increased beyond the required period since the timing started.

*** The value is likely to have some jitter in its value, so in practice you need to check whether it has changed by a value exceeding some threshold.

@psychespring - I'm going to stick my neck out here, and risk being reported to the moderators.

Your code is an unreadable mess. First, the IDE has an auto-formatting tool. Learn to use it.

The indentation is atrocious. I can't even begin to work out what you are doing, and I have been programming for many years. Use some spaces.

From what I can make out (and it isn't easy) you could probably turn a lot of it into functions.

Read this: Write-only language.

You may understand what you are doing today, but in a month? Will it really still make sense?

Take the time and effort to write clearly and neatly. In fact, it doesn't take longer, because you can save time by turning things that are done often into functions. And also, the time you save by spewing out unreadable code is lost when you spend the next month trying to make it work.

[quote author=Nick Gammon link=msg=2305226 date=1436166044]
@psychespring - I'm going to stick my neck out here, and risk being reported to the moderators.
[/quote] Lucky for you that the moderators don't have that option :smiley:

psychespring:
but i’m still stuck to one point,

I suspect more than one …
You seem to be using GOTO all over your program.

NEVER use GOTO - it just creates a mess. (unless you are good enough not to need help)

Have a look at how to break a program into functions in planning and implementing a program

…R

I use gotos in my code but that's not how you use them.

You need to implement functions, each doing a clear task and call them. Your gotos pretty much seem to replace function calls in your code and makes it unreadable for anyone but you. And you won't be able to read it in 6 month either. :slight_smile: