element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum How can I run multiple loops at the same time with an Arduino?
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 8 replies
  • Subscribers 391 subscribers
  • Views 5745 views
  • Users 0 members are here
  • loops
  • help
  • blinky
  • pwm
  • led
  • code
  • lights
  • stuff
  • with
  • independent
  • suit
  • Making
  • arduino
  • a
  • laser
  • pew
  • mech
  • question
Related

How can I run multiple loops at the same time with an Arduino?

Former Member
Former Member over 10 years ago

I was wondering if it would be possible to run 2 or more loops at the same time with an Arduino. I want to have a few LEDs blink (go around in a circle) while having another (in the middle of the others) use PWM. I already tried making another void loop, but it does not work. Here is the code that I have been using:

 

 

int red = 6;

int brightness = 0;

int fadeAmount = 4;

int green1 = 13;

int blue1 = 12;

int yellow1 = 11;

int green2 = 10;

int blue2 = 9;

int yellow2 = 8;

 

void setup() {

pinMode(red, OUTPUT);

pinMode(yellow1, OUTPUT);

pinMode(green1, OUTPUT);

pinMode(blue1, OUTPUT);

pinMode(yellow2, OUTPUT);

pinMode(green2, OUTPUT);

pinMode(blue2, OUTPUT);

}

 

void loop() {

  analogWrite(red, brightness);       // this first part was copied from the "Fade" example with some small modifications (pin numbers, brightness)

  brightness = brightness + fadeAmount;

  if (brightness == 0 || brightness == 255) {

    fadeAmount = -fadeAmount ;

  }

  delay(10);

 

// I want to have everything below this be part of a seperate loop

 

  digitalWrite(green1, 1);

  digitalWrite(yellow2, 0);

  delay(100);

  digitalWrite(blue1, 1);

  digitalWrite(green1, 0);

  delay(100);

  digitalWrite(yellow1, 1);

  digitalWrite(blue1, 0);

  delay(100);

  digitalWrite(green2, 1);

  digitalWrite(yellow1, 0);

  delay(100);

  digitalWrite(blue2, 1);

  digitalWrite(green2, 0);

  delay(100);

  digitalWrite(yellow2, 1);

  digitalWrite(blue2, 0);

  delay(100);

}




