Light Effect with Arduino Uno

Hi,
I have a question.
I have write a method for a light effect with Arduino. I have used delay in a code and I can't use multiple effects.

void spegnimentoProgressivo(int lettere[]) {
 int velocita = random (2,11) * 100;  
 for ( int i = 13 ; i < -1 ; i--){
    if (lettere [i] == 1 ) {
   digitalWrite (i, LOW);
   delay (velocita);
   }
 }
}

void scorrimento (int lettere[]) {
 int velocita = random (2,7) * 100;
 for ( int i = 13 ; i < -1 ; i--){
   if (lettere [i] == 1 ) {
   digitalWrite (i, HIGH);
   delay (velocita);
   digitalWrite (i, LOW);
   }
 }
}

void accensioneProgressiva(int lettere[]) {
 int velocita = random (2,11) * 100;
 for ( int i = 13 ; i<-1  ; i--){
   if (lettere [i] == 1 ) {
   digitalWrite (i, HIGH);
   delay (velocita);
   }
 }
}

lettere is a positional array. If lettere = 1 the pin was involved in effect but if lettere = 0.
If I wanted to run two effects simultaneously on different pins with non-overlapping array, how do I rewrite these methods?
I had thought to use the millis() function but I can't think of how to use it.
Does anyone have suggestions?

I'll bet that you wanted this code:

for ( int i = 13 ; i = -1 ; i--){
if (lettere == 1 ) {

  • digitalWrite (i, LOW);*
  • delay (velocita);*
  • }*

to look like this:

  for ( int i = 13 ; i = -1 ; i--){
     if (lettere[i] == 1 ) {
    digitalWrite (i, LOW);
    delay (velocita);
    }

The code is displayed improperly because the forum uses an "i" in brackets to indicate that the following text should be displayed in an italic font. The way to avoid this is to post code inside [ code ] tags.

Please read the sticky post, "How to use this forum - please read," shown at the top of the list of posts in each section of the forum. Please note, however, that the forum software has been updated since that sticky post was written, and the icon for code tags doesn't look the same. Instead of a hashtag, "#", it looks like a little scroll with a blue star. See the tiny attached image.

You'll find a lot of other information in that sticky post that will help you build posts that are more effective, in terms of communicating your intent, and in terms of inspiring others to respond with something that will help you.

The traditional method for avoiding using delay() is to save the value of millis() at the last operation, and have loop() check repeatedly to see if it's time for the next operation yet. Take a look at the example, "Blink without Delay," that came with the Arduino IDE. File > Examples > Digital in IDE 1.05.

tmd3:
I'll bet that you wanted this code:

for ( int i = 13 ; i < -1 ; i--){
if (lettere == 1 ) {

  • digitalWrite (i, LOW);*
  • delay (velocita);*
  • }*

to look like this:

  for ( int i = 13 ; i < -1 ; i--){

if (lettere[i] == 1 ) {
    digitalWrite (i, LOW);
    delay (velocita);
    }




The code is displayed improperly because the forum uses an "i" in brackets to indicate that the following text should be displayed in an italic font. The way to avoid this is to post code inside [ code ] tags.

Please read the sticky post, "How to use this forum - please read," shown at the top of the list of posts in each section of the forum. Please note, however, that the forum software has been updated since that sticky post was written, and the icon for code tags doesn't look the same. Instead of a hashtag, "#", it looks like a little scroll with a blue star. See the tiny attached image.

You'll find a lot of other information in that sticky post that will help you build posts that are more effective, in terms of communicating your intent, and in terms of inspiring others to respond with something that will help you.

The traditional method for avoiding using delay() is to save the value of millis() at the last operation, and have loop() check repeatedly to see if it's time for the next operation yet. Take a look at the example, "Blink without Delay," that came with the Arduino IDE. File > Examples > Digital in IDE 1.05.

Thank you for note.
About my code, I can not eliminate the delay with the millis () because the delay is in the middle of the effect and when it is finished, the loop back to work.

Could you give me an example for my specific case?

Here's a short sketch that blinks two LEDs, on pins 12 and 13, at a random rate for each LED:

int count12, count13;
int interval12, interval13;
unsigned long previousMillis12, previousMillis13;
const int numberOfFlashes = 13;

void setup() {
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
}

void loop() {

  if (count12 == 0) {
    interval12 = random(2,11) * 100;
    count12 = numberOfFlashes;
  }
  if (count13 == 0) {
    interval13 = random(2,7) * 100;
    count13 = numberOfFlashes;
  }

  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis12 > interval12) {
    if (digitalRead(12) == HIGH) {
      digitalWrite(12,LOW);
      count12--;
    }
    else {
      digitalWrite(12,HIGH);
    }
    previousMillis12 = currentMillis;
  }

  if (currentMillis - previousMillis13 > interval13) {
    if (digitalRead(13) == HIGH) {
      digitalWrite(13,LOW);
      count13--;
    }
    else {
      digitalWrite(13,HIGH);
    }
    previousMillis13 = currentMillis;
  }

}

The code is tested to verify that it blinks two LEDs at different rates.