0

New to Arduino here. I was wondering if there was anything wrong about my utilization of the nested 'for loop'. What i wanted is the execution of the 'looper4' and 'looper6' function if any of my Togglestates condition are met during the looping process. My Togglestates reads the state of my Togglepins which are linked to my toggle switches. Funny enough, the program executes the 'looper6' function immediately when i flip the switch. However, does not execute the 'looper4' function until the whole nested 'for loops' are finished even when the switch is flipped. All my outputs are to LEDs.

int ledPins[] = { 14, 15, 16, 17, 18, 19, 20, 21, 22, 24, 26, 28};
int pinCount = 12;       
int patternCount = 24;
int ledLevel = 0; 
int Togglepin = 7;
int Togglestate = 0;
int Togglepin2 = 2;
int Togglestate2 = 0;
int Togglepin3 = 4;
int Togglestate3 = 0;


void setup() 
 {              

  for(int thisPin = 0; thisPin < pinCount; thisPin++)
    {
      pinMode(ledPins[thisPin], OUTPUT);
    }
  for(int thisPin = 0; thisPin < pinCount; thisPin++)
    {
      pinMode(ledPins[thisPin], OUTPUT);
    }
      pinMode(Togglepin, INPUT);
      pinMode(Togglepin2, INPUT);
      pinMode(Togglepin3, INPUT);
  }

 void loop()
 {
 looper3();  
 }


 void looper() //All my loopers(1,2,6,7) execute a similar coding with  
                 difference in arrays and toggling conditions(found below)
 {               

  byte AOM0[]={0,0,0,0,0,0,0,0,0,0,0,0}; 
  byte AOM1[]={1,1,1,1,1,1,1,1,1,1,1,1}; 
  byte AOM2[]={1,1,1,1,1,1,1,1,1,1,1,1}; 
  byte AOM3[]={1,1,1,1,1,1,1,1,1,1,1,1}; 
  byte AOM4[]={1,1,1,1,1,1,1,1,1,1,1,1}; 
  byte AOM5[]={1,1,1,1,1,1,1,1,1,1,1,1}; 
  byte AOM6[]={1,1,1,1,1,1,1,1,1,1,1,1}; 
  byte AOM7[]={1,1,1,1,1,1,1,1,1,1,1,1}; 
  byte AOM8[]={1,1,1,1,1,1,1,1,1,1,1,1}; 
  byte AOM9[]={1,1,1,1,1,1,1,1,1,1,1,1}; 
  byte BOM0[]={1,1,1,1,1,1,1,1,1,1,1,1}; 
  byte BOM1[]={1,1,1,1,1,1,1,1,1,1,1,1}; 
  byte BOM2[]={1,1,1,1,1,1,1,1,1,1,1,1}; 
  byte BOM3[]={1,1,1,1,1,1,1,1,1,1,1,1}; 
  byte BOM4[]={1,1,1,1,1,1,1,1,1,1,1,1}; 
  byte BOM5[]={1,1,1,1,1,1,0,0,0,0,0,0};
  byte BOM6[]={1,1,1,1,1,1,1,1,1,1,1,1}; 
  byte BOM7[]={1,1,1,1,1,1,0,0,0,0,0,0}; 
  byte BOM8[]={1,1,1,1,1,1,1,1,1,1,1,1}; 
  byte BOM9[]={1,1,1,1,1,1,0,0,0,0,0,0}; 
  byte COM0[]={1,1,1,1,1,1,1,1,1,1,1,1}; 
  byte COM1[]={1,1,1,1,1,1,0,0,0,0,0,0}; 
  byte COM2[]={1,0,1,0,1,0,1,0,1,0,1,0}; 
  byte COM3[]={1,1,1,1,1,1,1,1,1,1,1,1}; 

  byte* All[24] = { AOM0, AOM1, AOM2, AOM3, AOM4, AOM5, AOM6, AOM7,
                    AOM8, AOM9, BOM0, BOM1, BOM2, BOM3, BOM4, BOM5,
                    BOM6, BOM7, BOM8, BOM9, COM0, COM1, COM2, COM3 };

    for (int Pattern = 0; Pattern < patternCount; Pattern++)
      {
      delay(500);
        for (int thisPin = 0; thisPin < pinCount; thisPin++)
          {
             Togglestate3 = digitalRead(Togglepin3);
             Togglestate = digitalRead(Togglepin); 
             digitalWrite(ledPins[thisPin],All[Pattern][thisPin]);
             if (Togglestate3 == HIGH && Togglestate == HIGH)
                 {
                for (int thosePin = 0; thosePin < pinCount; thosePin++)
                    {
                    digitalWrite(ledPins[thosePin],LOW);
                    }

                 looper4();

                 }
             else if (Togglestate3 == HIGH && Togglestate == LOW)
                 {
                for (int thatPin = 0; thatPin < pinCount; thatPin++)
                    {
                    digitalWrite(ledPins[thatPin],LOW);
                    }
                 looper6();

                 }


             }
        }
  }

