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!