Probably a Basic Switch Question

Hey Forum,

I’m trying to add an on/off temporary state change button to this circuit so that the switch will turn the entire circuit on and off. My issue is figuring out how to digitally write HIGH and LOW values to the entire circuit as opposed to a single LED (most examples I could find only use a single LED). I assume it’s the same principle but this sketch involves additional HIGH and LOW digital writing which makes it a little more confusing as to when and where to write HIGH and LOW values.

This is the code I’ve been updating but I’m unsure how to script the void loop portion.

//Flex Sensor Pin (flexPin)
//the analog pin the Flex Sensor is connected to
const int buttonPin = XXXX; 
int flexPin = 0;
int flexPin2 = 1;
int buttonState = 0;

void setup() {
 pinMode(buttonPin, INPUT);
  for (int i=2; i<10; i++){
    pinMode(i, OUTPUT);
  for (int i2=25; i2<36; i2++){
    pinMode(i2, OUTPUT); //sets the led pins 28 to 36 to output
  }
}
}


void loop(){
 //Ensure to turn off ALL LEDs before continuing 

if (buttonState == HIGH) {    
    digitalWrite(XXXXX, HIGH);  
  }
  else {
    digitalWrite(XXXXX, LOW);
  }
  
 for (int i=2; i<10; i++){
    digitalWrite(i, LOW);
 for (int i2=25; i<36; i2++){    
    digitalWrite(i2, LOW); 
  }
 } 
 

 int lightLevel = map(analogRead(flexPin), 175, 250, 2, 9);
 int lightLevel2 = map(analogRead(flexPin2), 175, 250, 25, 35); 
         
// Make sure the value is between 4 and 13, to turn on an LED
 int ledON = constrain(lightLevel, 2, 9);
 int ledON2 = constrain(lightLevel2, 25, 35);  
 
//Blink the LED on
 blink(ledON, 10,1);
 blink(ledON2, 10,1);
}

// The blink function - used to turn the LEDs on and off
void blink(int LEDPin, int onTime, int offTime){
  // Turn the LED on                                         
 digitalWrite(LEDPin, HIGH);
 
  // Delay so that you can see the LED go On.
 delay(onTime);
 
  // Turn the LED Off                                         
  digitalWrite(LEDPin, LOW);
 
 // Delay so that you can see the LED go On.
  delay(offTime);
}

// The blink function - used to turn the LEDs on and off
void blink2(int LEDPin2, int onTime2, int offTime2){
  // Turn the LED on                                         
 digitalWrite(LEDPin2, HIGH);
 
  // Delay so that you can see the LED go On.
 delay(onTime2);
 
  // Turn the LED Off                                         
  digitalWrite(LEDPin2, LOW);
 
 // Delay so that you can see the LED go On.
  delay(offTime2);
}

Note: I replied on this code in your other thread allready

your code nests the for loops !! The second for loop is executed 8 times.

The solution is proper indentation, press CTRL-T in the IDE to get the code auto formatted and you will see this layout indicating nested for.

for setting the outputmode that makes no sense.

void setup() {
  pinMode(buttonPin, INPUT);
  for (int i=2; i<10; i++){
    pinMode(i, OUTPUT);
    for (int i2=25; i2<36; i2++){
      pinMode(i2, OUTPUT); //sets the led pins 28 to 36 to output
    }
  }
}

same happens here , makes no sense, logically …

  for (int i=2; i<10; i++){
    digitalWrite(i, LOW);
    for (int i2=25; i<36; i2++){    
      digitalWrite(i2, LOW); 
    }
  }

The solution is proper indentation, press CTRL-T in the IDE to get the code auto formatted and you will see this layout indicating nested for.

What is the correct indentation? I auto formatted and the sketch reads:

void loop(){
  //Ensure to turn off ALL LEDs before continuing 

  if (buttonState == HIGH) {    
    digitalWrite(4, HIGH);  
  }
  else {
    digitalWrite(4, LOW);
  }

  for (int i=2; i<10; i++){
    digitalWrite(i, LOW);
    for (int i2=25; i<36; i2++){    
      digitalWrite(i2, LOW); 
    }
  }

for setting the outputmode that makes no sense.

I’m generally confused about the syntax and the order of digital writing in this situation. Do I have the operations in the right order and how should the buttonpin loop be organized with the other loop functions?

Thanks.

What is the correct indentation? I auto formatted and the sketch reads:

It is not about what is the correct indentation, The (auto) indentation is based upon and a result of the logic of your algorithm.

  for (int i=2; i<10; i++){
    digitalWrite(i, LOW);
    for (int i2=25; i<36; i2++){    
      digitalWrite(i2, LOW); 
    }
  }

Looking at the code above, and you follow it with paper and pencil (the most powerful tools besides the whiteboard) then you will see that when int i = 2 there is a call for digitalWrite(2, LOW) AND a complete for loop for int i2 groes from 25 to 36 setting them all low too. Then for i = 3 the for loop with i2 is executed again (not changing anything as all pins are allready low) and then for i =4,5,6,7,8,9 .

WHat you wanted to do is probably the following: 2 loops executed after each other to set all the pins to LOW. (but set them LOW only once!)

  for (int i=2; i<10; i++)
  {
    digitalWrite(i, LOW);
  }
  for (int i2=25; i<36; i2++)
  {    
    digitalWrite(i2, LOW); 
  }

This is one of the reasons that I put the { and the } on separate lines. It makes it easier to see which lines of code are in one block.

WHat you wanted to do is probably the following: 2 loops executed after each other to set all the pins to LOW. (but set them LOW only once!)

  for (int i=2; i<10; i++)
  {
    digitalWrite(i, LOW);
  }
  for (int i2=25; i<36; i2++)
  {    
    digitalWrite(i2, LOW); 
  }

So how would digital writing work with an on/off switch that controls HIGH and LOW values for ALL i vaules (i, i2, i3, etc…)? In other words when off, nothing happens, when on, the lights receive digital data?

Essentially digitally writing HIGH and LOW for LEDs within dependent on the HIGH and LOW values from the on/off loop function?