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).
#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...?
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!
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...
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.
#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.