Array Help

Here my code

int timer = 3000;           // The higher the number, the slower the timing.
int ledPins[] = { 
  9, 10, 12, 11 };       // an array of pin numbers to which LEDs are attached
int pinCount = 4;           // the number of pins (i.e. the length of the array)

void setup() {
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
    pinMode(ledPins[thisPin], OUTPUT);      
  }
}

void loop() {
  // loop from the lowest pin to the highest:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) { 
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);   
    delay(timer);                  
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);    

  }

  // loop from the highest pin to the lowest:
  for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) { 
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);
  }
}

It the standard array code I just changed the pins and the int timer to slow down the sequence.

However some leds stay on longer than other. Any ideas on how to change this?

However some leds stay on longer than other. Any ideas on how to change this?

Which pins are staying on too long? If it's the ones at the ends of the array, check that the loops actually start and end at the same place.

You're right its the ones at the ends, how do I check the loop like you mentioned? I'm new at this

You are starting the next loop at the same place you end the first, so you will see the LEDs turn on in the following order:

0
1
2
3
--------
3
2
1
0
-------
0
1
2
3
------
3
2

and so on.

The dashed lines mark the end of one loop and the beginning of the next. Because there is no delay between the loops, 0 and 3 will appear to be on for twice as long as they should.

Perhaps if you changed you second loop to the following:

for (int thisPin = pinCount - 2; thisPin > 0; thisPin--)

Notice that the loop will now no longer flash 0 and 3, so your leds will flash as such:

0
1
2
3
--------
2
1
-------
0
1
2
3
------
2

Print the thisPin variable inside the for loop to understand what is going on.

[quote author=Tom Carpenter link=topic=178391.msg1322581#msg1322581 date=1374269781]

Perhaps if you changed you second loop to the following:

for (int thisPin = pinCount - 2; thisPin > 0; thisPin--)

[/quote]

Thanks buddy, that did it.

Now how do I go about randomizing the array?

Now how do I go about randomizing the array?

First, you explain what this means. Then, you define when you want to do it. Then, we can help you. There is a random() function.

I’m trying to have the sequence that the leds come on be completely random, with a 4 second break between each led going high.

This is what I have now

long randNumber;
int timer = 4000;           // The higher the number, the slower the timing.
int ledPins[] = { 
  9, 10, 12, 11};       // an array of pin numbers to which LEDs are attached
int pinCount = 4;           // the number of pins (i.e. the length of the array)

void setup() {
  Serial.begin(9600);

  // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  // different seed numbers each time the sketch runs.
  // randomSeed() will then shuffle the random function.
  randomSeed(analogRead(0));
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
    pinMode(ledPins[thisPin], OUTPUT);      
  }
}

void loop() {
  
  // loop from the lowest pin to the highest:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) { 
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);   
    delay(timer);                  
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);    

  }

  // loop from the highest pin to the lowest:
  for (int thisPin = pinCount - 2; thisPin > 0; thisPin--) { 
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);
  }
}

I'm trying to have the sequence that the leds come on be completely random

There are different way that you can make this happen. You could choose a random number between 0 and the upper limit of the array, and use that to turn a pin on or off. That could cause problems with the pins that were turned on not being the ones turned off. Of course, you could turn them on in random order, and off in the defined order.

You could shuffle the array of pin numbers, and then turn the pins on in the shuffled order and off in the same order.

If you ever want to use the pin numbers in the original order, though, shuffling the original array is not a good idea.

More details about what you are trying to accomplish would be good.

Hey Paul so here’s what I want

I have leds on pins 9-12, and vibration sensors on pins 5-8. I want them to be linked together; pin 9 and 5, 10 and 6, 11 and 7, 12 and 8.

When the arduino turns on I do not care which led comes on first. But lets say the led on 11 goes high, I want it to stay high for 4 seconds unless the arduino reads a signal from pin 7. If it reads a signal on pin 7 it goes low. Once the led goes low then a 4 second break occurs until the next led goes goes high and then the cycle continues.

To summarize:

Power arduino
random led between pins 9-12 goes high for 4 seconds, if associated vibration sensor sends a signal then it goes low
(pin 12 high for 4 sec if 8 sees a signal 12 goes low, 11 high for 4 sec signal on 7 makes it low and so on)

once first led goes low; a 4 second break occurs

after 4 second break is over; random led goes high and same thing, on for 4 sec unless its vibration sensor sees a signal

4 second break

continues to randomly cycle