multiple loops...?

Hi,
I tried to search about if possible to have multiple loops.
For example: a loop is running with delay in every 5th or 10th minutes. Beside this I would like to use the LCD bakclight delay with checking a button state, what needs probably another loop (based on blinkwithoutdelay).

I have found something:
http://forum.arduino.cc/index.php?topic=45649.15

Is that right to use loops like this?

Thanks!

Is that right to use loops like this?

What bit of code specifically?
The latter stuff is right.

vespapierre:
a loop is running with delay in every 5th or 10th minutes.

Have you look at Blink Without Delay?

I mean code like what was on the posted topic:

#define loop1time 1000 //How many millis loop 1 will be used
#define loop2time 500 //How many millis loop 2 will be used
unsigned long startTime = 0;
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  unsigned long loopTime = millis() - startTime; //Calculate the time since last time the cycle was completed
  if (loopTime <= loop1time) //Check if the time is less than 1000 millis, and if so, run loop 1
  {
    Serial.println("Loop 1");
    loop1();
    delay(250); //Only here to prevent spamming serial monitor too much
  }
  else if (loopTime > loop1time && loopTime <= (loop1time + loop2time)) //If time is over 1000 millis and less than/or 2000 millis, run loop 2
  {
    Serial.println("Loop 2");
    loop2();
    delay(250); //Only here to prevent spamming serial monitor too much
  }
  else if (loopTime >= (loop1time + loop2time)) //If time is over 2000 millis, set the startTime to millis so the loop time will be reset to zero
  {
    startTime = millis();
  }
}
void loop1()
{
  digitalWrite(11, HIGH);
  delay(5);
  digitalWrite(12, HIGH);
  delay(5);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  delay(5);
}
void loop2()
{
  digitalWrite(10, HIGH);
  delay(5);
  digitalWrite(10, LOW);
  delay(5);
}

But connecting to JimboZA's question... Maybe the best way should be to change the current delay() to millis() like blink without delay, and than also use millis() with ckecking the button to LCD backlight...?

When ever you want to do two things at once you should never use the dealy function. The program needs to be written as a state machine.

Thanks!

Should I start to learn this?:
http://playground.arduino.cc/Code/FiniteStateMachine

or with this?
http://playground.arduino.cc/Code/SMlib

The code in the first post of this Thread may be useful.

...R

That's GREAT Robin2! Thanks!

Hi,
I have some problem with Robin2's sketch. The 2 function is not working paralelly.
For example in the loop I have:
lcd();
senddata();
in lcd() I am testing this: http://theuzo007.wordpress.com/2012/12/29/turn-on-lcd-backlight-by-button-arduino/
When senddata() starts, lcd() not working. And: in case of lcd backlight is just on than it will stay on during senddata(), if that was off, it will stay turned off!

Post your code and I will try to have a look at it.

...R

Hi Robin2,
attached, because of the length.
Thanks!
Using the millis() for sendingdata is also not ok, because it does not keep the 'delay' interval...

test.txt (10.9 KB)

Using the millis() for sendingdata is also not ok, because it does not keep the 'delay' interval...

long interval = 60000UL; //interval in milliseconds for GSM

Thank you PaulS!

So UL will make it really as unsigned long. In case should I change all other long or unsigned long values (using L or UL)? (previousMillis, lcdLightonTime) Will it be the medicine for working fine beside each other LCD and sendingdata?

I have had a quick look at your code. I'm not sure why you didn't post it as a .ino file? In my text editor there is a blank line between every line so the whole thing is over 800 lines long. It means I can only see very little on the screen at one time.

I haven't been able to relate the following quote to your code.

When senddata() starts, lcd() not working. And: in case of lcd backlight is just on than it will stay on during senddata(), if that was off, it will stay turned off!

Also there is a huge amount of specialized stuff in the code that I have no experience of and no ability to try. I don't think I can help much unless you can produce a minimal sketch that just illustrates the problem.

I find it a good idea when debugging to try to figure out why the code is doing what it is rather than trying to figure out why it is not doing what I want.

...R

#define loop1time 1000 //How many millis loop 1 will be used
#define loop2time 500 //How many millis loop 2 will be used
unsigned long startTime = 0;
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  unsigned long loopTime = millis() - startTime; //Calculate the time since last time the cycle was completed
  if (loopTime <= loop1time) //Check if the time is less than 1000 millis, and if so, run loop 1
  {
    Serial.println("Loop 1");
    loop1();
    delay(250); //Only here to prevent spamming serial monitor too much
  }
  else if (loopTime > loop1time && loopTime <= (loop1time + loop2time)) //If time is over 1000 millis and less than/or 2000 millis, run loop 2
  {
    Serial.println("Loop 2");
    loop2();
    delay(250); //Only here to prevent spamming serial monitor too much
  }
  else if (loopTime >= (loop1time + loop2time)) //If time is over 2000 millis, set the startTime to millis so the loop time will be reset to zero
  {
    startTime = millis();
  }
}
void loop1()
{
  digitalWrite(11, HIGH);
  delay(5);
  digitalWrite(12, HIGH);
  delay(5);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  delay(5);
}
void loop2()
{
  digitalWrite(10, HIGH);
  delay(5);
  digitalWrite(10, LOW);
  delay(5);
}

It's like somebody bred a hawk with a turtle.
Whoever wrote that does not understand the lesson of BlinkWithoutDelay.

That comes from the first linked topic... I am using Robin2's example and will try to analyze that.
Thanks for all!