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!!
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!!
Your question is vague at best. What is it that you wish to accomplish?
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.
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.
A simple example. (Need to declare variables, create void setup, etc.)
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
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.
Fully documented example available here:
This works for me:
/*
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);
}
}
}