# Two independent things at once

Hi, I hope, this is not a 1000times asked question, i was trying to find an answer, but I’ve had no luck.

Is it even possible, to make work two things at on moment, and if, how to combine them? I have made an small example.

I have here a small 7LED liquid knightrider, and I want it to run without stops, but if I add another for example blining LED, it will run only one after another. This means, KnightRider goes there and back, but then it stops and waits, after the LED on pin 50 blinks. In my opinion is there a problem with the loop, but dont know…

here is the sketch

``````int pinArray[] = {53, 51, 49, 47, 45, 43, 41};
int count = 0;
int timer = 30;
int led = 50;

void setup(){
for (count=0;count<7;count++) {
pinMode(pinArray[count], OUTPUT);
pinMode(led, OUTPUT);
}
}

void loop() {
for (count=0;count<6;count++) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count + 1], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer*2);
}
for (count=6;count>0;count--) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count - 1], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer*2);
}
{
digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
delay(100);               // wait for a second
digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
delay(100);               // wait for a second
}
}
``````

Thanks a lot for your help!

You need to remove all the delay() calls in the code. Loki at the blink without delay example and see how millis() is used to keep track of when to do things. You will also probably need to read up a bit on finite state machines before too long as well.

Thanks a lot, this worked, I changed it at the single led and its ok, now I have to write it in the fluid, this will be more complicated I think…

``````int pinArray[] = {53, 51, 49, 47, 45, 43, 41};
int count = 0;
int timer = 30;
const int ledPin = 50;
int ledState = LOW;
long previousMillis = 0;
long interval = 50;

void setup(){
for (count=0;count<7;count++) {
pinMode(pinArray[count], OUTPUT);
pinMode(ledPin, OUTPUT);
}
}

void loop() {
for (count=0;count<6;count++) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count + 1], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer*2);
}
for (count=6;count>0;count--) {
digitalWrite(pinArray[count], HIGH);
delay(timer);
digitalWrite(pinArray[count - 1], HIGH);
delay(timer);
digitalWrite(pinArray[count], LOW);
delay(timer*2);
}
unsigned long currentMillis = millis ();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;

if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;

digitalWrite (ledPin, ledState);
}
}
``````

You want these to be unsigned long
long previousMillis = 0;
long interval = 50;

Get rid of these: delay() 's
delay(timer);

I'll be following this a little, have kind of the same problem but with reading a button instead, has taken my weeks and still nothing...

CharlitoCZ is right - there is a problem in the "loop".

It's a conceptual thing, and it is the crux of perhaps half the questions overall.

If you only ever wish to do one thing, and have it repeat ad infinitum, then you can view the "loop" as the way to have that thing repeat.

If you wish to do more than one thing however, you must view the "loop" in a totally different manner. The "loop" becomes a "worker", running rapidly round and round, never waiting for anything (so, no "delay()" calls) and never spending more than the necessary attention to repetitive mechanical tasks (so, no "while" or "for" loops with any dependencies). He is a "time and motion expert", incessantly looking at his watch ("millis()") to see what needs to be done at any given moment, moving on to the next task until that time has come.

He makes notes to keep track of how each task is progressing ("state variables" or switches) and checks those notes one after the other.

So, your code using "ledState" is correct, but you have to re-write the "pinArray" code in the same way, using a second "previousMillis" timer and "count" as the switch.