Turning off an array using all at once

Hi,

I’m new to Arduino and quite enjoying it…for the most part.

For my engineering course, we were set the task of sequencing 6 LEDs using an array. We had learnt about turning a sequence on and off the week before using an external switch through an “if else” statement so I thought I would incorporate a switch into the circuit (just so I can get my head around how this whole system works).

What I want to do is to commence the sequence if the switch is HIGH but send ALL of the LEDs LOW if the switch is LOW. I’m happy for the sequence to finish if the switch is turned off mid-cycle. I think if I use a “while” statement I can get the sequence to finish as soon as the switch is turned off but for now, I want to use an “if else” statement (if it is possible).

The problem is, I don’t seem to be able to turn the LEDs off. The sequence will happily step through but when I set the switch to LOW, the LEDs stay on. Could someone please alert me to what I am doing wrong? I have an exam on Arduino on Wednesday night so I’d kinda like to have a reasonable grasp of the subject :). My program is below…

I think it is something to do with the syntax I am using to turning the array off (last line of the program) but I’m not sure. Any help is greatly appreciated.

Kind regards,
Troy

//Week 5 Prac 1_Sequencing LEDs using an array

// Declarations

const int Switch = 2; // Pin 2 declared as “Switch”
const int DelayPeriod = 1000; // Declare a constant delay period of 1 second
int SwitchState = 0; // The variable “SwitchState” is declared
int LEDs = {10,12,7,9,11,8}; // The Array
int pincount = 6; // Number of O/Ps for the Array

void setup() // SETUP routine will run once when the Arduino RESET button is pressed
{
for(int count=0; count<pincount; count++) // Assign the array pins their array position
pinMode(LEDs[count], OUTPUT); // “LEDs” initialised as O/P for LOOP
}

void loop() // LOOP routine runs continuously
{
SwitchState = digitalRead(Switch); // “SwitchState” is the value of “Switch” (Pin 2)
if (SwitchState==HIGH) // If the value of “buttonState” is HIGH, commence sequence
{
for(int count=0; count<pincount; count++) // Count sequence
{
digitalWrite(LEDs[count], HIGH); // Turn the LED on that matches the count
delay(DelayPeriod); // Wait 1 second
}
}
else // Else, if the switch is NOT on, turn all LEDs OFF
{
digitalWrite(LEDs[10,12,7,9,11,8], LOW); // Turn all LEDs OFF
}
}

Apologies to moderators if this is in in the wrong section. Some of the sections seem so alike.

Regards, Troy

digitalWrite(LEDs[10,12,7,9,11,8], LOW);   // Turn all LEDs OFF

This is wrong. digitalWrite() only works on one pin at a time. Try

for (byte n = 8; n < 13; n++) {
   digitalWrite(LEDs[n], LOW);
}

And please use the code button to display your code properly.

…R

Braydens_Dad:
The problem is, I don’t seem to be able to turn the LEDs off. The sequence will happily step through but when I set the switch to LOW, the LEDs stay on. Could someone please alert me to what I am doing wrong? I have an exam on Arduino on Wednesday night so I’d kinda like to have a reasonable grasp of the subject :). My program is below…

I think it is something to do with the syntax I am using to turning the array off (last line of the program) but I’m not sure.

You can do exactly the same for-loop procedure as with setting pinMode in the setup() function:

 for(int count=0; count<pincount; count++)   // Assign the array pins their array position
   pinMode(LEDs[count], OUTPUT);         // "LEDs" initialised as O/P for LOOP

and use a for loop to set all LEDs:

 for(int count=0; count<pincount; count++)   // Assign the array pins their array position
   digitalWrite(LEDs[count], LOW);         // set "LEDs" to LOW

Problem fixed. Thank you so much :smiley: :smiley:

I found a second problem whereby I had not initialised Pin 2 as an I/P

Updated code below.

Regards,

Troy

//Week 5 Prac 1_Sequencing LEDs using an array

// Declarations
 
 int Switch = 2;				// Pin 2 declared as "Switch"
 int SwitchState = 0;			// The variable "SwitchState" is declared
 const int DelayPeriod = 1000;	// Declare a constant delay period of 1 second
 int LEDs[] = {10,12,7,9,11,8};	// The Array
 int pincount = 6;				// Number of O/Ps for the Array

  
void setup()	// SETUP routine will run once when the Arduino RESET button is pressed
{
 pinMode(Switch, INPUT);					// "Switch" (Pin 2) initialised as an I/P
 for(int count=0; count<pincount; count++)	// Assign the array pins their array position
 pinMode(LEDs[count], OUTPUT);				// "LEDs" initialised as O/P for LOOP  
}


void loop()	// LOOP routine runs continuously
{
 SwitchState = digitalRead(Switch);	// "Switch State" will be High or LOW depending on position of the switch
 if (SwitchState==HIGH)				// If the value of "SwitchState" is HIGH, commence LED lighting sequence
  {  
   for(int count=0; count<pincount; count++)	// Count sequence
    {
     digitalWrite(LEDs[count], HIGH);	// Turn the LED on that matches the count
     delay(DelayPeriod);				// Wait 1 second
    }
  }
 else 								// Else, if the switch is NOT on, turn all LEDs OFF
  {
   digitalWrite(LEDs[0], LOW);		// Turn LED at position 0 of the array (Pin 10) OFF
   digitalWrite(LEDs[1], LOW);		// Turn LED at position 1 of the array (Pin 12) OFF
   digitalWrite(LEDs[2], LOW);		// Turn LED at position 2 of the array (Pin 7) OFF
   digitalWrite(LEDs[3], LOW);		// Turn LED at position 3 of the array (Pin 9) OFF
   digitalWrite(LEDs[4], LOW);		// Turn LED at position 4 of the array (Pin 11) OFF
   digitalWrite(LEDs[5], LOW);		// Turn LED at position 5 of the array (Pin 8) OFF
  }
}