These are the functions that i used to read and toggle between the different 'looper' functions.

 void looper3()

 {
  Togglestate = digitalRead(Togglepin);

       if(Togglestate == HIGH)
         {
         looper4();
         }
        else if(Togglestate == LOW)
         {
         looper5();  
         }

 }

 void looper4()

 {
  Togglestate2 = digitalRead(Togglepin2);
  Togglestate = digitalRead(Togglepin);

         if(Togglestate2 == HIGH && Togglestate == HIGH)
          {
          looper1();
          }
         else if(Togglestate2 == LOW && Togglestate == HIGH)
          {
          looper2();  
          }

  }

 void looper5()

 {
  Togglestate3 = digitalRead(Togglepin2);
  Togglestate = digitalRead(Togglepin);

         if(Togglestate3 == HIGH && Togglestate == LOW)
          {
          looper6();
          }
         else if(Togglestate3 == LOW && Togglestate == LOW)
          {
          looper7();  
          }

  }

Here is the toggling conditions for all my looper functions

 // Togglestate is the toggle switch i use to interchange between my 2 other          
    toggle switches

 Looper1() - if (Togglestate2 == LOW && Togglestate == LOW) -> looper5();
           - if (Togglestate2 == LOW && Togglestate == HIGH) -> looper2();

 Looper2() - if (Togglestate2 == HIGH && Togglestate == LOW) -> looper5();
           - if (Togglestate2 == HIGH && Togglestate == HIGH) -> looper1();

 Looper6() - if (Togglestate3 == LOW && Togglestate == HIGH) -> looper4();
           - if (Togglestate3 == LOW && Togglestate == LOW) -> looper7();

 Looper7() - if (Togglestate3 == HIGH && Togglestate == HIGH) -> looper4();
           - if (Togglestate3 == HIGH && Togglestate == LOW) -> looper6();

This would be pretty much the whole program. I appreciate and welcome any response. Thanks!

1

You can nest for loops fine. What you cannot do is neat for loops in loops where you name the variables the same thing.

For thisPin ... 
   For thisPin ...
   For thisPin...

It gets the compiler confused - or at the very least it gets me confused. You can use the same have if the loops are next to each other, but not if they are nested:

For thisPin...
    For otherPin...
    For otherPin...

Also, without seeing your whole code I cannot be sure, but you may be suffering from unexpected recursion. This is where function a calls function b which calls function a again. With no limiting factor on the call depth you end up with a stack a mile high, or on chips with a limited hardware stack you loose lots and lots of old stack data and can never get back again.

3
  • Thank you for your response. I have amended my code and replaced the loops that are next to each other with a differently declared int variable. However, it seems that the program still waits for the for loops to execute finish before transiting to the allocated function (looper4, looper5). Nov 5 '16 at 11:43
  • 1
    It does look very much like you have infinite recursion going on there. Looper1 calls looper5, looper5 calls looper1, looper1 calls looper5, etc. I think you need to re-think your program design completely - maybe consider using a Finite State Machine instead of all these loops calling functions calling functions calling functions calling .... etc
    – Majenko
    Nov 5 '16 at 12:55
  • At the moment it's in effect like you're asleep dreaming that you're asleep having a dream about being asleep and dreaming that you are asleep, etc... When you wake up (if you ever do) how do you know you have really woken up and not still in a dream?
    – Majenko
    Nov 5 '16 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.