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 Multiple Arduino functions without delay
  • 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 20 replies
  • Subscribers 395 subscribers
  • Views 2584 views
  • Users 0 members are here
  • timers
  • arduinouno
  • delay
  • interval_timer
  • delay_interval
  • led
  • arduino
  • solenoid
Related

Multiple Arduino functions without delay

Former Member
Former Member over 10 years ago

I have literally zero coding experience but have run into the need for it at my job -- all help appreciated. Basically, I am trying to accomplish many things at once and am not sure how to translate what i need into code. What I need: a tone and a light to simultaneously flash on for 500 milliseconds and then off again for 6 seconds; water reward to be delivered (by turning on/ off a solenoid) 2 seconds after the tone/LED have turned off, with the solenoid being open for 25 milliseconds; continuous collection of data from an input potentiometer. Since i need to monitor the input from the potentiometer at all times, i cannot use "delay()" because i cannot have the board stop reading the values from the potentiometer to preform a function.

 

This is a code i attempted -- however, the issue is the timing for the LED/tone and the solenoid. I need the solenoid to always open 2 seconds after the LED/tone turn off and it needs to open for 25 milliseconds and then shut back off. i have put hours into trying to figure out a way to do this --- as the code is now, the led/tone turn on and off fine, but the solenoid has a longer delay time (because i was trying to get it to come 2 seconds after the led/tone went off) and the excess time builds onto itself and makes the timing of the solenoid vary in its proximity to the LED/tone.

 

#include <elapsedMillis.h> //the timer.

 

 

//stopwatches

elapsedMillis timer; 

 

 

int ledPin = 11;

int lick = 4;

int solenoid = 8;

int speaker = 2;

int ledOn = 500;       //milliseconds

int ledOff = 6000;

int solenoidOn = 25;

int solenoidOff = 8475;

int LickState;

 

 

int playTime = 500;  //how long speakers will play for

int freq = 8000;  //frequency for left prize speakers in hz

int toneTime = 500;

 

 

unsigned long lms;        //time from millis()

unsigned long lmsLast;    //last time the LED changed state

unsigned long sms;        //time from millis()

unsigned long smsLast;

unsigned int LedOn = 0;

unsigned int Reward = 0;

unsigned int Lick = 0;

boolean ledState;        //current LED state

boolean solenoidState;

 

 

void setup(void){

  Serial.begin(9600);

  pinMode(lick, INPUT); 

  pinMode(ledPin, OUTPUT);

  pinMode(solenoid, OUTPUT);

  pinMode(speaker, OUTPUT);

 

 

  elapsedMillis timer = 0;

}

 

 

void loop(void){

  int ledState = digitalRead(ledPin);

  int lickstate = digitalRead(lick);

  if (lickstate == HIGH){

//    Serial.println(lickstate);

    Serial.println(timer); }

  lms = millis();

    blinkLED();

  sms = millis();

    blinkLED();

   

 

} }

 

 

void blinkLED(void){

 

 

    if (lms - lmsLast > (ledState ? ledOn : ledOff)) {

        digitalWrite(ledPin, ledState = !ledState);

        lmsLast = lms;

        tone (speaker, freq, toneTime);

        Serial.print("Led");

        Serial.println(timer);

    }

    if (sms - smsLast > (solenoidState ? solenoidOn : solenoidOff)) {

        digitalWrite(solenoid, solenoidState = !solenoidState);

        smsLast = sms;

        Serial.print("Reward");

        Serial.println(timer);

    }

}

 

