Go Down

Topic: Array/For loop with 'millis' (Read 8860 times) previous topic - next topic

Eddie04



How would you write the code to run a 'for loop' or 'array' using millis and no delays at all? Or any other way just without using delay.

Thank you if you can help!! :)

groundFungus

Your question is vague at best.  What is it that you wish to accomplish?

Eddie04



Sorry, I would like to do an led chase pattern,  just like the example 'Array' and 'ForLoopIteration' sketches except with no delays. Maybe using millis.

PeterH

Use the approach demonstrated in the blink without delay example sketch to trigger code to run at regular intervals.

The code you will run would update the LED outputs. For example is you wanted to turn one LED off and turn the mext LED on then you would have a variable recording which LED is currently on, and your code would turn that LED off, increment the variable to select the next LED and then turn that one on.

Presumably you would want this to run for more than one cycle so you would need some logic to detect when you have reached the end of the array and reverse the direction of the increment.

CrossRoads

A simple example. (Need to declare variables, create void setup, etc.)
Code: [Select]

void loop(){
currentMillis = millis();  // capture current "time"
// all time related variable are unsigned long
elapsedMillis = currentMillis - previousMillis; // see how much time has passed
if (elapsedMillis >= duration){ // time for next occurrence of event?
previousMillis = previousMillis + duration;  // set up time for next occurrence of event
arrayIndex = arrayIndex +1; // increment pointer
if (arrayIndex == arrayEnd){
arrayIndex = 0;  // reset to beginning
}
// do action based on value stored at dataArray[arrayIndex];
} // end time check
// do other stuff if needed
} // end loop

Looks better with CTRL-T, all nicely indented and stuff
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

cmiyc


Sorry, I would like to do an led chase pattern,  just like the example 'Array' and 'ForLoopIteration' sketches except with no delays. Maybe using millis.


Fully documented example available here:
http://www.baldengineer.com/blog/2013/12/12/arduino-chasing-leds-with-millis/
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

eaman

#6
Nov 06, 2015, 01:13 am Last Edit: Nov 06, 2015, 01:15 am by eaman
This works for me:
Code: [Select]

/*
  For Loop Iteration

 Demonstrates the use of a for() loop.
 Lights multiple LEDs in sequence
 The circuit:
 * LEDs from pins 2 through 9 to ground

 Schemi:
 - http://lab.piffa.net/schemi/8_led_single_res_bb.png
 - http://lab.piffa.net/schemi/8_led_single_res_schem.png

 http://www.arduino.cc/en/Tutorial/ForLoop
 */

byte ledPins[] = { 
  9, 10, 11, 12
}
; //Array
long previousMillis ;
long interval = 1000;
byte i = 0;


void setup() {
  for (int thisPin = 0; thisPin < sizeof(ledPins); thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT);
  }
  previousMillis = millis();
}

void loop() {
  if (millis() - previousMillis > interval) {
    previousMillis = millis();

    if ( i < sizeof(ledPins) - 1 ) {
      // Spegni precedente led
      digitalWrite(ledPins[i], LOW);

      // Accendi successivo led
      digitalWrite(ledPins[++i ], HIGH);
    }

    else if  (i == sizeof(ledPins) - 1 )   {
      // Ultimo caso
      i = 0;
      previousMillis = millis();
      digitalWrite(ledPins[i ], HIGH);
      digitalWrite(ledPins[ sizeof(ledPins) - 1 ], LOW);
    }
  }
}




Go Up