School Project: How to run 2 "loops" at the same time

Hey guys, it it the first time I work with arduino, so I actually know nothing more than the very basic

I read on other topics that you cant exactly run 2 loops, but there are instead other ways to do so like the millis(); comand, but I didnt understand much and I need some help

So, I connected a light sensor on the board and as long as the analogread is under 200 I want it to do 2 things simultaneously: play a music and make some leds blink
However, the delay(); causes me a huge problem, they make the one run after the other.
Can you tell me a way to fix this?
Thanks in advance :slight_smile:

Here is the loop:

void loop() {
int PR=analogRead(lightPin);
Serial.println(PR);
if (PR<200) {
    for (int i=1; i<=8; i=i+1) {
      tone(buzzer,tones[i]);
      delay(500);
    }
    digitalWrite(led, HIGH);
    delay(500);
    digitalWrite(led2, HIGH);
    delay(500);
    digitalWrite(led, LOW);
    delay(500);
    digitalWrite(led3, HIGH);
    delay(500);
    digitalWrite(led2, LOW);
    delay(500);
    digitalWrite(led, HIGH);
    delay(500);
    digitalWrite(led3,LOW);
  }
  else {
    digitalWrite(led, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
  }
 delay(100);
}

You're right, you can't run two loops at once, and most real world programs avoid delay(), except for perhaps a very-short delay depending on the application.

Look at the [u]Blink Without Delay Example[/u]. Try to understand the concepts and logic of how that works...

In your application you'll need (at least) two different previousMillis and two (or more) interval variables to keep the timing separate. For example, you could name your variables previousMillisTone, previousMillisLED1, previousMillisLED2, etc. You only need one currentMillis, because the current time is always the current time whenever you read it!

This is probably going to get confusing, so I'd start with 2 LEDs, each with different timing first. When you get that figured-out you can additional timed events.

Looking at your LED timing, you can probably use one LED timer and just step-through the sequence.

They are 3 coloured leds so the blink cant be done with one but I think I can manage to make it again with millis, now that I understood how it works(I think)

Thanks for the link!

I'd use the timer library
#include <TimerOne.h>
and turn the LEDs on/off based on the timer count using if statements.

I got it to work last night , however I will look into the timer library

Its 3 times this:

if(currentMillis - previousMillis2 > interval2) {
   previousMillis2 = currentMillis;
   if (ledState2 == LOW){
     ledState2 = HIGH;
     interval2 = 1500;
     digitalWrite(led2, HIGH);
   }
   else {
     ledState2 = LOW;
     interval2 = 1500;
     digitalWrite(led2, LOW);
   }  
 }

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the "Code" button above the posting area (It looks like a scroll with < > inside it).

NikosBond:
I got it to work last night , however I will look into the timer library

The timer library won't do any better and, even if you can't immediately see it, it will use more code to do the same job.

Your code could be shortened a tiny bit like this

if(currentMillis - previousMillis2 > interval2) {
   previousMillis2 = currentMillis;
   if (ledState2 == LOW){
     ledState2 = HIGH;
     interval2 = 1500;
   }
   else {
     ledState2 = LOW;
     interval2 = 1500;
   }  
 }
 digitalWrite(led2, ledState2);

if interval2 never changes you could define it as a constant at the top of your program and remove it from this code.

And if you have several similar pieces of code you could put the variables into arrays and do it all in one piece of code.

...R

interval1 is 0, interval2 is 1000 and interval3 is 2000 and then they all change in 1500

I do want the interval to stay in the if, in case I decide to change or "restart" the blink tempo then the music starts or something

But other than the starting values of the intervals, everything else is the same in the 3 pieces of code, is there any way is this case to put them in arrays?

Full code here

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

const int led1 = 13;
const int led2 = 12;
const int led3 = 11;
const int LED = 4;
const int buzzer = 3;
const int lightPin = 0;
const int tones[] = {NOTE_E7, NOTE_E7, NOTE_E7, NOTE_C7, NOTE_E7, NOTE_G7, NOTE_G6, NOTE_C7, NOTE_G6, NOTE_E6, NOTE_A6, NOTE_B6, NOTE_AS6, NOTE_A6, NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, NOTE_F7, NOTE_G7, NOTE_E7, NOTE_C7,
  NOTE_D7, NOTE_B6, NOTE_C7, NOTE_G6, NOTE_E6, NOTE_A6, NOTE_B6, NOTE_AS6, NOTE_A6, NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, NOTE_F7, NOTE_G7, NOTE_E7, NOTE_C7, NOTE_D7, NOTE_B6 };
long previousMillis = 0;
long interval = 0;
int ledState1 = LOW;
long previousMillis2 = 0;
long interval2 = 1000;
int ledState2 = LOW;
long previousMillis3 = 0;
long interval3 = 2000;
int ledState3 = LOW;
long previousMillis4 = 0;
long interval4 = 500;
int i = 1;

void setup() {
  
  Serial.begin(9600);  
  
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(LED, OUTPUT);
  pinMode(buzzer, OUTPUT);
}

void loop() {
  
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
    if (ledState1 == LOW){
      ledState1 = HIGH;
      interval = 1500;
      digitalWrite(led1, HIGH);
    }
    else {
      ledState1 = LOW;
      interval = 1500;
      digitalWrite(led1, LOW);
    }  
  }
  if(currentMillis - previousMillis2 > interval2) {
    previousMillis2 = currentMillis;
    if (ledState2 == LOW){
      ledState2 = HIGH;
      interval2 = 1500;
      digitalWrite(led2, HIGH);
    }
    else {
      ledState2 = LOW;
      interval2 = 1500;
      digitalWrite(led2, LOW);
    }  
  }
  if(currentMillis - previousMillis3 > interval3) {
    previousMillis3 = currentMillis;
    if (ledState3 == LOW){
      ledState3 = HIGH;
      interval3 = 1500;
      digitalWrite(led3, HIGH);
    }
    else {
      ledState3 = LOW;
      interval3 = 1500;
      digitalWrite(led3, LOW);
    }
  }
  if(currentMillis - previousMillis4 > interval4) {
    previousMillis4 = currentMillis;
    int PR=analogRead(lightPin);
    Serial.println(PR);
    if (PR>200){
      digitalWrite(LED, HIGH);
      if (i<40){
        tone(buzzer,tones[i]);
        i = i + 1;
      }
      else {
        i = 1;
      }      
    }
    else {
      i = 1;
      digitalWrite(LED, LOW);
      noTone(buzzer);
    }
  }
}

NikosBond:
is there any way is this case to put them in arrays?

I will keep clear of music stuff.

...R

const int tones[] = {NOTE_E7, NOTE_E7, NOTE_E7, NOTE_C7, NOTE_E7, NOTE_G7, NOTE_G6, NOTE_C7, NOTE_G6, NOTE_E6, NOTE_A6, NOTE_B6, NOTE_AS6, NOTE_A6, NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, NOTE_F7, NOTE_G7, NOTE_E7, NOTE_C7,
  NOTE_D7, NOTE_B6, NOTE_C7, NOTE_G6, NOTE_E6, NOTE_A6, NOTE_B6, NOTE_AS6, NOTE_A6, NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, NOTE_F7, NOTE_G7, NOTE_E7, NOTE_C7, NOTE_D7, NOTE_B6 };

That is an array.