I have tried using if then statements to say that if the LED state is HIGH, then wait 2 seconds and deliver the reward --- the issue is the "wait 2 seconds part". how do i get the arduino to add two seconds to the current time when the LED turns off and then give my 25 millisecond reward without messing up the on/off times of the LED? Very confused.

  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz over 10 years ago +1
    This is a classic problem when creating real-time systems (which this is, since you want things to occur at or within certain times). Writing code without an OS to provide real time capabilities is difficult…
  • jw0752
    0 jw0752 over 10 years ago

    Hi, I am probably not the one to answer your question as there are many guys this forum with more experience than me. I suspect that you may be able to solve your problem using "interrupts". You will have to investigate how they work but I know that they do not stop the program like delay() does. The Arduino by its design cannot do two things at once but you should be able to accomplish what you outlined above. I will watch to see if I can be of any help but as soon as one of the real experts check out your question you may get a more applicable reply.

    John

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

    This is a classic problem when creating real-time systems (which this is, since you want things to occur at or within certain times). Writing code without an OS to provide real time capabilities is difficult. If you just try to execute things one at a time, then you get a result as shown in the first diagram below. Then when you add one more function to your program, the timing for everything is affected and your cycle execution time stretches (see the second diagram below). Then you have to try and reduce any delays in your program to compensate, and it's all an unpleasant mess.

    image

     

    You might end up with an interrupt based solution as John mentions, and that will probably meet your needs. This entails you structuring the code around the concept of timer interrupt 'ticks' and you can count them, and dispatch or execute tasks at the correct ticks (each task running to completion, so you need to make sure it finishes before the next tick). If you want to slow things down (e.g. a slow blinking LED) then you need to retain the state of the LED (on or off) and the amount of ticks that you've been in that state. And so on, for all the things you want to do in your software.

    The above is a general description, which you need to translate into software. The diagram below shows the concept. Each task must complete before the next tick.

    image

    At some point, when a program becomes more complex, you may need or want to make use of an OS, and move to a different processor probably (since OS's consume resources, probably more than the Arduino has).

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

    Hi Shabaz, What do you think of using the Arduino strictly as a time keeper and a potentiometer monitor and having it trigger astable 555 timers to complete his other subtasks? Perhaps even the use of a real time clock and interrups could eliminate the counting of clock ticks?

    John

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

    Hi FIN, Here is one more idea though it is theoretical as I have never done it myself. Perhaps you could synchronize real time clocks and use them with multiple arduinos so that you could effectively do multiple actions at once. The multiple arduinos would be parrallel processors which would give you lots of flexibility. One to read the potentiometer, one to blink the light, and etc.

    John

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago in reply to jw0752

    I don’t have time to document my full thoughts on this until next week but this is very doable just with the Arduino

     

     

     

    As Shabaz stated, if the requirement was more complex, then a different approach would be required but this is not a complex requirement

     

     

     

    You need the main loop to be none blocking and a set of timer variables that will be based on the output from millis(), then compared to the current Millis()

     

     

     

    A simple state machine can be setup for this with the time being reset for the delay to the next state

     

     

     

    I will try to write this up over the next few days if Im not explaining myself well enough for you to figure it out

     

     

     

     

     

    My only question is how accurate does the loop need to be to trigger the sequence

     

     

     

    You mention the LED and tone fire together for the same period, then a delay, then briefly fire the solenoid, how long before that repeats or is there some kind of manual trigger

     

     

     

    Regards

     

     

     

    Peter

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

    Hi John!

     

    That's feasible, I see what you mean (use the 555's as pulse stretchers). It could be an option, but is generally avoided because software is (supposedy) free image

    and more flexible to alter parameters in a program than change capacitor/resistor values. The Arduino has the resources for what FiN wants to do,

    but will involve him a bit of time in creating his software structure based around the tick type method. It's quite a common thing (as are state machines, which is

    effectively what he'll need to create to get the LED behavior, solenoid behavior, etc. implemented), so he should be able to find descriptions or pseudocode examples

    on the Internet (or wait for Peter's post on the topic, which I'm sure will detail it extremely well).

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

    FiN,         some good answers so far.  If I understand you correctly you want a continuously running process where every 6 seconds a tone and light event occurs for 500 milliseconds, then 2 seconds after the tone turns off you want a water event for 25 milliseconds.  Whilst at the same time you want to continuously monitor an analogue input.  Provided the tone is generated by a 'buzzer' for example then I think you can accomplish the above requirement using the milllis() function Peter referred to.  If you Google Arduino non blocking delays you will find some good examples of non blocking delay code.

    In principle if you want your chain of events to occur every 6 seconds then you could use current millis() + 60000 to create a trigger every 6 seconds.  At the trigger, set the tone output and LED on also set a Boolean flag to true, then do an IF with the flag and a current millis() + 500.  When this IF is true turn off the buzzer / LED and clear the flag, set another different flag and do another IF with millis() + 2000.

     

    I hope you can follow the above I am using flags and millis() to control the various outputs whilst have no delays.  The analog read would occur continuously and you do not say how you wish to process this information.  I hope this helps because you can develop your code one stage at a time.  There are undoubtedly better ways to do what you want but I think the above method is reasonably straight forward.

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

    Hi everyone!!


    Thank you all so much for the responses -- it's unbelievably helpful.


    Exactly what bobcroft said is correct. What I am trying to accomplish would go something like this:

    Time: 0 milliseconds

    tone/LED delivered for 500 milliseconds

    nothing delivered for 2000 milliseconds (bringing us to 2500 milliseconds into loop)

    solenoid delivers for 25 milliseconds (bringing us to 2525 milliseconds into loop)

    Nothing delivers for 3475 milliseconds (bringing us to the end of the loop at 6000 milliseconds)

     

    then the loop begins again with the tone/LED.

     

    All the while I get a serial.print of the input from the potentiometer as well as a serial print of the on/off of the LED, tone, and solenoid with their time stamps. As I said I have never written code before (in truth I wasn't even sure what language of code I was even dealing with before I did some heavy googling). I put the above code together using examples and help from the website -- so I wasn't sure if what I was trying to do actually has a very simple fix or wasn't something the system could do and I just am too unfamiliar with the process to know.

     

    All of your suggestions are awesome. Definitely giving me confidence that I will be able to get it up and working!

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

    Friend

    use the Millis() timer as you have been.

    Store the Millis() time when you start the process (LED, Nothing, Solenoid, Nothing), then as you loop through reading your pot, check if the time difference causes you to stop/start something else.

     

    Your example code (use the >> Syntax Highighting  and C++ to make it like below) at line 53 -56 appear outside of the If statement. This means they will always trigger every loop.

    I'm not sure what lickstate is, but it just doesn;t seem right.

     

     

    #include <elapsedMillis.h> //the timer.
    
    
    //stopwatches
    elapsedMillis timer;
    
    
    int ledPin = 11;
    int lick = 4;
    int solenoid = 8;
    int speaker = 2;
    int ledOn = 500;       //milliseconds
    int ledOff = 6000;
    int solenoidOn = 25;
    int solenoidOff = 8475;
    int LickState;
    
    
    int playTime = 500;  //how long speakers will play for
    int freq = 8000;  //frequency for left prize speakers in hz
    int toneTime = 500;
    
    
    unsigned long lms;        //time from millis()
    unsigned long lmsLast;    //last time the LED changed state
    unsigned long sms;        //time from millis()
    unsigned long smsLast;
    unsigned int LedOn = 0;
    unsigned int Reward = 0;
    unsigned int Lick = 0;
    boolean ledState;        //current LED state
    boolean solenoidState;
    
    
    void setup(void){
      Serial.begin(9600);
      pinMode(lick, INPUT);
      pinMode(ledPin, OUTPUT);
      pinMode(solenoid, OUTPUT);
      pinMode(speaker, OUTPUT);
    
    
      elapsedMillis timer = 0;
    }
    
    
    void loop(void){
      int ledState = digitalRead(ledPin);
      int lickstate = digitalRead(lick);
      if (lickstate == HIGH){
    //    Serial.println(lickstate);
        Serial.println(timer); }
      lms = millis();
        blinkLED();
      sms = millis();
        blinkLED();
    
    
    } }
    
    
    void blinkLED(void){
    
    
        if (lms - lmsLast > (ledState ? ledOn : ledOff)) {
            digitalWrite(ledPin, ledState = !ledState);
            lmsLast = lms;
            tone (speaker, freq, toneTime);
            Serial.print("Led");
            Serial.println(timer);
        }
        if (sms - smsLast > (solenoidState ? solenoidOn : solenoidOff)) {
            digitalWrite(solenoid, solenoidState = !solenoidState);
            smsLast = sms;
            Serial.print("Reward");
            Serial.println(timer);
        }
    }

     

     

    Also you state its for work, hopefully in the spirit of OpenSource and Free advise you might be able to enlighten us as to what it is.

     

    Mark

     

    PS I've missed something in my Arduino butchering

    What exactly does this bit do in the If statement.

    (ledState ? ledOn : ledOff)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago

    here is an alternate approach you can try

     

    I have not included the actual digital IO, you already know how to do that but it does use a rudimentary state machine that should work for you and is easy extended if you need

     

    the code does compile on an arduino but I have not uploaded or tested it yet

     

    it will run every 6 seconds, will never re-run until the state machine is complete and reached the 6 seconds (Or should behave that way image )

     

    hope it helps, I will try to execute it and verify i got it right over the next few days but you can have a look now and try it for your self if you feel lucky image

    // constants
    #define  solenoidOnTime 25 // 25mS
    #define  LEDBuzzerTime 500 // 500mS
    #define  SolenoidDelayTime 2000 // 2 seconds
    #define  LoopTime 6000 // 6 seconds
    
    #define LEDPin 8
    #define BUZZERPin 9
    #define SOLENOIDPin 11
    
    //variables
    double LoopTrigger = 0; // loop timer
    int stateMachineValue = -1; // state tracker “-1” = waiting for trigger
    double nextState = 0; // used by the state machine function for timing
    
    void setup()
    {
      Serial.begin(9600); // add other initialization stuff
      LoopTrigger = millis();
      stateMachineValue = 0; // start state machine
    }
    
    void loop()
    {
      if ( (LoopTrigger <= (millis() - LoopTime)) && (stateMachineValue == -1)) // waiting for trigger and looptime has elapsed
      {
        stateMachineValue = 0 ; // start the state machine
        LoopTrigger = millis(); // reset loop timer, not this will not trigger until the state machine has completed
      }
      stateMachine();
    }
    
    void stateMachine()
    {
      switch (stateMachineValue)
      {
      case 0: 
        {
          nextState = millis();
          LEDBUZZERON();
          stateMachineValue ++; // next state
          break;
        }
      case 1: 
        {
          if(nextState <= (millis()-LEDBuzzerTime))
          {
            nextState = millis();
            LEDBUZZEROFF();
            stateMachineValue++; // next state
            break;
          }
        }
      case 2: 
        {
          if(nextState <= (millis()-SolenoidDelayTime))
          {
            nextState = millis(); 
            SOLENOIDON();
            stateMachineValue++; 
            break;
          }
        }
      case 3: 
        {
          if(nextState <= (millis()-solenoidOnTime))
          {
            nextState = millis(); 
            SOLENOIDOFF();
            stateMachineValue == -1;  // end state machine
            break;
          }
        }
        default:
        {
          LEDBUZZEROFF();
          SOLENOIDOFF();
        }
      }
    }
    void  LEDBUZZERON()
    {
      Serial.println("LED and Buzzer ON");
    }
    void  LEDBUZZEROFF()
    {
      Serial.println("LED and Buzzer OFF");
    }
    void  SOLENOIDON() 
    {
      Serial.println("LED and Buzzer ON");
    }
    void  SOLENOIDOFF() 
    {
      Serial.println("LED and Buzzer OFF");
    }

    • 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