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


hey guys, it first time work arduino, know nothing more basic

i read on other topics cant run 2 loops, there instead other ways millis(); comand, didnt understand , need


so, connected light sensor on board , long analogread under 200 want 2 things simultaneously: play music , make leds blink
however, delay(); causes me huge problem, make 1 run after other.
can tell me way fix this?
thanks in advance  :)

here loop:

code: [select]
void loop() {
int pr=analogread(lightpin);
serial.println(pr);
if (pr<200) {
    (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, can't run 2 loops @ once, , real world programs avoid delay(), except perhaps very-short delay depending on application.

look @ blink without delay example.   try understand concepts , logic of how works...

in application you'll need (at least) 2 different previousmillis , 2 (or more) interval variables keep timing separate.   example, name variables previousmillistone, previousmillisled1, previousmillisled2, etc.    need 1 currentmillis, because current time current time whenever read it!

this going confusing, i'd start 2 leds, each different timing first.   when figured-out can additional timed events.


looking @ led timing, can use 1 led timer , step-through sequence.


Arduino Forum > Using Arduino > Project Guidance > School Project: How to run 2 "loops" at the same time


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code