Time Loops.

Hi everyone!! :D. I'd to know how can I do for repeat a loop during a time and pass to other. Thank you. :)

Could you rephrase the question please? It doesn't make a great deal of sense.

Have you found the blink without delay tutorial?

I've also found the MsTimer2 library very useful for sketches needing a interrupt driven timing reference for handling things like you hinted at possibly. http://www.arduino.cc/playground/Main/MsTimer2

Lefty

Could you rephrase the question please? It doesn't make a great deal of sense.

(Sorry for my English xD).

I mean, I have something like this, for example:

void loop() {
  digitalWrite(11, HIGH);
  delay(5);
  digitalWrite(12, HIGH);
  delay(5);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  delay(5);
}

And I want to repeat this during a few seconds and later do another thing, another loop, It is also ok mark the cycles I want instead the time. Thanks.

I think that what you need is millis()

It is also ok mark the cycles I want instead the time.

Huh :-? No idea what that means

I think that what you need is millis()

I think that millis() doesn't do what I want... :S because it works as delay(), true? I need to repeat a loop and pass to other loop. I have a loop with changes of 5 msec and I want to repeat that combination during a few seconds but I cannot copy/paste the code a hundred times, so, I need something that make possible repeat a loop and pass to other.

Huh :-? No idea what that means

I want to say that it would be correct for me be able to write how much times I would to repeat the loop instead of write the time I want for each loop.

I don't know if I explain very well.. :S

Don't understand your comment about delay vs millis. The blink without delay does just that; it schedules events without using the call to delay, which would block and prevent other things happening.

What is your native language? Maybe there is a forum in it.

Ok, thank you.

I'm going to think a lot and try to do something ;)

I think that millis() doesn’t do what I want… :S because it works as delay(), true?

No, with millis you can calculate how much time passed, without stopping (delaying) the entire loop.

I need to repeat a loop and pass to other loop. I have a loop with changes of 5 msec and I want to repeat that combination during a few seconds but I cannot copy/paste the code a hundred times, so, I need something that make possible repeat a loop and pass to other.

millis() is what you need then, and you then only need to put the functions in you need, it can then jump back and forth between them. This is exactly what I am doing here http://bld.is-a-geek.com/2010/10/31/using-millis/ where I am timing the leds to when the button was pressed. In your case you then set the time in the functions instead of the button, so ex:

millis - startTime <= 1000 do loop 1
millis - startTime > 1000 do loop 2
millis - startTime >= 2000 set startTime = millis();

Like this tjhe loop 1 will be used for 1 second
Loop 2 will then be used for another second
After 2 seconds passed, startTime will be set to millis(), so millis - startTime will be zero again, and that then means that loop 1 again will be used, because millis - startTime <= 1000 is true

Switching loop

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 <= 1000) //Check if the time is less than 1000 millis, and if so, run loop 1
  {
    Serial.println("Loop 1");
  }
  else if (loopTime > 1000 && loopTime <= 2000) //If time is over 1000 millis and less than/or 2000 millis, run loop 2
  {
    Serial.println("Loop 2");
  }
  else if (loopTime > 2000) //If time is over 2000 millis, set the startTime to millis so the loop time will be reset to zero
  {
    startTime = millis();
  }
  
  delay(250); //Only here to prevent spamming serial monitor too much
}

millis() is what you need then, and you then only need to put the functions in you need, it can then jump back and forth between them. This is exactly what I am doing here http://bld.is-a-geek.com/2010/10/31/using-millis/ where I am timing the leds to when the button was pressed. In your case you then set the time in the functions instead of the button, so ex:

millis - startTime <= 1000 do loop 1
millis - startTime > 1000 do loop 2
millis - startTime >= 2000 set startTime = millis();

Like this tjhe loop 1 will be used for 1 second
Loop 2 will then be used for another second
After 2 seconds passed, startTime will be set to millis(), so millis - startTime will be zero again, and that then means that loop 1 again will be used, because millis - startTime <= 1000 is true

Ok, thank you very much. So, I have this

void loop() {
  digitalWrite(11, HIGH);
  delay(5);
  digitalWrite(12, HIGH);
  delay(5);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  delay(5);
}

AND

void loop() {
digitalWrite(10, HIGH);
delay(5);
digitalWrite(10, LOW);
delay(5);
}

How I can differentiate a loop of another?
Where I have to put
“millis - startTime <= 1000 do loop 1
millis - startTime > 1000 do loop 2
millis - startTime >= 2000 set startTime = millis();” ?

How I can differentiate a loop of another?
Where I have to put
“millis - startTime <= 1000 do loop 1
millis - startTime > 1000 do loop 2
millis - startTime >= 2000 set startTime = millis();” ?

Nowhere, that was only to explain you how to do it

I guess this is what you need. It will loop the first one for 1 second, then loop the next one for another second, then switch back and loop the first for a second again, and so on…

unsigned long startTime = 0;

void setup()
{
}

void loop()
{
  unsigned long loopTime = millis() - startTime; //Calculate the time since last time the cycle was completed

  if (loopTime <= 1000) //Check if the time is less than 1000 millis, and if so, run loop 1
  {
    digitalWrite(11, HIGH);
    delay(5);
    digitalWrite(12, HIGH);
    delay(5);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    delay(5);
  }
  else if (loopTime > 1000 && loopTime <= 2000) //If time is over 1000 millis and less than/or 2000 millis, run loop 2
  {
    digitalWrite(10, HIGH);
    delay(5);
    digitalWrite(10, LOW);
    delay(5);
  }
  else if (loopTime > 2000) //If time is over 2000 millis, set the startTime to millis so the loop time will be reset to zero
  {
    startTime = millis();
  }
}

I guess this is what you need. It will loop the first one for 1 second, then loop the next one for another second, then switch back and loop the first for a second again, and so on...

Ok, sincerely, thank you. I'm going to check what can I do and then, I post ;)

All you need to do, is to change the 1000 and 2000 values to change the amount of time each loop will run

All you need to do, is to change the 1000 and 2000 values to change the amount of time each loop will run

I have a problem... It says "redefinition of 'void setup()'" when I click compile :S

You've got two (or more) "setup" functions. Merge the codes into a single one.

Another way of doing it.

Here the time for each loop is defined in the top, and then calculated inside the loop.
At the same time I have put the loop1 and loop2 into each their function, to keep the loop as clean as possible.

#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);
}

I have a problem… It says “redefinition of ‘void setup()’” when I click compile :S

You haven’t copied and pasted it exactly as the code I posted then, I am seeing it running on my Arduino.

You've got two (or more) "setup" functions. Merge the codes into a single one.

Ok, perfect, it works ;)

Here the time for each loop is defined in the top, and then calculated inside the loop. At the same time I have put the loop1 and loop2 into each their function, to keep the loop as clean as possible.

Thanks, here it is very clearly, I'm going to write ;D When done, I'll tell here :)

I don't know why, but copying/pasting the code as you write here, the Arduino doesn't do anything... The TX led powers on and doesn't work :-/

What error is it giving you?

If it only turns on, and never uploads, something else is wrong than the sketch.

If it only turns on, and never uploads, something else is wrong than the sketch.

It doesn't give me an error, It says "done uploading". The RX and TX leds blink and then, it does'n do anything... The pins are putted correctly, and if I write a easier code, It works...