Unexpected for loop behaviour

This code works perfectly fine however if I remove the comment markers for the second for loop and add into the main loop() i get the first for loop looping multiple times a second until the switch is high again.

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

const int switchPins[] = {2, 3, 4, 5};
int ranBefore[] = {0, 0, 0, 0, 0, 0, 0, 0};
boolean advanced = false;
int column = 1;
int switchStates[] {HIGH, HIGH, HIGH, HIGH};
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";


void setup() {
  Serial.begin(9600);
  for (int i = 0; i < 4; i++) {
    pinMode(switchPins[i], INPUT_PULLUP);
  }
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}


void loop() {
  // column always equals 1 throught my tests

  for (int i = 0; i < 4; i++) {
    switchStates[i] = digitalRead(switchPins[i]);
    if (advanced == false) {
      if (column == 1) {
        if (switchStates[i] == LOW && (ranBefore[i]) == 0) {
          Serial.println("Iteration");
          Serial.println(i);
          transmitFunc(i);
        }  else if (switchStates[i] == HIGH && ranBefore[i] == 1) {
          Serial.println("Iteration2");
          Serial.println(i);
          int switchter = i;
          transmitFuncOff(i);
        }
      }
    }

  }


  /*
    for (int i = 0; i < 4; i++) {
    switchStates[i] = digitalRead(switchPins[i]);
    if (advanced == false) {
      if (column == 2) {
        if (switchStates == LOW && (ranBefore[i + 4]) == 0) {
          Serial.println("Iteration");
          Serial.println(i);
          transmitFunc(i);
        }  else if (switchStates[i] == HIGH && (ranBefore[i + 4]) == 1) {
          transmitFuncOff(i + 4);
        }
      }

    }

    }
  */
  void transmitFunc(int number) {
    Serial.println("running");
    String variable;
    variable = String((number + 1));
    Serial.println("variable");
    Serial.println(variable);
    String Signal = "L" + variable;
    char text[] = "";
    Serial.println("almost still running");
    Signal.toCharArray(text, 3);
    radio.write(&text, 3);
    Serial.println("still running");
    String inst = String(text);
    Serial.println("insta");
    Serial.println(inst);
    // Serial.println(inst);
    delay(50);
    ranBefore[number] = (1);
    Serial.println(ranBefore[3]);
  }
  void transmitFuncOff(int numbero) {
    Serial.println("running2");
    String variable2;
    variable2 = String(numbero + 1);
    String Signal2 = "LO" + variable2;
    char text[] = "";
    Signal2.toCharArray(text, 4);
    radio.write(&text, 4);
    String inst2 = String(text);
    delay(50);
    ranBefore[numbero] = 0;
    Serial.println("ranbefore");
    Serial.println(ranBefore[3]);

  }

Updated code

One thing I notice in the 2nd FOR loop is that you are making reference to switchStates[i-4] when reading the switches, but in the follow IF statements are referencing switchStates[i]

Ahh yes I just noticed that. Earlier today i took some code from my main program and just added the relevant parts to this post its now been updated

Also some background info. What im trying to do with this part of the program is when column == 1 read the 4 switchstates and ranbefore variables true on the 0,1,2,3 indexes and the transmit function output l1 l2 l3 l4 but then when column == 2 i need to read the same switchstates but have ranbefore true on the 4,5,6,7 array indexes and have the transmit function output l5 l6 l7 and l8 respectively. If you need more clarification just ask but thats the best way i can describe it right now

PS. The switchstates are inverted as im using the internal pullup resistors of the arduino uno

Dev132:
What im trying to do with this part of the program is when column == 1 read the 4 switchstates and ranbefore variables true on the 0,1,2,3 indexes and the transmit function output l1 l2 l3 l4 but then when column == 2 i need to read the same switchstates but have ranbefore true on the 4,5,6,7 array indexes and have the transmit function output l5 l6 l7 and l8 respectively. If you need more clarification just ask but thats the best way i can describe it right now

Are you kidding?

The quoted unstructured word salad is far away from any clarification.

Dev132:
Also some background info. What im trying to do with this part of the program is when column == 1 read the 4 switchstates and ranbefore variables true on the 0,1,2,3 indexes and the transmit function output l1 l2 l3 l4 but then when column == 2 i need to read the same switchstates but have ranbefore true on the 4,5,6,7 array indexes and have the transmit function output l5 l6 l7 and l8 respectively. If you need more clarification just ask but thats the best way i can describe it right now

PS. The switchstates are inverted as im using the internal pullup resistors of the arduino uno

:o

Ok let me try again… I wrote that on my phone late.

For my project I have two boxes. The transmitter box contains a transmitter chip, 4 switches and a button.

The receiver box contains 8 LED’s (soon to be 12 hence the third column variable below) and a receiver chip.

The button on the transmitter box adds 1 to the column variable to make it two, or three, and removes 1 when it equals 4 so that its always either one, two or three. No higher no lower.

if (buttonState == LOW) {
          column += 1;
          if (column == 4) {
            column = 1;
          }
}

I haven’t included the debounce code as I didn’t deem it necessary for this example however the button is debounced.

Now what I’m trying to achieve is when: column == 1 and the for loop iterates over the four switches,

for (int i = 0; i < 4; i++) {
    switchStates[i] = digitalRead(switchPins[i]);
    if (advanced == false) {
      if (column == 1) {
        if (switchStates[i] == LOW && (ranBefore[i]) == 0) {
          transmitFunc(i);
        }  else if (switchStates[i] == HIGH && (ranBefore[i]) == 1) {
          transmitFuncOff(i);
        }

when the switch goes low (or high in the physical world ) The program passes the i variable to the transmitFunc() method, then when the switch goes high (or low in the physical world) The program passes the i variable to the transmitFuncOff() method. This works flawlessly.
However even if the second for loop is present in the code not even being used. IE when column != 2, the first for loop loops infinitely when any switch goes low (high in the real world)

Second for loop

for (int i = 0; i < 4; i++) {
    switchStates[i] = digitalRead(switchPins[i]);
    if (advanced == false) {
      if (column == 2) {
        if (switchStates == LOW && (ranBefore[i + 4]) == 0) {
          Serial.println("Iteration");
          Serial.println(i);
          transmitFunc(i);
        }  else if (switchStates[i] == HIGH && (ranBefore[i + 4]) == 1) {
          transmitFuncOff(i + 4);
        }
      }

    }

    }

I'm almost certain that the fine folks at http://snippets-r-us.com will be able to help you with your snippets.

It is not at all clear what that mess of code is trying to do. At a minimum, your code seriously needs comments.

Start with Planning and Implementing an Arduino Program

Look at how Robin suggests you structure your program. One of the important things is only reading inputs in one place in the code. Then the rest of the code can make decisions about that single, consistent, set of inputs.