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 5748 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…
Parents
  • 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
Reply
  • 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
Children
No Data
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