I have been using an Arduino Leonardo and the sections work fine separately; I just want them to use different pins and run independently, but at the same time.

  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz over 10 years ago +1
    See the discussion here: http://www.element14.com/community/message/129449/l/re-multiple-arduino-functions-without-delay Basically just check the millisecond timer and call a couple of functions at the…
  • mcb1
    mcb1 over 10 years ago in reply to shabaz +1
    The IMO poorly worded example " BlinkWithouDelay" shows how to achieve waht Shabaz has pointed you to. I use an analogy of filling a paddling pool. You can stand there holding the hose until it is full…
  • shabaz
    0 shabaz over 10 years ago

    See the discussion here: http://www.element14.com/community/message/129449/l/re-multiple-arduino-functions-without-delay

    Basically just check the millisecond timer and call a couple of functions at the appropriate times - one function to do the fading for one LED, and the other to do the blinking for the other LED.

    There is more detail in that discussion thread.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • balearicdynamics
    0 balearicdynamics over 10 years ago

    Hello,

     

    to run multiple loop at the same time, independently, you should ... NOT use a second loop ! image

     

    The loop() function in the Arduino program skeleton is a sort of "fake"; as a matter of fact it is only a way to tell the CPU "do this sequence indefinitely", but it is not covering the loop following the concept for(..) { } as it is usually intended in C/C++ programming. So first of all forget this vision.

     

    Another thing that is too frequently used but as a matter of fact is more a damage than an advantage is the Delay(). It is a function blocking the processor for a certain period, so it is not the worth to use it in your case, but I suggest to never use the Delay() in programs excluding when you are doing tests to see if a certain part works.

     

    Instead you should use a counter. If you initialise a long int global variable you can read the time of the processor in every moment without stopping the execution of the entire program.

     

    long timeCounter; // milliseconds counter
    long timePassed; // The actual milliseconds time
    
    // We need a "loop" doing event 1 every 10 ms, one doing event 2 every 5 ms
    // and one doing event 3 every 25 ms
    long delay1 = 10;    // ms for cycle of event 1
    long delay2 = 5;     // ms for cycle of event 2
    long delay3 = 25;    // ms for cycle of event 3
    
    // Counters for the number of triggered events before the
    // corresponding event starts
    int countEvent1, countEvent2, countEvent3;
    
    int TRIGGER = 5; // ms to trigger the milliseconds timer
    
    int MAX1 = 2; // Number or triggered milliseconds to start event1
    int MAX2 = 1; // Number or triggered milliseconds to start event2
    int MAX3 = 5; // Number or triggered milliseconds to start event3
    
    // Initialisation
    setup {
         timeCounter = millis(); // read the actual internal time
         timePassed = 0; // we start with no time passed
    
         countEvent1 = 0;
         countEvent2 = 0;
         countEvent3 = 0;
    }
    
    // Infinite cycle
    loop() {
    
         // Read the actual time
         timePassed = millis() - timeCounter;
         // The smallest unit of time that should trigger an event is for event 2 (5 ms)
         // so our trigger should count this smaller unit
    
         // Check if the time passed after last reading is the trigger time
         if(timePassed >= TRIGGER) {
              // It's time to manage the events
              if(++countEvent1 == MAX1){
                   // Reset the trigger and start the event
                   event1();
                   countEvent1 = 0;
              } // Event 1 ???
              
              // It's time to manage the events
              if(++countEvent1 == MAX2){
                   // Reset the trigger and start the event
                   event2();
                   countEvent2 = 0;
              } // Event2 ???
    
              // It's time to manage the events
              if(++countEvent3 == MAX3){
                   // Reset the trigger and start the event
                   event3();
                   countEvent3 = 0;
              } // Event3 ???
    
         } // Trigger ??
    
    } // LOOP
    
    // ============ Event functions
    
    event1() {
    // Add the code for your event
    }
    
    event2() {
    // Add the code for your event
    }
    
    event3() {
    // Adde the code for your event
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 10 years ago in reply to shabaz

    The IMO poorly worded example  " BlinkWithouDelay" shows how to achieve waht Shabaz has pointed you to.

     

    I use an analogy of filling a paddling pool.

     

    You can stand there holding the hose until it is full.

    You can't do anything else ... which is what Delay() does.

     

    OR

     

    You can start the filling and come back at regular intervals.

    You note the time and decide to come back 5 mins later to see if it is full.

    Meanwhile you are free to do other things ...this is where the use of the Millis() timeer comes in.

     

    Mark

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • balearicdynamics
    0 balearicdynamics over 10 years ago in reply to mcb1

    shabaz pointed to another question that has the same problem of many others, unfortunately. It appears as unanswered that may generate confusion on what is the right procedure to follow. Do you have somenideas on how it is possible to avoid this? there is not something specific to signal to the users that simply thanking for the answers is not useful for the others...

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 10 years ago in reply to balearicdynamics

    some ideas on how it is possible to avoid this?

    The best you can do is mark other answers useful ....

     

    There are many methods to achieve a result, and some will argue this is the best way, others will argue this way.

    As far as I am concerned, if the OP understands the concept, and the result does what they want, then they can refine or improve on it when they need to.

     

    IMO writing short cryptic code just because it runs faster is a waste of energy if it just sits there waiting, and is difficult to understand.

     

    mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • balearicdynamics
    0 balearicdynamics over 10 years ago in reply to mcb1

    Mark,

     

    IMO writing short cryptic code just because it runs faster is a waste of energy if it just sits there waiting, and is difficult to understand.

    I have not understood if you refers in general or to my example in this question.

     

    Enrico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mcb1
    0 mcb1 over 10 years ago in reply to balearicdynamics

    No ones example, just that there are often several different ways to do something.

    One might have lots of comments and do extra manipulation, while other methods might be one or two lines.

     

     

    I have found that new comers sometimes get focused on the code, and don't understand what the concept is  ... hence the pool example.

    Once you understand WHAT you are trying to achieve, it is often easier to understand the solution or answers provided.

     

    In your example you use the 'long' declaration but a new comer doesn't understand why unless you point it out  ... yes they could look it up, but will they.

    Sometimes we forget that it has become second nature to us, but for anyone new they are still learning ...

     

    It's not unlike driving a car when you are teaching someone who has never done it.

     

     

    Does that make sense

     

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • gadget.iom
    0 gadget.iom over 10 years ago

    Hi Andrew

     

    Have you got this working yet?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube