Simple code for a 4 leds not working

Hello 8)
So, I am pretty new to arduino (5 days to be exact) and just today I received a pack of 100 LEDs from Amazon.
I have made some code so that when my toggle switch is off, the LEDs should flash on and off with a 500 ms delay and then the switch is on they should do a night rider type thing. However, when running my code, only the 3rd(pin 4) and 4t(pin 5) LEDs do as they should, the first 2 LEDs(pins 2 + 3) just stay off.

I have tried many things to fix this including:

  1. Test the LEDs
  2. Try different resistors (450ohm instead of 220ohm)
  3. Testing the pins in question

I am able to use another piece of code to simply blink 1 led and that works fine for each LED on each pin so i don't understand the problem with this. Most likely it is something to do with my code, but i really don't know.

Here is my code:

//name pins 
// constant variables cat change and are used for naming pins
// non constant variables can change, such as the state of the button
const int led[] = {2, 3, 4, 5 };
const int but1 = 6;
int but1state = 1;

//set up the pins and board
void setup() {
  //configure pins
  pinMode(led[2], OUTPUT);
  pinMode(led[3], OUTPUT);
  pinMode(led[4], OUTPUT);
  pinMode(led[5], OUTPUT);
  pinMode(but1, INPUT);
  digitalWrite(but1, HIGH); // internal pull up resistor
  
  //test led's Calling upon function (at bottom)
  butPress();
}

//main code to run forever
void loop() {
  but1state = digitalRead(but1);
  // because of internal pull up, 0 means on, 1 means off
  if(but1state == 0){
  butPress(); //Calling upon function (at bottom)
  }
  else{
    digitalWrite(led[2], HIGH);
    digitalWrite(led[3], HIGH);
    digitalWrite(led[4], HIGH);
    digitalWrite(led[5], HIGH);
    delay(500);
    digitalWrite(led[2], LOW);
    digitalWrite(led[3], LOW);
    digitalWrite(led[4], LOW);
    digitalWrite(led[5], LOW);
    delay(500);
  }
}

//a bit of code to 'call' upon in code so i dont have to rewrite it multiple times (saves space)
void butPress() {
  digitalWrite(led[2], HIGH);
  delay(500);
  digitalWrite(led[2], LOW);
  digitalWrite(led[3], HIGH);
  delay(500);
  digitalWrite(led[3], LOW);
  digitalWrite(led[4], HIGH);
  delay(500);
  digitalWrite(led[4], LOW);
  digitalWrite(led[5], HIGH);
  delay(500);
  digitalWrite(led[5], LOW);
}

And this is the code I used to test each pin individually:

int led = 5;

void setup() {
  pinMode(led, OUTPUT);
}

void loop() {
  digitalWrite(led, HIGH);
  delay(500);
  digitalWrite(led, LOW);
  delay(500);
}

This
const int led[] = {2, 3, 4, 5 };

should be accessed as
led[0] thru led[3] in setup & loop, to access pins 2,3,4,5 respectively.

CrossRoads:
This
const int led[] = {2, 3, 4, 5 };

should be accessed as
led[0] thru led[3] in setup & loop, to access pins 2,3,4,5 respectively.

Thank you so much! Could think of a solution for the life of me! must have read over that code 10 times looking for anything wrong :slight_smile:

Now, not later, stop using delay() to time things. You will hate yourself later if you don't do so now.

Use the millis() timer as a clock. There is an example in the Arduino called "BlinkWithoutDelay".

Also, here is a thread about using it to time more than one thing. It also allows you to run things asynchronously. So you can have an LED blinking every 500ms, another every 502ms, another every 20ms, etc. or have the timing on them change without regard to the timing on all the others. No way can you do that with delay().

http://forum.arduino.cc/index.php?topic=223286.0