Relative newbie to arduino but I have some coding experience, mostly in C# and java. To be honest the thing about arduino that has thrown me for a loop is the void loop() function. If I only want a function or method to be called once it complicates things. Anyway on to my problem.
I received from ebay a number of uln2003 chips so this morning I set up a circuit just to play around with the chip and get a feel for how it functions. So I set up the simple circuit mentioned below.
I have 7 leds connected to pins 2 through 8 on the arduino with the uln2003 between them and the leds. I want to blink the leds on then off in sequence.
This is easy to do using the delay function. But I can't figure out how to do it without using delay.
I have looked at the blink without delay sketch and tried to implement it in a number of ways.
The code with delay is below. I've tried using the if(currentMillis - previousMillis >= interval) statement before the for loop, I knew that wouldn't work but gave it a try. I also used the if statement in the for loop and that didn't work either. Can someone give me some help with this. I know it has to be possible.
thanks
int leds[] = {2,3,4,5,6,7,8};
int time = 100;
void setup() {
for(int i = 0; i < 7; i++)
{
pinMode(leds[i], OUTPUT);
}
}
void loop() {
for(int i = 0; i < 7; i++)
{
digitalWrite(leds[i], HIGH);
delay(time);
digitalWrite(leds[i], LOW);
delay(time);
}
}
dualtrace:
If I only want a function or method to be called once it complicates things. Anyway on to my problem.
That part is mind numbingly easy. You just do it in setup().
If you want something to happen selectively in loop(), you put it inside a conditional statement.
Don't use a for loop to iterate through the array, use the loop() function and a variable that you increment as the array index. When you have just incremented the array index do what you need to with the LED and save the time from millis(). Next time through loop() check whether the required period has elapsed. If not, go round again until it has. When the period has elapsed do what you need with the current LED, increment your array index, dealing with rollover back to zero if necessary (the modulus operator is handy here), do what you want with the new LED and save the time from millis() and do it all over again.
aarg:
That part is mind numbingly easy. You just do it in setup().
If you want something to happen selectively in loop(), you put it inside a conditional statement.
yeah, that works if I want to run the function at the beginning of the sketch. I understand that it can be accomplished with a conditional statement. That's what I've been doing. Calling something mind numbling easy is of no help.
UKHeliBob:
Don't use a for loop to iterate through the array, use the loop() function and a variable that you increment as the array index. When you have just incremented the array index do what you need to with the LED and save the time from millis(). Next time through loop() check whether the required period has elapsed. If not, go round again until it has. When the period has elapsed do what you need with the current LED, increment your array index, dealing with rollover back to zero if necessary (the modulus operator is handy here), do what you want with the new LED and save the time from millis() and do it all over again.