Controlling 18 LEDs With Arduino Mega

Here is the situation: I am going to be running a little science experiment called "How Fast Can You See?" Obviously this envolves different speeds of blinking lights.

I already know that I am going to use an Arduino Mega to controll the leds. The issue is that I dont know how to do the delays. I knwo that I could do 18 digital writes and then the delays, but that seems WAY sloppy... How can I make it a little bit cleaner?

Simple, less techniques to learn way: use arrays and for loops. An array stores the pin numbers of the LEDs, and inside the for loop is a digitalwrite. The pin number for the digitalwrite is the element in the array with the index i, or whatever variable you increment in the loop.

More complex way (in that it requires more learning in code, I think): port manipulation. I know enough about it to know it is a possible solution, but I don't know enough to explain it well. Basically it is a way to directly address groups of pins or ports all at once, with a byte holding the state of 8 pins. There is a tutorial somewhere on the arduino site, I believe in the section with the bit math tutorial. (maybe not)

I know how to do a constant byte for the led pins. I also know how to do the void setup. I dont know how to do the void loop. I don’t quite understand your examples. Can you maybe post smoe example code?

I dont know how to do the void loop.

Look at:-

http://www.arduino.cc/en/Tutorial/KnightRider

look at the difference between to first and second examples to see what he is talking about.

Remember you can only have 200mA of LED current before you overload the chip.

I need to blink 18 leds at 18 different delay times. I think I failed to mention that... Sorry guys. And as for the 200mA, I will be driving the leds with ULN2803A chips.

I think I failed to mention that.

Yes you did ... but the link tells you about those techniques you didn't know about. Try the blink without delay example:- http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay Only put a resistor in line with the LED.

Ok, but how would I do 18 DIFFERENT delay times?

By repeating what the code does for another 17 times. With separate interval variables for each LED. When you are clever you can even put it in a loop using arrays and indexes.

Yeah, that's above my head... So basically I copy and paste the BlinkWithoutDelay 18 times total, and then have 18 different delay time setups?

Also, how COULD I go about doing the loop and array thing? I would like to make this code clean; it will be demonstrated in my class, and if it freezes... yikes... :P

So basically I copy and paste the BlinkWithoutDelay 18 times total, and then have 18 different delay time setups?

Yes that is one way. Remember to change the variable names for each LED.

how COULD I go about doing the loop and array thing

Right but I want you to fully understand this and explain it to the class, also do not pretend it is all your own work.

Might have the odd bug as I have not tested it, but it compiles:-

long nextFlash[18];
int interval[] = { 100, 200, 300, 400, 500, 600, 700, 800,
                   900, 1000, 1100, 1200, 1300, 1400, 1500, 1600,
                   1700, 1800}; // flash intervals
int ledPin[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
                12, 13, 14, 15, 16, 17, 18, 19, 20}; // LED pins to use.
int ledState[18];

void setup(){
 for(int i = 0; i<18; i++){
   pinMode(ledPin[i],OUTPUT);
   ledState[i] = LOW;
   digitalWrite(ledPin[i], LOW); // all LEDs off
     nextFlash[i] = millis()+ interval[i];
 } 
}

void loop(){
for(int i = 0; i<18; i++){
if(millis() > nextFlash[i]){
  if(ledState[i] == LOW) ledState[i] = HIGH; else ledState[i] = LOW;
  digitalWrite(ledPin[i],ledState[i]);
  nextFlash[i] = millis()+ interval[i];
} }
}

I understand that a little bit... But as far as the class, I plan on using this as a science fair project for school, not an electronics class... But I might show it at the convention that cr0sh and some others are planning... I will be sure to credit you there!

I will be sure to credit you there!

I am not bothered about me being credited just make sure you don't claim it is all your own work. ;)

Well, I am pretty sure they could tell... My projects are all like simple crossfades and all that... Well, I'm going to bed... Thanks for your help!