Need help with code!!

Hi!

We are stuck in our science project and we are in desperate need of you help! We need to be able to sequentially control eight LEDs using an Arduino Uno. The problem is we have no idea of how to write a suitable code. The LEDs are to be activated in intervals with specific time for on/off for each one. In the first sequence the LEDs must have a special timing unlike the following which will have the same. We have read about delay, loop and interrupt but are unsure of how to use them to fit our purpose.

Best regards

What type of school project? (College, High School, Middle, …)?

We do not normaly do homework here, and you certainly will not get much help unless you read this first:

viewtopic.php?f=32&t=27089

Once you properly design the question, you may find us giving up hints.

^^Seems to be the standard C/C/P answer here with very little help:P

vetenskapsman1:
Hi!

We are stuck in our science project and we are in desperate need of you help! We need to be able to sequentially control eight LEDs using an Arduino Uno. The problem is we have no idea of how to write a suitable code. The LEDs are to be activated in intervals with specific time for on/off for each one. In the first sequence the LEDs must have a special timing unlike the following which will have the same. We have read about delay, loop and interrupt but are unsure of how to use them to fit our purpose.

Best regards

While I don’t exactly know what you are looking for as far as code I may suggest a couple places that I have found far more helpful than here as far as collaborating ideas and getting help.

http://hackaday.com/category/led-hacks/

http://www.youtube.com/results?search_q … duino&aq=f

http://vimeo.com/search/videos/search:arduino/48464f5e

-Cheers

vetenskapsman1:
…to fit our purpose.

This is what is at issue. We have no clue what your purpose is other than to blink some LEDs in various sequences!

Do you need second accuracy or picosecond? What are the durations? What are the intervals. How does one sequence differ from another? How does the system know when to transition from one sequence to another? Do the LEDS have to come on and off at the exact same time, or can there be very slight delays between them?

Each answer can give you very different answers.

fll-freak:
What type of school project? (College, High School, Middle, …)?

Well , I can give you a quick lesson of the swedish education system:

Ground school: 1-9 years

Gymnasium: 1-3 years - we’re at the third year

College:

This is not a usual homework, it is a one year long project which is to be finished in two weeks. We are sorry for giving you so little to grab on, and we must admit we lied a little bit. It’s not really LEDs we are going to activate but solenoids through MOSFETs, but we thought we would be denied information (has happend before) due to suspicion of us building a coilgun - which we are not. We are acutally building a cooling device which shall use solenoids to circulate a cooling ferrofluid.

Although the principle is very much like the one of the coilgun: In first sequence (or lap) the fluid must accelerate, thus the special timing. The timing must not be more specific than milliseconds and shall be calibrated after testing.

In first sequance (hypothetical)

Sol1

on @ 0.00s off @ 0.70s

Sol2

on @ 0.70s off @ 1.30s

etc…

The second sequence will start immediatly after the first one and it will have be the same intervalls as the following sequences (as the ferrofluid will have reached maximum speed in the first sequence and we only want to preserve constant velocity). The sequence shall be looped untill we tell it to stop.

We really appreciate your help!

First of all let me compliment you on your English. It gave little indications you are not native speakers.

With proper supervision, a 10 terra watt rail gun would have been a fun project. :smiley:

The best way to help you is to walk you through what I did.

I opened up the Arduino IDE (version 0022). Fom the main menu I selected:

File…Example…Basics…Blink

This opened a sample program that is ridiculously close to what you need.

In the “setup” function (only called once on power up or reboot), I added the definitions of pins 3,4,5,6,7,8,9 as outputs. I also make the assumption that a high signal will turn on your MOSFET.

I changed the comment at the top.

Next I started to work on the loop function. Loop gets called over and over again. As soon as one iteration is completed, another is started. No need to code an infinite loop. Here I create the first sequence turning on and off the pin using digitalWrites and delays (in milliseconds) to pulse the pins in sequence. Once this was written, I needed a simple way to start a different sequence. I picked (among several different ways) to use a series of IF statements. At the conclusion of one IF I set the “Sequence” variable for the second. This way you could extend the sequences if you need to.

This program is not likely what you will need to get your project to work. The program compiles, but I have left in at least one logic “bug” that you should be able to find and solve simply.

It might be hepful to add an LED to each output pin as well as the MOSFET so that you can see what is happening.

Good luck!

/*
  Ferro fluid test
 */

int sequence;


void setup() {                
  // initialize the digital pins as an output.
  pinMode(3, OUTPUT);     
  pinMode(4, OUTPUT);     
  pinMode(5, OUTPUT);     
  pinMode(6, OUTPUT);     
  pinMode(7, OUTPUT);     
  pinMode(8, OUTPUT);     
  pinMode(9, OUTPUT);     
  
  sequence = 1;
}

#define S1_ON  10
#define S1_OFF 10
#define S2_ON  5
#define S2_OFF 5


void loop() {
  
  if (sequence == 1) {
  
    digitalWrite(3, HIGH);
    delay(S1_ON);
    digitalWrite(3, LOW);
    delay(S1_OFF);
    
    digitalWrite(4, HIGH);
    delay(S1_ON);
    digitalWrite(4, LOW);
    delay(S1_OFF);
    
    digitalWrite(5, HIGH);
    delay(S1_ON);
    digitalWrite(5, LOW);
    delay(S1_OFF);
    
    digitalWrite(6, HIGH);
    delay(S1_ON);
    digitalWrite(6, LOW);
    delay(S1_OFF);
    
    digitalWrite(7, HIGH);
    delay(S1_ON);
    digitalWrite(7, LOW);
    delay(S1_OFF);
    
    digitalWrite(8, HIGH);
    delay(S1_ON);
    digitalWrite(8, LOW);
    delay(S1_OFF);
    
    digitalWrite(9, HIGH);
    delay(S1_ON);
    digitalWrite(9, LOW);
    delay(S1_OFF);
    
    sequence = 2;
    
  } else if (sequence == 2) {
  
    digitalWrite(3, HIGH);
    delay(S2_ON);
    digitalWrite(3, LOW);
    delay(S2_OFF);
    
    digitalWrite(4, HIGH);
    delay(S2_ON);
    digitalWrite(4, LOW);
    delay(S2_OFF);
    
    digitalWrite(5, HIGH);
    delay(S2_ON);
    digitalWrite(5, LOW);
    delay(S2_OFF);
    
    digitalWrite(6, HIGH);
    delay(S2_ON);
    digitalWrite(6, LOW);
    delay(S2_OFF);
    
    digitalWrite(7, HIGH);
    delay(S2_ON);
    digitalWrite(7, LOW);
    delay(S2_OFF);
    
    digitalWrite(8, HIGH);
    delay(S2_ON);
    digitalWrite(8, LOW);
    delay(S2_OFF);
    
    digitalWrite(9, HIGH);
    delay(S2_ON);
    digitalWrite(9, LOW);
    delay(S2_OFF);
    
  }

}